Skip to content

Instantly share code, notes, and snippets.

@adriannier
Last active September 3, 2015 13:54
Show Gist options
  • Save adriannier/3b2a06259e200783ef5c to your computer and use it in GitHub Desktop.
Save adriannier/3b2a06259e200783ef5c to your computer and use it in GitHub Desktop.
(*
Allows the user to grab a screen selection and places a scaled version on the clipboard.
*)
on run
try
-- Allow the user to grab a screen shot to the clipboard
screenSelectionAsPNGToClipboard()
-- Scale the PNG image on the clipboard
scalePNGOnClipboard()
on error eMsg number eNum
if eNum = -128 then return
activate
set dialogMessage to eMsg
if eNum is not -2700 then set dialogMessage to dialogMessage & " (" & (eNum as text) & ")"
display alert "Something went wrong" message dialogMessage
end try
end run
on scalePNGOnClipboard()
-- Get the PNG data from the clipboard
set imageData to pngFromClipboard()
-- Generate a temporary file name
set tempImageFilePath to temporaryPathWithFolderName(false) & ".png"
-- Write the PNG data to temporary file
writeToFile(imageData, tempImageFilePath, {})
-- Scale the image in the temporary file
scaleImageAtPath(tempImageFilePath, 50)
-- Set the clipboard to the scaled iamge
set the clipboard to (read file tempImageFilePath as «class PNGf»)
-- Delete the temporary file
do shell script "rm -f " & quoted form of (POSIX path of tempImageFilePath)
end scalePNGOnClipboard
on scaleImageAtPath(filePath, percentage)
-- Make sure we’re dealing with an HFS path
set filePath to anyPathToHFSPath(filePath)
-- Use Image Events to open, scale and save the image
tell application "Image Events"
set loadedImage to open file filePath
scale loadedImage by factor (percentage / 100)
close loadedImage saving yes
end tell
end scaleImageAtPath
on screenSelectionAsPNGToClipboard()
try
-- Use screencapture command line tool to enter interactive sceen shot mode
do shell script "screencapture -i -c -t png -x"
on error eMsg number eNum
if eNum is 1 then error "User canceled." number -128
error eMsg number eNum
end try
end screenSelectionAsPNGToClipboard
on pngFromClipboard()
try
return «class PNGf» of (the clipboard)
on error
error "There is no PNG data on the clipboard."
end try
end pngFromClipboard
on anyPathToHFSPath(filePath)
-- Expand tilde in filePath
if filePath starts with "~" then
-- Get the path to the user’s home folder
set userPath to POSIX path of (path to home folder)
-- Remove trailing slash
if userPath ends with "/" then set userPath to text 1 thru -2 of userPath as text
if filePath is "~" then
set filePath to userPath
else
set filePath to userPath & text 2 thru -1 of filePath as text
end if
end if
-- Convert to HFS style path if necessary
if filePath does not contain ":" then set filePath to (POSIX file filePath) as text
return filePath
end anyPathToHFSPath
on writeToFile(content, filePath, options)
try
set filePath to anyPathToHFSPath(filePath)
-- Set default options
set overrideType to false
set atomically to false
set appendNewContent to false
set appendWithNewline to false
set newlineCharacter to ASCII character 10
-- Get specified options
try
set overrideType to (overrideType of options)
end try
if overrideType is false then set overrideType to class of content
try
set atomically to (atomically of options)
end try
try
set appendNewContent to (appendNewContent of options)
end try
try
set appendWithNewline to (appendWithNewline of options)
end try
try
set newlineCharacter to (newlineCharacter of options)
end try
set originalContent to "<-FileHadNoContent->"
if atomically is false then
set tempPath to filePath
else
(***** ATOMIC WRITING *****)
-- Determine file name and directory
-- Create file path for atomical writing
set prvDlmt to text item delimiters
try
set text item delimiters to ":"
set fileName to text item -1 of filePath
set parentFolderPath to (text items 1 thru -2 of filePath as text) & ":"
set text item delimiters to prvDlmt
on error errorMessage number errorNumber
set text item delimiters to prvDlmt
error "Error while determining file name and directory: " & errorMessage number errorNumber
end try
-- Initializes variables to split file name into base and suffix
set baseName to fileName
set suffix to ""
if fileName contains "." then
-- Set delimiters
set prvDlmt to text item delimiters
set text item delimiters to "."
-- Get rid of everything
set baseName to (text items 1 thru -2 of fileName) as text
-- Get the last text item; that should be the suffix
set suffix to text item -1 of fileName
-- Restore delimiters
set text item delimiters to prvDlmt
if suffix contains " " or baseName is "" or suffix is "" then
set baseName to fileName
set suffix to ""
end if
end if
-- Create a unique temporary file path
set i to 1
repeat
set tempPath to parentFolderPath & baseName & "_" & (i as text) & "." & suffix
tell application "System Events" to if (exists file tempPath) is false then exit repeat
set i to i + 1
end repeat
-- Read original file content if necessary
if appendNewContent then
tell application "System Events" to set fileExists to (exists file filePath)
if fileExists then
try
open for access file filePath
on error errorMessage number errorNumber
error "Could not open file for reading: " & errorMessage number errorNumber
end try
try
set originalContent to read file filePath as overrideType
on error errorMessage number errorNumber
try
close access file filePath
end try
error "Could not read original file: " & errorMessage number errorNumber
end try
try
close access file filePath
end try
end if
end if
end if
-- Open file
try
open for access file tempPath with write permission
on error errorMessage number errorNumber
error "Could not open file with write permission: " & errorMessage number errorNumber
end try
-- Write to file
try
-- Determine end of file and data to write
if appendNewContent is false then
set eof of file tempPath to 0
set fileEnd to 0
set writeData to content
else
try
set fileEnd to (get eof of file tempPath) + 1
on error
set fileEnd to 0
end try
if originalContent is not "<-FileHadNoContent->" then
if appendWithNewline then
set writeData to originalContent & content & newlineCharacter
else
set writeData to originalContent & content
end if
else
if appendWithNewline then
set writeData to content & newlineCharacter
else
set writeData to content
end if
end if
end if
write writeData to file tempPath starting at fileEnd as overrideType
on error errorMessage number errorNumber
try
close access file tempPath
end try
error "Error while writing to file: " & errorMessage number errorNumber
end try
-- Close file
try
close access file tempPath
end try
-- Option: Atomically - Delete original file and rename backup file
if tempPath is not filePath then
try
tell application "System Events"
if (exists file filePath) then
-- Try to delete the original file if it exists
try
delete file filePath
on error
do shell script "rm -f " & quoted form of (POSIX path of filePath)
end try
end if
-- Rename backup file
try
set name of file tempPath to fileName
on error
try
do shell script "mv " & quoted form of (POSIX path of tempPath) & " " & quoted form of (POSIX path of filePath)
on error errorMessage number errorNumber
error "Error wile renaming backup file: " & errorMessage number errorNumber
end try
end try
end tell
end try
end if
return true
on error errorMessage number errorNumber
try
if tempPath is not filePath then
tell application "System Events" to delete file tempPath
end if
end try
set errorMessage to "writeToFile(): " & errorMessage
error errorMessage number errorNumber
return false
end try
end writeToFile
on temporaryPathWithFolderName(folderName)
(*
Generates a unique path for a file in the
current user's temporary items folder.
Takes a single argument that can be set
to the name of a subfolder or false to
create no subfolder.
*)
-- Generate pseudorandom numbers
set rand1 to (round (random number from 100 to 999)) as text
set rand2 to (round (random number from 100 to 999)) as text
set randomText to rand1 & "-" & rand2
-- Create file name
set fileName to (("AppleScriptTempFile_" & randomText) as text)
-- Get the path to the parent folder
set temporaryFolderPath to (path to temporary items folder from user domain) as text
if folderName is false then
set parentFolderPath to temporaryFolderPath
else
-- Create parent folder if necessary
set parentFolderPath to temporaryFolderPath & folderName & ":"
tell application "System Events"
if (exists folder parentFolderPath) is false then
make new folder at the end of folders of folder temporaryFolderPath with properties {name:folderName}
end if
end tell
end if
-- Make sure the file does not exist
set rNumber to 1
repeat
if rNumber is 1 then
set tempFilePath to parentFolderPath & fileName
else
set tempFilePath to parentFolderPath & fileName & "_" & (rNumber as text)
end if
tell application "System Events" to if (exists file tempFilePath) is false then exit repeat
set rNumber to rNumber + 1
end repeat
return tempFilePath
end temporaryPathWithFolderName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment