Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Prompt for filename after or before taken a screenshot

Hi there, I like the screenshot function of Mac OS X, I have configured the default path, but I would love to be able to set the filename of every screenshot I take after (or even before) I press Cmd+Shift+4 or whatever. I'm trying to do it with automator but I cannot set the file name and send it as a variable properly.


Could some body help me to do this in automator or apple script?


Thanks in advance,

Alex

MacBook Pro with Retina display

Posted on Oct 16, 2015 3:41 PM

Reply
12 replies

Oct 17, 2015 3:09 AM in response to a_fanjul

I see two options for you here.


One is a folder action on wherever you save your screenshots. The folder action will trigger when a new screenshot is saved and you can catch that, ask the user for a file name and then rename the file.


User uploaded file

The other option is to have a script ask for a file name and then have the script invoke /usr/sbin/screencapture (the shell command for taking a screenshot), passing in the desired filename. Create an AppleScript application that does:


set f to text returned of (display dialog "Save screen capture as:" default answer "screenshot.jpg")

do shell script "/usr/sbin/screencapture ~/Desktop/" & f


Then launch your application to take your named screenshot. You could even create this as a service and use System Preferences to assign a keystroke.

Oct 17, 2015 3:08 AM in response to a_fanjul

Hello


The following AppleScript script will ask for file name and save screenshot in the file.



set f to (choose file name default location (path to desktop) ¬ default name (do shell script "date +'Screen_%Y%m%d_%H%M%S.png'"))'s POSIX path do shell script "screencapture -i " & f's quoted form




You might save this as an Automator service and assign keyboard shortcut e.g., Command + Shfit + Option + 4.


Regards,

H

Oct 17, 2015 3:14 AM in response to Hiroto

OMG! Both Camelot and Hiroto saved my life with that simple script. I choosed the Hiroto's one because of the filename dialog with default name that works so great! To complete the script I just added the -tjpg and -P options to save as jpg and preview it after screenshot is taken. This is my final script:


set f to (choose file namedefault location ¬

"/Users/user/Pictures/Screenshots" default name (do shell script "date +'Screenshot-%Y%m%d-%H%M%S.jpg'"))'s POSIX path

do shell script "screencapture -i -P -tjpg " & f'squoted form


Thanks again for your quick support!

Oct 17, 2015 3:36 AM in response to Camelot

Thanks for your support, your solution has one advantage over the Hiroto's one: you can obviate the extension part, I ended with this:


set f to text returned of (display dialog "Save screen capture as:" default answer "Screenshot")

do shell script "/usr/sbin/screencapture -i -tjpg -P ~/Pictures/Screenshots/" & f'squoted form & ".jpg"

Regards,

Alex

Feb 6, 2016 11:20 PM in response to mizusajt

Hello


You're quite welcome and glad to hear it serves you well. 🙂


By the way, I noticed my original script changes the focus when run as an Automator service because Automator Runner.app takes the focus (at least under OS X 10.6.8).


So here's a revised script which should prevent the original focus from changing inappropriately.



set n to do shell script "date +'Screen_%Y%m%d_%H%M%S.png'" tell application "SystemUIServer" set f to (choose file name default location (path to desktop) default name n)'s POSIX path end tell do shell script "screencapture -i " & f's quoted form




Tested under OS X 10.6.8.


Regards,

H

Feb 16, 2016 6:53 PM in response to Hiroto

Thanks Hiroto, that was very helpful.

I'll add for anyone reading this that there is a tricky way to name the file AFTER taking the screenshot, if that is what you desire. I myself have difficulty thinking of names before i take the actual picture, so here's how i do it:

try do shell script "screencapture -i /screenshots/temp.png" tell application "SystemUIServer" set f to (choose file name default location (POSIX path of "/screenshots") ¬ default name (do shell script "date +'Screen_%Y%m%d_%H%M%S.png'"))'s POSIX path end tell if f does not end with ".png" then set f to f & ".png" do shell script "mv /screenshots/temp.png " & f's quoted form end if on error errMsg number errNo error number -128 end try

I, of course, use different folder locations and i have an extra step to check if i want to save it as a jpeg using sips, but i don't want to confuse anyone else who might be seeking an answer.


This also works with tags, which i was surprised by, and it was tested under 10.11.3, so YMMV

Feb 16, 2016 11:42 PM in response to mizusajt

Hello


With regard to supporting other formats, here's a revised script to determine image format by name extension of specified file name.



(* Image file type is determined by name extension of specified file name. Supported formats (name extensions) include but are not limited to: PNG (png) JPEG (jpg, jpeg) JPEG2000 (jg2) TIFF (tiff, tif) PDF (pdf) Unsupported format results in error reported in dialogue. File name without name extension results in screencapture in default format (without name extension). *) set n to do shell script "date +'Screen_%Y%m%d_%H%M%S.png'" tell application "SystemUIServer" set f to (choose file name default location (path to desktop) default name n)'s POSIX path end tell try do shell script "f=" & f's quoted form & " [[ $f =~ ^(.*/)?[^/]+\\.([[:alnum:]]+)$ ]] && t=\"-t ${BASH_REMATCH[2]}\" || t= screencapture -i $t \"$f\" || { r=$?; echo $t >&2 ; exit $r; }" on error errs number errn display dialog errs end try




Yes, it is getting complicated... So this far and not further. 😉


All the best,

H

Feb 17, 2016 4:15 PM in response to Hiroto

Hiroto,


That's one way of doing it. However, since i like naming the files after, I found it simplest to just use the shell command "sips" to convert the image. This is my full script:

try do shell script "screencapture -i /screenshots/temp.png" tell application "SystemUIServer" set f to (choose file name default location (POSIX path of "/screenshots") ¬ default name (do shell script "date +'Screen_%Y%m%d_%H%M%S'"))'s POSIX path end tell if f ends with ".jpg" then do shell script "sips -s format jpeg /screenshots/temp.png --out /screenshots/temp.jpg" do shell script "mv /screenshots/temp.jpg " & f's quoted form else if f does not end with ".png" then set f to f & ".png" do shell script "mv /screenshots/temp.png " & f's quoted form end if on error errMsg number errNo error number -128 end try

You could change the script to detect any image file you want and just check each file type you want to support with if statements and do the appropriate conversion with sips. This way does not give you much control over jpeg compression but i personally saw no need for that.

Sep 7, 2016 6:26 PM in response to afound

Hey afound,


I was able to figure out what they were saying in order to begin tinkering with it.


Where to set up the script with Automator:

-Open Automator (Looks like a robot with a bazooka).

-Select Service

-Make sure Library is highlighted for this part; In the search bar enter "Run Applescript"

-Drag that over to the right, the big grey box area

-You will then replace "(* Your script goes here *)" with whoever's script from above that you like (Or learn the language and create something new! 🙂). I preferred a_fanjul's response since I can understand it; I like how it asks me to save wherever while renaming my file and opening it in preview afterwards:


set f to (choose file namedefault location ¬

"/Users/user/Pictures/Screenshots" default name (do shell script "date +'Screenshot-%Y%m%d-%H%M%S.jpg'"))'s POSIX path

do shell script "screencapture -i -P -tjpg " & f'squoted form

-Forewarning: Make sure your file directory is correct

- Clicking the big black play button in the same box lets us run the script to test it out

-Save the file.

How to set it up to as a keyboard shortcut:

-Open System Preferences->Keyboard->Shortcuts->Services

-Under General you shall see your new file you just created with no shortcut combination

-Click on "none" right next to the file name and set up your favourite keyboard combination

-I used my Macs original shortcut for this so I had to turn it off under Preferences->Keyboard->Shortcuts->Screen Shots


Close everything and your combo should work!

We can now run that script using our shortcut.


🙂

Prompt for filename after or before taken a screenshot

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.