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.
- 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.