Automated screenshot image sequences on a mac

Create an automator task to capture screenshots from your mac for use in video documentation / animated GIFs

applescript,automator,animated gif

I recently worked on a video project which required taking a series of nearly ten thousand frames of 1920x1080 still images from a program called Golly - which simulates Conway's Game of Life. While the program has a built-in script which can create animated gifs, these are very slow to work with in After Effects, and very slow to create for complex patterns. In addition, they could only be created for the 1:1 scale in Golly, and I wanted to really focus on the macro details of cellular automata. The best way to get what I needed was to take screenshots, but taking 10,000 screenshots and advancing to the next frame of an animation was carpal tunnel syndrome waiting to happen, so I decided to write an applescript to handle the workflow.

I felt like there might be enough situations people would encounter in which this would be useful that I would post it here. The following is useful not just for screenshots, but for any repetitive workflow you might encounter on a mac.

The Applescript Editor is included in Mac by default. Open it up, and paste in this script:



activate application "Golly"
delay 5
repeat 1000 times
    tell application "System Events"
        key code 20 using {shift down, command down} -- shift-command-three
        delay 0.4
        key code 49
        delay 0.1
    end tell
end repeat

In this script, we're telling Mac:

  • to open our application (in my case, Golly)
  • wait for 5 seconds (this gives you enough time to get your application in order before the screenshotting starts
  • to start a loop which will repeat 1000 times. Everything inside this loop will be repeated, everything outside will not
  • to enter the keycode for a screenshot (Command-Shift-3)
  • to wait .4 seconds (you can play with this number - I originally had it at .1 seconds but I noticed my mac was really straining to keep up
  • to hit the spacebar (key code 49)
  • to wait another .1 seconds (giving the application time to go to the next frame and update the display)
  • to repeat the process of screenshotting the next frame

The key codes for mac can be found here, or you can download this application for mac which will tell you the keycode for any key combination you press.

You've probably got the screenshots all going to your desktop, which isn't ideal for most people. What I did was create a folder in my project directory called "screenshots" - and then in terminal, you can run this command to change it:

defaults write com.apple.screencapture location /Users/[yourname]/[yourproject]/screenshots/

where the path would obviously be the path on your harddrive to the screenshot folder you created. Then just run:

killall SystemUIServer

to update your system with the new location. Now all screenshots will go to this folder. You can always change it back to your desktop by running the command above with the path to your desktop.

The last thing I needed to do was to rename all of the screenshot files into an image sequence that aftereffects would recognize. This is simple and quick to do with automator, which is also built into OSX. Open up the application. It will prompt you for a type for your document. Choose the gear icon ("service"). In the righthand panel, it will give you a box with a couple of dropdowns. For the first, "Service receives: " choose "Files or Folders" and for the second, "in: " choose "Finder".

Then, from the menu on the left, find the "Files and Folders" Finder Icon in the list, and drag the "Get Specified Finder Items" over to the right. Then drag "Rename Finder Items" below that. It will prompt you as to whether you want to create a copy of the files - so unless you want 1000 more screenshot files on your hard drive, I'd click "Don't Add".

In the second box you dragged over, the first dropdown will say "Add Date/Time" - well, screenshots already have that by default in Mac. All you want is a recurring prefix, followed by a sequential number. So choose "Make Sequential". Then for the next radio button - "Add number to: " - choose "new name" and give it a prefix. For me it was "golly". I then also changed the last field - "Make all numbers: " - to "4" so that the numbers would be 4 digits long (since I have > 1000 images. So the first image would be called golly-0001.png. The last, golly-1000.png.

You're now ready to rename files. Just select them all from your screenshots folder, drag them into the "Get Specified Finder Items" window, and click the "Run" button in the upper right. In a few seconds, all of your images will be renamed, and you can import them into After Effects with the import command.

You're now set up to rename your screenshots to an image sequence after effects will recognize.