I use TimeSnapper on windows. It’s a great little app that lets me very quickly go back in time and see what I was doing. It basically takes a screeshot every 10 seconds which I can then play as a video. This comes in very handy specially when I am coding because sometimes I will make a bad change that breaks my code and can’t really remember how I got there. Now I can go back in time and see what I was typing or doing.
I couldn’t find anything like this on the Mac, so I had to hack it together. I have never done any shell scripting on the mac before, so this could all be wrong. Any help is greatly appreciated. Here is what I did:
Create a launchd agent
Basically, I created a plist file and saved it in ~/Library/LaunchAgents and called it me.screenshot.plist – see below:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>me.screenshot</string> <key>ProgramArguments</key> <array> <string>/Users/emad/Desktop/Screenshots/timesnap.sh</string> </array> <key>StartInterval</key> <integer>10</integer> <key>RunAtLoad</key> <true/> </dict> </plist>
The file above runs my shell script every 10 seconds and it starts running as soon as I login.
Create a shell script
The shell script simply creates a folder for the day and then takes a screenshot and saves it with the date and time in the filename to make it easy to sort and find specific files. I put them in folder for every day for better organization. I also generate 2 filenames to take a screenshot of both monitors, if the second monitor is disconnected, only 1 image is created. Here is the script
#! /bin/bash foldername=""`eval date +%Y-%m-%d`"" mkdir "/Users/emad/Desktop/Screenshots/$foldername" filename1="/Users/emad/Desktop/Screenshots/$foldername/image_01_"`eval date +%Y-%m-%d-%H-%M-%S`".png" filename2="/Users/emad/Desktop/Screenshots/$foldername/image_02_"`eval date +%Y-%m-%d-%H-%M-%S`".png" screencapture -x "$filename1" "$filename2"
What’s missing? I Need Help
I would like to detect idle time and skip saving the screenshot if I am idle to save some storage space. Any ideas?
UPDATE
I figured out how to detect idle time, so my mac isn’t just snapping away all day… Here is the updated script.
#! /bin/bash
x=20
if [ $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000)) -lt $x ]; then
echo "taking screenshot"
foldername=""`eval date +%Y-%m-%d`""
mkdir "/Users/emad/Desktop/Screenshots/$foldername"
filename1="/Users/emad/Desktop/Screenshots/$foldername/image_01_"`eval date +%Y-%m-%d-%H-%M-%S`".png"
filename2="/Users/emad/Desktop/Screenshots/$foldername/image_02_"`eval date +%Y-%m-%d-%H-%M-%S`".png"
screencapture -x "$filename1" "$filename2"
else
echo "idle - no screenshot"
fi
Basically, it will not take a screenshot if the computer has been idle for 20 seconds or more.
