An automated iTunes?
So I've wanted this for a really long time but I'm not 100% sure how to do it. I basically want to be able to have iTunes play a radio station when I come home.
I was thinking of being able to have a script run every ten seconds or so to see if my iPhone has joined my wireless network, thereby opening iTunes and playing that radio station. After it starts playing, it kills the script from checking again till 12 hours later. (I'm giving an example of 12 hours since I don't want to be at home already and suddenly have music suddenly starting for no reason).
So where should I start? Am I even starting in the right place? Is there something better than this? I need some help and direction! I feel what I want is relatively simple... :-/
For a start, you could look at ControlPlane to trigger iTunes when you come home.
from http://www.controlplaneapp.com/about/ :
ControlPlane allows you to build configuration profiles, contexts in ControlPlane lingo, for your Mac based on where you are or what you are doing. ControlPlane determines where you are or what you are doing based on a number of available evidence sources and then automatically reconfigures your Mac based on your preferences. Evidence sources can include your current location, visible WiFi networks, attached USB devices, running applications and more.
It would be helpful if you could provide a bit more information about your expected functionality. It sounds like you've got a Mac in your house — does it stay on all the time?
A couple ideas spring to mind, but how you put them together is going to depend on what specifically you want to happen.
Location Checking
If you have your iPhone with you, and it automatically connects to your Wi-Fi, that's probably the best way to check if you're at home.
First, note your iPhone's Wi-Fi MAC address, which you'll find in Settings → General → About → Wi-Fi Address.
You can use the arp -a
command in Terminal (or a script) to print the Mac's list of known addresses on the local network, then use grep
to check if your iPhone's address is in there:
arp -a | grep -i IPHONE_MAC_HERE
That will print the ARP entry if one matches your iPhone's address, and print nothing if there isn't.
It's important to note that this may only work reliably if your iPhone is set to sync over Wi-Fi with your Mac. arp -a
only lists addresses it has recently communicated with - it doesn't know your iPhone is there unless it talks to your Mac. Because the Wi-Fi syncing causes the iPhone to send an identification message when it joins a network (to see if any computers it syncs with are online), it will get added to your Mac's ARP table when you come home, but it may not if you don't have Wi-Fi syncing set up.
Scripting iTunes Playback
If you don't use something like Controlplane (mentioned in kraymer's answer), AppleScript is the best way to control iTunes playback.
It's pretty straightforward:
tell application "iTunes"
play playlist "Music"
end tell
Will start playing your main library. Change "Music" to any one of your playlist names if you have a specific one you want to play. You can also add set shuffle of playlist "Music" to yes
on the line before the play
line if you want shuffle on.
You can also start playback within a shell script:
osascript -e 'tell application "iTunes" to play playlist "Music"'
Example Script
#!/bin/bash
iPhoneMAC='78:a3:e4:4C:8f:a9' # From Settings > General > About > Wi-Fi Address
# Check if the iPhone is in the ARP table
arp -a | grep -i $iPhoneMAC > /dev/null # Silence output
# Repeat check every 30 seconds, if the iPhone address isn't detected
while [[ $? == 1 ]]; do
sleep 30
arp -a | grep -i $iPhoneMAC > /dev/null
done
# Once the iPhone is detected, play music
osascript -e 'tell application "iTunes" to play playlist "Music"'
This is a pretty simplistic version, but it should be a good starting point to make something closer to what you want. When you run the script, it will check for the iPhone address (make sure to put the proper one in the script) every thirty seconds until it detects it, then it will play iTunes.
Suggested Enhancements
- It's a one-shot deal, once it detects the iPhone it plays the music and stops checking. You'll want to figure out what logic you want behind it (i.e. should it launch once per weekday, so it plays when you come home from work, or do you want it playing every time you enter the house).
- Running it via OS X's
launchd
functionality (which allows for various scheduling and repeating of scripts) may be a good idea. There are lots of resources on this around, both on Ask Different and elsewhere.- If you do it this way, you may not need the loop functionality,
launchd
could be responsible for running it at an interval to check.
- If you do it this way, you may not need the loop functionality,
- Some sort of mechanism to avoid unnecessary checking once you've actually come home and the music has played. Could just be a long sleep once it plays the music, or something a bit smarter, based on when you want it to check.
Adding on to what everyone else says, there's another option for you to consider if you don't mind leaving Bluetooth enabled on your phone (and since the newer iPhones use Bluetooth 4.0, it won't be nearly as bad with draining your battery as previous incarnations). This website details an app called Bluetooth Proximity Tasker, that lets you script various actions when your system detects your iPhone (or other Bluetooth-enabled device) with a certain signal strength.
I haven't personally tested this app, but I've tried another one that I'd found a month or two ago that successfully was able to detect my iPhone and disable the password protection on my iMac (though that app was both free and lacked the custom strength selection option).