Help to convert iTunes AppleScript to Python

Posted on
Thu Oct 18, 2018 7:14 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Help to convert iTunes AppleScript to Python

I'm rewriting some scripts an need to call iTunes in Python. All I want to do is start the local NPR station when my radio wake alarm goes off. I tried the script and modifications below but it throws an error. Any thoughts? Obviously it works for others.

Code: Select all
import applescript
script_source = 'tell application "iTunes" to play (first track of playlist "Radio" whose name is "Sky Radio 101 FM")'
script = applescript.AppleScript(source=script_source)
reply = script.run()


I tried this, which also doesn't work.
Code: Select all
import subprocess
command = ['osasript','-e', 'tell application "iTunes" to play first track of playlist "Music" whose name is "iTunes PList 13 NPR"']
p = subprocess.check_output(command)


I'm not sure if "Radio" is one of the lists... Do I need to use the list "music playlists" or whatever?

Nor did this.
Code: Select all
import applescript
script source = 'tell application "iTunes" to play playlist "iTunes PList 13 NPR"'
script = applescript.AppleScript(source=script_source)
reply = script.run



OTOH, this works fine to directly address iTunes and speak the weather:

Code: Select all
itunesId = "com.perceptiveautomation.indigoplugin.itunes"
itunesPlugin = indigo.server.getPlugin(itunesId)

if itunesPlugin.isEnabled():
   myWeatherStation = indigo.devices[976449363]
   outsideTemp = myWeatherStation.states['temperatureF']
   currentCondition = indigo.variables["WeatherNOAA_Day1Period1TextForecast"]  #speaking the entire weather forecast
itunesPlugin.executeAction("pause", deviceId=772048342)


In this post there are several commands for addressing iTunes:
https://wiki.indigodomo.com/doku.php?id ... g_tutorial

Is there a set of direct commands that can be put in a python script to call simple functions like "play playlist"?

Thanks!

Ham

Posted on
Fri Oct 19, 2018 9:52 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Help to convert iTunes AppleScript to Python

[MODERATOR NOTE] split this post into a separate topic since it's not directly related to the post it was under before.

Code: Select all
tell application "iTunes" to play (first track of playlist "Radio" whose name is "Sky Radio 101 FM")


That AppleScript doesn't appear to run in Script Editor against iTunes 12.8 - it generates this error:

Code: Select all
error "iTunes got an error: Can’t make some data into the expected type." number -1700 to item


So, step #1 of your conversion is that you need to verify that the AppleScript that you're trying to run actually works.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Oct 19, 2018 10:40 am
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Help to convert iTunes AppleScript to Python

Jay, thanks very much. I will certainly try working with that when I get a moment.

My other question was whether there are direct python commands to address iTunes. In your tutoriaI referenced above there are several commands which appear to be able to direct actions in iTunes from Python. Is there another listing detailing more of those? Or could you post a listing of those commands somewhere? That would make life a lot simpler.

Posted on
Fri Oct 19, 2018 11:00 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Help to convert iTunes AppleScript to Python

hamw wrote:
My other question was whether there are direct python commands to address iTunes. In your tutoriaI referenced above there are several commands which appear to be able to direct actions in iTunes from Python. Is there another listing detailing more of those? Or could you post a listing of those commands somewhere? That would make life a lot simpler.


Sure - it's in the Scripting Support section of the iTunes plugin documentation.

Note: this isn't a general/generic iTunes control API - it's just the scripting API for the actions presented in the iTunes plugin (which is a subset of what iTunes can do).

The 3rd party iTunes Local Control plugin presents a lot of actions that the built-in iTunes plugin doesn't support so that plugin may do what you want (as long as it's talking to iTunes on the same Mac as your Indigo Server). I don't know if that plugin has documented scripting actions, but it's likely that you could script it's actions as well if the necessary information was documented.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Oct 20, 2018 11:03 am
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Help to convert iTunes AppleScript to Python

I may have been a bit unclear in my prior post. the initial example is one that apparently works in this thread https://forums.indigodomo.com/viewtopic.php?f=252&t=20486&hilit=import+applescript+itunes, in which my original note was posted. However, trying to adapt it does not work. More to the point, typical applescript commands don't appear to work in my python script in the following formats.

Here's the usual applescript, which continues to work:

Code: Select all
tell application "iTunes" to play playlist "iTunes PList 13 NPR"



Same command:

Code: Select all
import subprocess
command = ['osasript','-e', 'tell application "iTunes" to play playlist "iTunes PList 13 NPR"']
p = subprocess.check_output(command)


This throws an error on the last line.

Code: Select all
      Script Error                    embedded script: [Errno 2] No such file or directory
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 4, at top level
     File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 566, in check_output
       process = Popen(stdout=PIPE, *popenargs, **kwargs)
     File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
       errread, errwrite)
     File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
       raise child_exception
OSError: [Errno 2] No such file or directory



A different call from Jay's post here:https://forums.indigodomo.com/viewtopic.php?f=252&t=20486&hilit=import+applescript+itunes#p158375

Code: Select all
import applescript
script source = 'tell application "iTunes" to play playlist "iTunes PList 13 NPR"'
script = applescript.AppleScript(source=script_source)
reply = script.run


throws this error:

Code: Select all
 Script Error                    embedded script: invalid syntax
   Script Error                    around line 12 - "script source = 'tell application "iTunes" to play playlist "iTunes PList 13 NPR"'"


I realize the iTunes local control plugin does all this, but I need to do it within a complete script that does other things as well. AFAIK it is not possible to call that plugin from a script.

My apologies for dragging out something that is most likely very simple...

Posted on
Sat Oct 20, 2018 11:41 am
FlyingDiver online
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Help to convert iTunes AppleScript to Python

hamw wrote:
Code: Select all
import subprocess
command = ['osasript','-e', 'tell application "iTunes" to play playlist "iTunes PList 13 NPR"']
p = subprocess.check_output(command)


This throws an error on the last line.

Code: Select all
      Script Error                    embedded script: [Errno 2] No such file or directory
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 4, at top level
     File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 566, in check_output
       process = Popen(stdout=PIPE, *popenargs, **kwargs)
     File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
       errread, errwrite)
     File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
       raise child_exception
OSError: [Errno 2] No such file or directory



You have a typo in your code. It's "osascript", not "osaript". Also, to be safe, you should use the full path name.

Code: Select all
import subprocess
command = ['/usr/bin/osascript','-e', 'tell application "iTunes" to play playlist "iTunes PList 13 NPR"']
p = subprocess.check_output(command)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sat Oct 20, 2018 11:43 am
FlyingDiver online
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Help to convert iTunes AppleScript to Python

hamw wrote:
Code: Select all
import applescript
script source = 'tell application "iTunes" to play playlist "iTunes PList 13 NPR"'
script = applescript.AppleScript(source=script_source)
reply = script.run


throws this error:

Code: Select all
 Script Error                    embedded script: invalid syntax
   Script Error                    around line 12 - "script source = 'tell application "iTunes" to play playlist "iTunes PList 13 NPR"'"



In this one, you left out an underscore character.

Code: Select all
import applescript
script_source = 'tell application "iTunes" to play playlist "iTunes PList 13 NPR"'
script = applescript.AppleScript(source=script_source)
reply = script.run


or, more succinctly:
Code: Select all
import applescript
script = applescript.AppleScript(source='tell application "iTunes" to play playlist "iTunes PList 13 NPR"')
reply = script.run

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Oct 21, 2018 9:39 am
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Help to convert iTunes AppleScript to Python

Thanks to everyone who pitched with ideas; much appreciated. However I was still stuck as this:

Code: Select all
import applescript
script = applescript.AppleScript(source='tell application "iTunes" to play playlist "iTunes PList 13 NPR"')
reply = script.run

did not work, even corrected for the underscore. However, after digging around I ran across this post, referencing the PyObjC bridge: https://stackoverflow.com/questions/16065162/calling-applescript-from-python-without-using-osascript-or-appscript, which does work, but sometimes throws a timeout error.

Code: Select all
from Foundation import *
s = NSAppleScript.alloc().initWithSource_('tell application "iTunes" to play playlist "iTunes PList 13 NPR"')
s.executeAndReturnError_(None)


"play playlist", "pause", "play" all seem to work.

Maybe that can be added to the list of options for integrating AS. One final note is that I tried all of the different scripts in the embedded script executor box in a test action group. Perhaps they work in an external script better?

Posted on
Tue Oct 23, 2018 8:33 am
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Help to convert iTunes AppleScript to Python

hamw wrote:
Code: Select all
import applescript
script = applescript.AppleScript(source='tell application "iTunes" to play playlist "iTunes PList 13 NPR"')
reply = script.run



You forgot the parenthesis after run:

Code: Select all
reply = script.run()


run is a function, not a property.

hamw wrote:
Code: Select all
from Foundation import *
s = NSAppleScript.alloc().initWithSource_('tell application "iTunes" to play playlist "iTunes PList 13 NPR"')
s.executeAndReturnError_(None)



In fact, this is almost exactly what the py-applescript library uses.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Tue Oct 23, 2018 10:45 am
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Help to convert iTunes AppleScript to Python

Thanks, Jay. Is there a particular script editor that my pick up some of these obvious errors? It’s My impression that we have been fortunate with the AppleScript editor, which is really helpful and compiling and showing obvious syntax errors and mistakes. Anything like that out there for python?

Posted on
Tue Oct 23, 2018 1:28 pm
jay (support) offline
Site Admin
User avatar
Posts: 18200
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Help to convert iTunes AppleScript to Python

There are quite a few options for text editors that understand Python: BBEdit, Sublime Text are two that are used around here. PyCharm is also great for plugin development, but for script development it's somewhat overkill.

Unfortunately, neither would have caught the problem you had above because both are syntactically correct. Without the parenthesis is the function object (which you probably won't use, at least until you get into advanced Python) and the other is the function call itself.

As you gain experience with Python, it will become more obvious when you have basic syntax errors like this. And, in the meantime, double-check your copy/paste from other examples to make sure they are identical (except where you're supposed to change things)... ;) :)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest