On occasion, I too have graphic image data from a screen shot on the clipboard and have used a very stripped down version of the example AppleScript code shown further below to write it to a disk file. However, I've rewritten it so all you have to do is set the value for two variables, read the comments in the code, then save the script using Script Editor. Then you can trigger it from a keyboard shortcut using an app like FastScripts or similar.
This way you do not need to give every application that has focus separate permission to run it, like you would if you used an Automator Service/QuickAction set to any application. (Or you could just set it to one application, e.g. Finder and then only trigger it from there.)
FastScripts is free for up to 10 keyboard shortcuts. Note that I'm not affiliated with the developer of this product, just a satisfied user.
Save the following example AppleScript code as a script named e.g. Write Clipboard Graphic Image Data to File, then assign it a keyboard shortcut in FastScripts, I used ⇧⌘6, and start using it. Note that you can also use it in a Run Shell Script action in an Automator Service/QuickAction if you want to, but FastScripts is the way to go.
If you want to hard code a location, instead of a choose folder dialog, just let me know and I can change the code.
What this example AppleScript code does:
- Checks the clipboard to see if it contains graphic image data and if not informs the user.
- If the clipboard does contain graphic image data a choose folder dialog opens.
- Choose the location to save the graphic image data from the clipboard to file, and it does, unless you do not have write permission to that location, you are warned and then choose again.
- The naming convention is, e.g.: Screen Shot 2020-05-21 at 9.34.29 PM.png
Example AppleScript code:
-- #
-- ### Write Clipboard Graphic Image Data to File ###
-- #
-- # This example AppleScript code will write graphic image data in its normal form on the clipboard to disk file.
-- # All you need to do is set the value of the fileExtension variable to the type of graphic image file you want.
-- # Then when the script runs, a choose folder dialog box will appear, you select a location and click the Choose button.
-- # The graphic image data in its normal form on the clipboard is written to disk file with the following naming convention:
-- # 'Screen Shot 2020-05-21 at 9.34.29 PM.fileExtension' e.g. 'Screen Shot 2020-05-21 at 9.34.29 PM.png'
-- # Set the value of the fileExtension variable
-- # to the type of graphic image file you want.
-- #
-- # Valid values are: png, jpg, tiff, gif
set fileExtension to "png"
-- # The defaultChooseFolderLocation is the default
-- # location the choose folder dialog opens to.
-- # It can be any properly formed AppleScript
-- # 'path to (folder)', or use a fully qualified POSIX path.
set defaultChooseFolderLocation to path to (pictures folder)
-- ########################################
-- ## Do Not Edit Below Unless You Know What You Are Doing. ##
-- ########################################
-- # With the exception of the file naming convention, the rest
-- # of the code is tokenized and should not need to be edited.
-- # Obviously though, feel free to edit it if you so choose.
if fileExtension is equal to "png" then
set imageType to «class PNGf»
else if fileExtension is equal to "jpg" then
set imageType to JPEG picture
else if fileExtension is equal to "tiff" then
set imageType to TIFF picture
else if fileExtension is equal to "gif" then
set imageType to GIF picture
else
display dialog "The file extension set is not recognized!" buttons {"OK"} ¬
default button 1 with title "Write Clipboard Graphic Image Data to File" with icon stop
return
end if
if ((clipboard info) as string) contains imageType then
my chooseLocationAndWriteFile(imageType, fileExtension, defaultChooseFolderLocation)
else
display dialog "The clipboard does not contain graphic image data!" buttons {"OK"} ¬
default button 1 with title "Write Clipboard Graphic Image Data to File" with icon caution
end if
-- ## Handler(s) ##
on chooseLocationAndWriteFile(imageType, fileExtension, defaultChooseFolderLocation)
set cbImage to (the clipboard as imageType)
set fileName to do shell script ¬
"echo $(date '+Screen Shot %Y-%m-%d% at %l.%M.%S %p." & fileExtension & "')"
set saveToLocation to POSIX path of (choose folder with prompt ¬
"Choose the Folder to save the clipboard image to..." default location ¬
defaultChooseFolderLocation)
set imageFile to saveToLocation & fileName
my writeToFile(cbImage, imageFile, true, imageType, fileExtension)
end chooseLocationAndWriteFile
on writeToFile(theData, theFile, overwriteExistingContent, imageType, fileExtension)
try
set theOpenedFile to open for access POSIX file theFile with write permission
if overwriteExistingContent is true then set eof of theOpenedFile to 0
write theData to theOpenedFile starting at eof
close access theOpenedFile
on error eStr number eNum
try
close access file theFile
end try
if eNum is equal to -5000 then
display dialog ¬
"You do not have write permission to this location!" buttons {"OK"} ¬
default button 1 with title "File I/O Error..." with icon stop
my chooseLocationAndWriteFile(imageType, fileExtension, defaultChooseFolderLocation)
else
display dialog eStr & " number " & eNum ¬
buttons {"OK"} default button 1 ¬
with title "File I/O Error..." with icon stop
end if
return
end try
end writeToFile
Note: The example AppleScript code is just that and does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5
, with the value of the delay set appropriately.
My screenshots go directly to my clipboard, which works for me most of the time, so I'm not trying to change that behavior.
How have you set this up? By default screenshots in macOS are saved on Desktop.