Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to create timed screen captures System
Someone on the MacNN Forums asked about doing timed repetitive screen captures. To set up a script that takes a screenshot of your desktop every hour, use the following Applescript. Note that "" is Apple's line continuation character; type it by hitting option-enter on your keyboard. Here's the script:
set save_location to  
(choose folder with prompt "Choose where to save screenshots")

repeat with shotcount from 1 to 100
do shell script "screencapture " &
quoted form of POSIX path of save_location
& "screen" & (shotcount as string) & ".pdf"
delay (60 * 60) -- delay one hour
end repeat
Note that you can change the loop to whatever you want. You can even make it indefinite and specify a time to quit if you want. Read up on Applescript as there are lots of cool things like this you can do.

[Editor's note: You'll have to save the script as a compiled application and then launch it, of course, in order for this to work. And as an aside, if you do something stupid, like setting the interval to "60 * .016667," (one second) you may find that you have to force quit the script to get it to stop ... don't ask how I know this!]
    •    
  • Currently 2.83 / 5
  You rated: 2 / 5 (6 votes cast)
 
[22,893 views]  

An AppleScript to create timed screen captures | 14 comments | Create New Account
Click here to return to the 'An AppleScript to create timed screen captures' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
why applescript?
Authored by: foon on Jan 15, '03 12:05:06PM

Can anyone tell me what's gained by using applescript to call the shell command, rather than just using this as a shell script? For example, I've written myself a little program that takes a screenshot every minute, converts it to jpg (using imagemagick), and saves it in my web directory:

#!/bin/sh
if [[ $# -ne 0 ]]
then
for id in `ps | grep screenie | cut -f 1 -d " "`
do
kill $id
done
cp /Library/WebServer/Documents/screenoff.jpg /Library/WebServer/Documents/screenie.jpg
exit 0
else
while (true)
do
screencapture /tmp/s.pdf
convert -page 1280x1024 -size 640x512 /tmp/s.pdf -resize 640x512 /tmp/s.jpg
cp /tmp/s.jpg /Library/WebServer/Documents/screenie.jpg
sleep 60
done
fi

Saved and appropriately chmoded, this will start taking screen shots if given no argument, and if given an argument, will kill all instances of itself (effectively shut down) and put up an "away" image.



[ Reply to This | # ]
why applescript?
Authored by: ClarkGoble on Jan 15, '03 04:01:35PM

You can double clip an icon. I prefer the shell myself, but a lot of people don't.



[ Reply to This | # ]
why applescript?
Authored by: FACEMILK on Jan 15, '03 09:04:09PM

better yet:

write the shell script to just take the picture,
let cron handle the timing and repetition



[ Reply to This | # ]
Delay one second...
Authored by: thefinite on Jan 15, '03 12:15:30PM
In regard to the Editor's Note, why in the world would you do this:
delay (60 * .016667)

when you can do this:
delay 1
?

[ Reply to This | # ]
Delay one second...
Authored by: robg on Jan 15, '03 12:33:00PM

As I said, "don't ask." :-)

Odd things happen early in the morning, such as failing to think prior to typing...

-rob.



[ Reply to This | # ]
did we have this?
Authored by: VEGx on Jan 15, '03 02:14:50PM

Some 3 months ago we had this post... or something very similar how to take timed screen shots. I think it was a shell script. So, what's the big news? that you can do "do shell script" in the AppleScript??? Um, I thought everyone knew that already...



[ Reply to This | # ]
Yes, kind of
Authored by: robg on Jan 15, '03 02:27:36PM
It's very similar to this hint, which is a Perl script to do screen captures (and yes, I know there's an AppleScript linked in the comments). There are, however, some people who would prefer not to use the Terminal and/or Perl, and who might want something that can be launched and quit from the GUI - and this AppleScript fits the bill. I felt that this script merited its own hint for those that would rather not use the Terminal and yet were unable to create their own AppleScript wrapper. Hence, we have two similar yet different solutions to one problem. -rob.

[ Reply to This | # ]
This will be more efficient...
Authored by: cougar718 on Jan 15, '03 05:59:00PM

The below script is a slight modification to the original. This uses the application's idle handler instead of keeping the application in a repeat loop. The "Delay" call tends to use more of the CPU than the idle handler. To use this version, save the script as an application with the "stay open" option checked. The property "shot_interval" can be set to any number, default is 3600 seconds which is 1 hour. This version also rules out the editor's warning about too small of an interval. Good luck and any questions, email me cougar718@comcast.net. Thanks.

property save_location : ""
property shot_count : 1
property shot_interval : 3600 -- in seconds

on run {}
set save_location to
(choose folder with prompt "Choose where to save screenshots")
set shot_count to 1
end run

on idle
do shell script "screencapture " &
quoted form of POSIX path of save_location
& "screen" & (shot_count as string) & ".pdf"
set shot_count to shot_count + 1
return shot_interval
end idle



[ Reply to This | # ]
This will be more efficient...
Authored by: diZZy on Jan 16, '03 11:24:29AM

And how about changing the " "screen" & (shot_count as string) " bit to something a bit more usefull (for some!) like the current date and time?
And maybe a 10 seconds delay before the first capture!!!



[ Reply to This | # ]
This will be more efficient...
Authored by: cougar718 on Jan 16, '03 02:22:53PM

Ok, I was not trying to completely modify the original, but your wish is my command bro. This modified script features a 10 second delay before the first capture and names the Screenshot

ScreenCap - Thursday, January 16, 2003 14/21/45.pdf

Note: Because of the way the shell handles semi-colons, the time is formatted with / instead of :

Use this new script in place of the old one. Have fun and good luck!

property first_run : true
property save_location : ""
property shot_interval : 3600 -- in seconds

on run {}
set first_run to true
set save_location to
(choose folder with prompt "Choose where to save screenshots")
end run

on idle
if (first_run) then
set first_run to false
return 10 -- 10 second delay before first picture
else
do shell script "screencapture " &
quoted form of (POSIX path of save_location
& "ScreenCap - " & ((current date) as string) & ".pdf")
return shot_interval
end if
end idle



[ Reply to This | # ]
This will be more efficient... Brilliant is the word!
Authored by: diZZy on Jan 16, '03 03:05:24PM

Ta very much mate!
I did toy a bit with your original script but I couldn't quite work it out properly... I was using >>> ((the current date) as string) <<<
Thanks a bunch



[ Reply to This | # ]
This will be more efficient... Brilliant is the word!
Authored by: cougar718 on Jan 16, '03 10:56:52PM

You are welcome bro. Laterz

Rick



[ Reply to This | # ]
Do you want a clickable shell script?
Authored by: Anonymous on Jan 16, '03 11:04:28AM

Some people have pointed out that you don't need to wrap the shell commands in AppleScript, but others have replied that this gives you an icon that you can double-click in the GUI.

If you want a shell script to be clickable, put ".command" at the end of the name and make sure the file's permissions allow it to be executable. This has worked since the Public Beta.



[ Reply to This | # ]
Do you want a clickable shell script?
Authored by: millz on Nov 17, '03 09:09:03PM

but a terminal window stays open while the script is still running, right?

If I put
/Applications/Mail.app/Contents/MacOS/Mail &
in a file and call it run.command, I can double-click it to run it, but the terminal window stays open until I close mail. Is there a way around that?



[ Reply to This | # ]