Making iPhone iTunes remote more useful
The iTunes remote is pretty slick - if only it could turn on the stereo
I have a mac mini set up as a HTPC. The music end of this was a bit neglected as it was originally set up to use a custom onscreen menu control to switch between several different iTunes libraries and then have basic control within them.
Now that we have iPhones in the house - I consolidated the decade + worth of iTunes libraries into one.
I wanted a way to control the speakers based on iTunes state.
So this little python script runs every 10 seconds, here is a quick outline of what it does.
If iTunes is playing, but the speakers are off - then adjust some volumes on the speakers, turn them on, then rewind the track.
If iTunes is paused for more than 2 minutes, and the speakers are on - and they were turned on by this autodetect system, then turn the speakers off.
The turning on and off is done by my original HTPC applescript which controls an [irTrans](http://www.irtrans.de/en/shop/usb.php) USB IR transceiver through the iRed software.
Now we just fire up the remote app on the phone, and within a few seconds the speakers are on and the music is playing.
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Preston Holmes on 2009-09-05.
Copyright (c) 2009
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import sys
import os
from appscript import *
from subprocess import Popen, call, STDOUT, PIPE
import plistlib
import time
prefs = '/Library/Preferences/com.ptone.tuneswatcher.plist'
vol_target = '/Users/Preston/Documents/HTPC/targetvolume'
def sh(cmd):
return Popen(cmd,shell=True,stdout=PIPE,stderr=PIPE).communicate()[0]
def itunes_running_():
for p in psi.process.ProcessTable().items():
try:
if "Applications/iTunes" in p.command:
return True
except:
pass
return False
def init_prefs ():
d = {'state':'unknown','last_state_change':time.time(),'auto_on':False}
plistlib.writePlist (d,prefs)
def itunes_running():
pcount = sh('ps -xa | grep -c iTunes').strip()
return int(pcount) > 3
def getSettings():
settings_file = '/Users/preston/Documents/HTPC/settings'
f = open(settings_file,'r')
flines = f.readlines()
f.close()
settingsDict = {}
for aLine in flines:
aLine = aLine.rstrip('\n')
#print aLine
if aLine != '':
apair = aLine.split(' ')
settingsDict[apair[0]] = apair[1]
return settingsDict
def read_prefs ():
if not os.path.exists (prefs):
init_prefs()
d = plistlib.readPlist (prefs)
htpc_settings = getSettings()
d.update(htpc_settings)
return d
def save_prefs(d):
plistlib.writePlist (d,prefs)
def main():
d = read_prefs()
# print d
is_running = itunes_running()
if is_running:
tunes = app("iTunes")
# print "tunes.running"
playing = (tunes.player_state() == k.playing)
# print tunes.player_state()
# print playing
if playing:
# print "tunes.playing"
if d['speakers'] == 'off':
# turn on speakers
tunes.pause()
sh ("""osascript -e 'tell application "HTPC" to power_speakers()'""")
if int(d['volume']) < 40:
open(vol_target,'w').write("50")
sh ("""osascript -e 'tell application "HTPC" to setVolume()'""")
tunes.sound_volume.set(60)
tunes.player_position.set(0)
tunes.play()
d['auto_on'] = True
save_prefs(d)
# start streamer here?
if d['state'] != 'playing':
d['state'] = 'playing'
d['last_state_change'] = time.time()
save_prefs(d)
else: # iTunes not playing
if d['state'] == 'playing':
d['state'] = 'stopped'
d['last_state_change'] = time.time()
save_prefs(d)
# check if last change was 5 minutes ago and turn off speakers
if d['auto_on'] and ((time.time() - d['last_state_change']) > 10):
if d['speakers'] == 'on':
# turn speakers off only if turned on automatically
sh ("""osascript -e 'tell application "HTPC" to power_speakers()'""")
sh ("""osascript -e 'tell application "HTPC" to calibrateVolume()'""")
d['auto_on'] = False
save_prefs(d)
# print (tunes.player_state() == k.stopped)
if __name__ == '__main__':
main()