I have many legacy applescripts controlling certain things in Indigo that either at the time didn't have Python counterparts or at the time I didn't have the Python skills for. Now that I'm decent with Python I've been slowly going through and eliminating any applescripts I can for future-proofing (since I can't use device id's in applescript) and for more powerful scripting. In my quest to do this I stumbled on this article showing how to easily call an applescript in Python and even pass Python variables mid-applescript.
Essentially you create a Python script that you will store in your Python "site-packages" folder and import it to any script just like any other Python module. Here is the script:
- Code: Select all
#!/usr/bin/python
import subprocess
def asrun(ascript):
"Run the given AppleScript and return the standard output and error."
osa = subprocess.Popen(['osascript', '-'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
return osa.communicate(ascript)[0]
def asquote(astr):
"Return the AppleScript equivalent of the given string."
astr = astr.replace('"', '" & quote & "')
return '"{0}"'.format(astr)
Note: The above script is modified from the article for compatibility with Python 2.6
In addition to this, I've now created two other scripts which import this one for control of iTunes and Airfoil through Python. Here's an example where you can pass parameters into your Applescript:
- Code: Select all
def playPlaylist(my_playlist):
ascript = '''
tell application "iTunes"
play playlist {0}
end tell
'''.format(asquote(my_playlist))
print ascript
asrun(ascript)
This wasn't possible before Indigo 6.1 because the str.format() function only works with Python 2.6 and above. If you have more parameters to pass you just add more {}'s with incrementing numbers in them and pass what you want in the format function using asquote (from our applescript.py). ''' {0} {1}'''.format(asquote(someVar1), asquote(someVar2))
With the ability to import these basic scripts as modules I can now easily make Python scripts like this:
- Code: Select all
import itunes, airfoil
thisPlaylist = indigo.variables[somevarid].value
airfoil.setSource('iTunes')
airfoil.setVol('string with speaker name', 0)
airfoil.connect('string with speaker name')
itunes.setVol('80')
itunes.stop()
itunes.shuffleOld(thisPlaylist)
itunes.playPlaylist(thisPlaylist)
airfoil.fadeUp('string with speaker name', .33)
Here are the scripts if anyone wants to use them. Note that iTunes changed how it lets you shuffle after version 10, so if you have 10 or older use the shuffleOld() function and if you have 11 or newer use the shuffleOn() and shuffleOff() functions.
Available functions:
itunes.py
playpause()
play()
pause()
stop()
next()
prev()
back() # (will return to beginning of track before going to prev track)
shuffleOn() # (iTunes 11+)
shuffleOff() # (iTunes 11+)
shuffleOld(playlistNameString) # (iTunes 10 and below)
playPlaylist(playlistNameString)
setVol(volumeAsString)
airfoil.py
setSource(sourceString)
disconnectAll()
connect(speakerNameString)
disconnect(speakerNameString)
setVol(speakerNameString, volume_INT)
getVol(speakerNameString)
fadeUp(speakerNameString, volumeToFadeUpTo_INT)
fadeDown(speakerNameString, volumeToFadeDownTo_INT)
Happy to help anyone get started with this if you have questions!