Help to convert iTunes AppleScript to Python

Use this forum to post specific questions you may have when trying to convert an AppleScript to Python. Answers may also include pointers to plugins that would help.
hamw
Posts: 1246
Joined: Mon Mar 31, 2008 7:45 pm

Help to convert iTunes AppleScript to Python

Post by hamw »

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
User avatar
jay (support)
Site Admin
Posts: 18411
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Help to convert iTunes AppleScript to Python

Post by jay (support) »

[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
hamw
Posts: 1246
Joined: Mon Mar 31, 2008 7:45 pm

Re: Help to convert iTunes AppleScript to Python

Post by hamw »

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.
User avatar
jay (support)
Site Admin
Posts: 18411
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Help to convert iTunes AppleScript to Python

Post by jay (support) »

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
hamw
Posts: 1246
Joined: Mon Mar 31, 2008 7:45 pm

Re: Help to convert iTunes AppleScript to Python

Post by hamw »

I may have been a bit unclear in my prior post. the initial example is one that apparently works in this thread viewtopic.php?f=252&t=20486&hilit=impor ... ipt+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:viewtopic.php?f=252&t=20486&hilit=impor ... es#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...
User avatar
FlyingDiver
Posts: 7323
Joined: Sat Jun 07, 2014 10:36 am
Location: Southwest Florida, USA

Re: Help to convert iTunes AppleScript to Python

Post by FlyingDiver »

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
User avatar
FlyingDiver
Posts: 7323
Joined: Sat Jun 07, 2014 10:36 am
Location: Southwest Florida, USA

Re: Help to convert iTunes AppleScript to Python

Post by FlyingDiver »

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
hamw
Posts: 1246
Joined: Mon Mar 31, 2008 7:45 pm

Re: Help to convert iTunes AppleScript to Python

Post by hamw »

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/160 ... -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?
User avatar
jay (support)
Site Admin
Posts: 18411
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Help to convert iTunes AppleScript to Python

Post by jay (support) »

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
hamw
Posts: 1246
Joined: Mon Mar 31, 2008 7:45 pm

Re: Help to convert iTunes AppleScript to Python

Post by hamw »

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?
User avatar
jay (support)
Site Admin
Posts: 18411
Joined: Wed Mar 19, 2008 11:52 am
Location: Austin, Texas
Contact:

Re: Help to convert iTunes AppleScript to Python

Post by jay (support) »

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
Post Reply

Return to “Help Converting from AppleScript”