Automating Rick Rolls with launchd

—Thursday, May 01 2008

Lets face it: Your coworkers can be a real pain in the ass sometimes. What better way
to remind them of this than automated Rick rolls? They’re likely to never know what hit them.

What is launchd?

According to the launchd guide on Apple’s website:

The launchd daemon takes over many tasks from cron, xinetd, mach_init, and init,
which are UNIX programs that traditionally have handled system initialization, called
systems scripts, run startup items, and generally prepared the system for the user

launchd stores tasks in plist format. You can find your agents in ~/Library/LaunchAgents.
Here is a typical launcd file that will open iTunes every 60 seconds:

  <?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>com.alternateidea.article.test</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/open</string>
        <string>-b</string>
  	<string>com.apple.iTunes</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
    <key>StartInterval</key>
    <integer>60</integer>
  </dict>
  </plist>

Obviously this would be seriously annoying, but for the purpose of this demonstration,
save this file as “com.alternateidea.article.test” in your ~/Library/LaunchAgents folder.

This script won’t run automatically, so we need to load it up first using the launchctl
program:

launchctl load ~/Library/LaunchAgents/com.alternateidea.article.test.plist

Our agent is loaded up now. After 60 seconds, you should see iTunes.app open. Pretty nice, eh?

Ok, so we don’t want to keep running this agent. Lets unload it:

launchctl unload ~/Library/LaunchAgents/com.alternateidea.article.test.plist

Never gonna give you up. Never gonna automate your Mac.

So, here’s the trick: Wait until your coworker goes to lunch or you know they’ll be out of the
office for 10 minutes or so. Then, let the mischief begin.

The first thing we need to do is create our agent:

  <?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>com.youtube.rickroll</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/open</string>
        <string>-b</string>
  			<string>com.apple.Safari</string>
  			<string>http://www.youtube.com/watch?v=eBGIQ7ZuuiU</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
    <key>StartInterval</key>
    <integer>1800</integer>
  </dict>
  </plist>

Everything should be pretty obvious in this plist. The StartInterval is in seconds. I
want to be an asshole every 30 minutes (1800) seconds. If you want to be a bigger asshole
(or Son of a Bitch) you could set this to run in
shorter intervals.

Now, we need to save this and load it up on your coworkers computer. Save this
to ~/Library/USERNAME/LaunchAgents/com.youtube.rickroll. And load it up:


  launchctl load ~/Library/LaunchAgents/com.youtube.rickroll.plist

If you want to test and make sure everything is in working order, you can run:

launchctl start com.youtube.rickroll

Question: Did you just Rick Roll yourself?

Now, if your test ran successfully slip back to your desk and wait in anticipation for that
awesome intro.

Disclaimer: If you destroy your coworkers computer or end up scrapping in the street, I’m not responsible.