Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

celticpride678

Guest
Original poster
Feb 15, 2009
5,486
2
Boston, MA
I am trying to setup a script where a screenshot is performed. However, every single time the script is run, the file name is the same, not allowing it to save. How can I change file name every time the script is run? Thanks

Current Script: do shell script "screencapture -i -s -tjpeg ~/Desktop/1.jpg"
 
I am trying to setup a script where a screenshot is performed. However, every single time the script is run, the file name is the same, not allowing it to save. How can I change file name every time the script is run? Thanks

Current Script: do shell script "screencapture -i -s -tjpeg ~/Desktop/1.jpg"

Any particular reason you're forcing the script to require you to interact with the screen capture?
 
You need to define a variable and then use it as your filename:

CAPTURE_FILE_NAME=`date "+%Y_%m_%d_%H%M.jpg"`
/usr/sbin/screencapture ~/Desktop/$CAPTURE_FILE_NAME

Should work. Feel free to remove /usr/sbin/...it should still work without it.
 
You need to define a variable and then use it as your filename:

CAPTURE_FILE_NAME=`date "+%Y_%m_%d_%H%M.jpg"`
/usr/sbin/screencapture ~/Desktop/$CAPTURE_FILE_NAME

Should work. Feel free to remove /usr/sbin/...it should still work without it.

I am getting a "Expected expression found but found unknown token" message on the symbol, '
 
Are you getting the error punching the lines into Terminal, or from AppleScript? It looks like AS is burping on the quote marks. I believe this works:

Code:
set littleBit to quote & "+%Y_%m_%d_%H%M.jpg" & quote

set biggerBit to "CAPTURE_FILE_NAME=`date " & littleBit & "`; /usr/sbin/screencapture ~/Desktop/$CAPTURE_FILE_NAME"

do shell script biggerBit

mt
 
Are you getting the error punching the lines into Terminal, or from AppleScript? It looks like AS is burping on the quote marks. I believe this works:

Code:
set littleBit to quote & "+%Y_%m_%d_%H%M.jpg" & quote

set biggerBit to "CAPTURE_FILE_NAME=`date " & littleBit & "`; /usr/sbin/screencapture ~/Desktop/$CAPTURE_FILE_NAME"

do shell script biggerBit

mt

It is working, but not changing the file name still. It uses the same file name, but changes the screenshot taken. I am using AppleScript
 
It is working, but not changing the file name still. It uses the same file name, but changes the screenshot taken. I am using AppleScript

What do you want the screenshots to be named?

The script should provide different names, if only because one second passes between each screen shot. But if you want a different name, we'll work on that.

Also, your original script seemed to do command-shift-4, where you can select a region of the screen for the screen shot. My script, based on Zyniker's, does a simpler, command-shift-3. Do you want a specific region or the whole screen?

mt
 
What do you want the screenshots to be named?

The script should provide different names, if only because one second passes between each screen shot. But if you want a different name, we'll work on that.

Also, your original script seemed to do command-shift-4, where you can select a region of the screen for the screen shot. My script, based on Zyniker's, does a simpler, command-shift-3. Do you want a specific region or the whole screen?

mt

I'd be OK with the date and time of the screenshot as the name. I would like a different name. I'd also like a specific region (but if needed, I can code that part). Thanks for the help so far.
 
This might be more what you're after.

Code:
set any_old_string to the text returned of (display dialog "Please enter any old string for me" default answer "" default button 2)

set littleBit to quote & any_old_string & ".jpg" & quote

set biggerBit to "/usr/sbin/screencapture ~/Desktop/" & littleBit

do shell script biggerBit

The display dialog line is merely to provide a string. You can create one in your script, maybe through a repeat loop or some other way, put quotes around it then pass it to "screencapture" with a proper path.

mt
 
This might be more what you're after.

Code:
set any_old_string to the text returned of (display dialog "Please enter any old string for me" default answer "" default button 2)

set littleBit to quote & any_old_string & ".jpg" & quote

set biggerBit to "/usr/sbin/screencapture ~/Desktop/" & littleBit

do shell script biggerBit

The display dialog line is merely to provide a string. You can create one in your script, maybe through a repeat loop or some other way, put quotes around it then pass it to "screencapture" with a proper path.

mt

THANK YOU! That is EXACTLY what I'm looking for! Thanks for all the help.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.