AppleScript->Python, iTunes Radio

Posted on
Thu Apr 05, 2018 2:13 am
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

AppleScript->Python, iTunes Radio

Thanks for the help to convert this one:
(or is there a plugin for selecting specific iTunes numbers/radio stations?)

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

Posted on
Thu Apr 05, 2018 2:38 am
FlyingDiver online
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: AppleScript->Python, iTunes Radio

That one doesn't need to be converted, as it doesn't target the Indigo server.

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

Posted on
Fri Apr 06, 2018 1:46 am
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

Re: AppleScript->Python, iTunes Radio

That's true (and we know) but Jay said:
we expect that you won't be able to embed AppleScripts any more, so the only way to execute them will be via an external script file


So, we don't want ~100 external scripts and we think the Python method is more smooth.
It this script possible in Python?

Posted on
Fri Apr 06, 2018 8:34 am
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: AppleScript->Python, iTunes Radio

McJohn wrote:
It this script possible in Python?

The only way you'll get that in "Python" is if you use the Python AppleScript library to run it, or you can alternatively running it using subprocess to execute osascript since it's just a simple one line script:

Code: Select all
import subprocess

command = ['osascript', '-e', 'tell application "iTunes" to play first track of playlist "Radio" whose name is "Sky Radio 101 FM"']
p = subprocess.check_output(command)


The problem is that iTunes commands are AppleScript specific mostly. I'm actually playing with a new plugin to work with iTunes under the new AppleScript-free model but it's just in the beginning phases.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Fri Apr 06, 2018 9:22 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: AppleScript->Python, iTunes Radio

Using py-applescript as described in the blog post:

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'm not sure if the new iTunes Local Control plugin supports that level of specification, but it might. He's using the py-applescript library to do the work in that plugin as well.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Apr 06, 2018 11:24 am
kmarkley offline
Posts: 185
Joined: Nov 15, 2016

Re: AppleScript->Python, iTunes Radio

jay (support) wrote:
I'm not sure if the new iTunes Local Control plugin supports that level of specification, but it might. He's using the py-applescript library to do the work in that plugin as well.

It does not. And I'm not sure I could cover every use-case. What if you wanted the Nth track, or the last track that doesn't match a condition?

But I could easily add a 'play something' action where you supplied the applescript specifier in the action config -- in this case 'first track of playlist "Radio" whose name is "Sky Radio 101 FM"'

Would that be useful to folks?

Posted on
Fri Apr 06, 2018 12:01 pm
Professor Falken offline
User avatar
Posts: 289
Joined: Mar 29, 2015

Re: AppleScript->Python, iTunes Radio

Colorado4Wheeler wrote:
McJohn wrote:
It this script possible in Python?

The only way you'll get that in "Python" is if you use the Python AppleScript library to run it, or you can alternatively running it using subprocess to execute osascript since it's just a simple one line script:

Code: Select all
import subprocess

command = ['osascript', '-e', 'tell application "iTunes" to play first track of playlist "Radio" whose name is "Sky Radio 101 FM"']
p = subprocess.check_output(command)


The problem is that iTunes commands are AppleScript specific mostly. I'm actually playing with a new plugin to work with iTunes under the new AppleScript-free model but it's just in the beginning phases.



Oh, Oh, Oh! In that case C4W, can I make a feature request??? To have the ability to play from a particular playlist:
1) a particular track number (play song n from playlist x) and
2) to play a random song from a playlist (play random selection from playlist y)?

Also, to have itunes stop after the one selection finishes (get duration of the track, play it, delay, then stop itunes).

I actually have a set of applescripts that do all this, and I've begun saving them all externally and using python to call them, in order to better survive the upcoming loss of Indigo Applescritpt support, but if you were to add that functionality into a new itunes plugin, it would greatly simplify my setup.

Thanks for considering it.

Posted on
Fri Apr 06, 2018 1:02 pm
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

Re: AppleScript->Python, iTunes Radio

Thanks for all the help.
This one works perfect:
import subprocess

command = ['osascript', '-e', 'tell application "iTunes" to play first track of playlist "Radio" whose name is "Sky Radio 101 FM"']
p = subprocess.check_output(command)

Thanks C4W!

We made a number of action groups with this script for different internet radio stations and a logo of each radio station at the control page.

Posted on
Fri Apr 06, 2018 1:17 pm
Colorado4Wheeler offline
User avatar
Posts: 2794
Joined: Jul 20, 2009
Location: Colorado

Re: AppleScript->Python, iTunes Radio

Here is a plugin I wrote for myself that I dressed up for public consumption if you want to kick the tires.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Sat Apr 07, 2018 10:05 am
kmarkley offline
Posts: 185
Joined: Nov 15, 2016

Re: AppleScript->Python, iTunes Radio

I know McJohn has sorted his issue, but I wanted to mention that the iTunes Local Control plugin can now do these additional iTunes-specific things.

Play Playlist Shuffled
Play Single Track by Playlist+Number
Play Single Track by Playlist+Name
Play Applescript Specifier
Execute Applescript Text

Posted on
Mon Apr 09, 2018 7:54 am
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

Re: AppleScript->Python, iTunes Radio

Thanks for the feedback kmarkley.
Just installed your plugin but we got these errors below.

Starting plugin "iTunes Local Control 0.0.6" (pid 23150)
iTunes Local Control Error Error in plugin execution InitializeMain:

Traceback (most recent call last):
File "plugin.py", line 13, in <module>
File "/Library/Application Support/Perceptive Automation/Indigo 7/Plugins/iTunes Local Control.indigoPlugin/Contents/Server Plugin/iTunesAppleScript.py", line 156, in <module>
File "/Library/Application Support/Perceptive Automation/Indigo 7/Plugins/iTunes Local Control.indigoPlugin/Contents/Server Plugin/iTunesAppleScript.py", line 16, in _make
File "/Library/Application Support/Perceptive Automation/Indigo 7/IndigoPluginHost.app/Contents/PlugIns/applescript/__init__.py", line 49, in __init__
ScriptError: Expected end of line, etc. but found property. (-2741) range=85-92

Stopping plugin "iTunes Local Control 0.0.6" (pid 23150)
Stopped plugin "iTunes Local Control 0.0.6"

Posted on
Mon Apr 09, 2018 8:30 am
kmarkley offline
Posts: 185
Joined: Nov 15, 2016

Re: AppleScript->Python, iTunes Radio

Weird. That's an applescript compile error, that I'm not seeing. I won't be able to dig into it until this evening.

Just curious, what OS are you on? Applescript doesn't seem to change much between OS's, but I suppose that might explain it.

Also what version of iTunes?

Posted on
Mon Apr 09, 2018 5:44 pm
kmarkley offline
Posts: 185
Joined: Nov 15, 2016

Re: AppleScript->Python, iTunes Radio

I suspect this is an issue with an older version of iTunes.

Apparently shuffle changed from a playlist property to an application property with iTunes 11. This also broke applescript access to shuffle, but that was fixed in v12.4 according to dougscripts.

To confirm, put this in Script Editor and see if it compiles:
Code: Select all
tell application "iTunes"
   return shuffle enabled
end tell

Posted on
Tue Apr 10, 2018 3:40 am
McJohn offline
User avatar
Posts: 631
Joined: Dec 18, 2012
Location: The Netherlands

Re: AppleScript->Python, iTunes Radio

Thanks for the feedback and sorry for the delay.
Mac OS 10.10.5 (save for Indigo)
iTunes 12.3.2.35 (12.7.4 won't work on this OS)
Indigo 7.1.1

When I run this script:
Code: Select all
tell application "iTunes"
   return shuffle enabled
end tell

It gives a syntax error (Expected end of line but found property).

Posted on
Tue Apr 10, 2018 5:37 am
kmarkley offline
Posts: 185
Joined: Nov 15, 2016

Re: AppleScript->Python, iTunes Radio

McJohn wrote:
It gives a syntax error (Expected end of line but found property).

McJohn wrote:
ScriptError: Expected end of line, etc. but found property. (-2741) range=85-92

This is the problem


McJohn wrote:
iTunes 12.3.2.35 (12.7.4 won't work on this OS)

It might be possible to run 12.4, but I don't know for sure. I also don't know where to find prior versions.


I have updated the plugin description to indicate that iTunes 12.4 or newer is required.

Sorry.

Who is online

Users browsing this forum: No registered users and 3 guests