Skip to content

Instantly share code, notes, and snippets.

@STrRedWolf
Created January 1, 2015 14:18
Show Gist options
  • Save STrRedWolf/0ea74b7fb250c1a861bc to your computer and use it in GitHub Desktop.
Save STrRedWolf/0ea74b7fb250c1a861bc to your computer and use it in GitHub Desktop.
Using a Raspberry Pi to sync podcasts in iTunes on a Mac.

Like many people, I would like my podcasts delivered to my iPhone before I go to work. While there are many ways of doing it, Apple seems to screw things up every time they release a new MacOS X version.

With me, that version was Yosemite. They essentially broke cron.

So? Lets throw some hardware at it. While I use a Cubietruck, a Raspberry Pi will do just as good! Just make sure it's connected to the network and has ntpd keeping it's clock up to date (Rasbian does this for you).

On the Mac:

  1. Go into your System Preferences (read: Control Panel), hit the Sharing panel, and turn on "Remote Login". This starts the Mac's SSH server and gives you the command to use to access the Mac. You need the "[email protected]" part.
  2. Save the two Applescripts below into your ~/Library/Scripts folder using the Script Editor (located in Applications >> Utilities).
  3. Open up your Applications folder and create a "Local Apps" folder. I put Automator apps there.
  4. Open up Automator and create a new application called "Restart iTunes" in that new folder you created. Make it do the following steps:
  • Quit Application >> iTunes (do not save)
  • Pause >> 30 seconds
  • Launch Application >> iTunes

Your set up is now complete.

On the Pi:

  1. Install Avahi on your Pi! This lets you use the ".home" domain and will find your Mac. For Raspbian it is: sudo apt-get install avahi-daemon
  2. Make a ~/bin directory if you don't have it already: mkdir ~/bin
  3. Switch into it: cd ~/bin
  4. Make a new file with the below shell script: nano -w PiTunesSync.sh
  5. Make it executable: chmod 755 PiTunesSync.sh
  6. Test it out: ./PiTunesSync.sh You will need to say "yes" to authenticating your Mac to the Pi, and the put in your Mac's password.
  7. Putting the password in is going to get annoying really quickly. Thankfully SSH offers a solution, using a pre-shared key. Start by running: ssh-keygen
  8. Just keep hitting Return to accept the defaults. You don't even want a password on this key.
  9. Switch to the SSH config directory: cd ~/.ssh
  10. A "id_rsa.pub" file was created here. Copy it to the mac using SSH's SCP: scp id_rsa.pub [email protected]:. *The extra :. at the end of the command is needed! And once again, use your Mac's password.```
  11. Switch back "home": cd

Back on the Mac:*

  1. Open up a Terminal (I use iTerm myself, but Apple's own Terminal in Applications >> Utilities will do).
  2. Make a .ssh folder if there isn't one (it won't show in Finder): mkdir .ssh
  3. Append the new key to a "authorized_keys" file in ".ssh": cat id_rsa.pub >> .ssh/authorized_keys If it didn't exist, it will now!

And finally, back on the Pi:

  1. Give it another test: ~/bin/PiTunesSync.sh This should not prompt you anymore!
  2. Open up your crontab and append the following lines. To open it, run: crontab -e
# Pull podcasts and sync at 5:50am every day:
50 5 * * *     /home/tygris/bin/PiTunesSync.sh >> /home/tygris/pts.log 2>&1

You can adjust the "50" and "5" to when you want it to sync. It's in Millitary time, though, so 1:05pm is really 1305, and in cron is "5 13". For a better explination, man 5 crontab will give you the full details.

#!/bin/bash
# Set to your [email protected] address gotten from the Mac.
[email protected]
# Magic happens here
# Tell the Mac to sync the podcasts
ssh "$theMac" osascript Library/Scripts/UpdateAllPodcasts.scpt
# Sleep for 10 minutes
sleep 600
# Tell the Mac to restart iTunes. This has a side benefit of syncing
# any device connected to it (via USB or Wifi).
ssh "$theMac" osascript Library/Scripts/Restart\ iTunes.scpt
tell application "Finder"
activate
open application file "Restart iTunes.app" of folder "Local Apps" of folder "Applications" of startup disk
end tell
tell application "iTunes"
activate
updateAllPodcasts
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment