You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Is there a simple way to play an album/artist in iTunes via osascript?

There's a lot to be found online about everything re: iTunes and AppleScript -- simple ways to play a playlist, play a stream, bump to previous/next track, set volume, etc. But for whatever reason, playing an album or artist has to be much more complicated, and I've found almost no advice about it.

Indirectly I've seen part of a solution to, for example, "set my tracklist to (every track of playlist 'Library' whose artist is '$artist' ...)" etc. Then I assume you must loop through and play each track.

Is there no way to do this more simply, without all the jumping jacks -- in other words can't I just tell iTunes (in AppleScript, from the shell w/osascript) to select and play an album or artist?

I probably could have gone farther with this if I had found a reference somewhere that says, "Here's what you can do with iTunes and AppleScript," but I can't find that either.

Mac Mini 1.5GHz Intel Core Solo, Mac OS X (10.4.10)

Posted on Jul 22, 2007 5:27 PM

Reply
Question marked as Top-ranking reply

Posted on Jul 22, 2007 5:53 PM

>Indirectly I've seen part of a solution to, for example, "set mytracklist to (every track of playlist 'Library' whose artist is '$artist' ...)" etc. Then I assume you must loop through and play each track.

No, you don't need to loop through the list. You can play them directly:

<pre class=command>tell application "iTunes"
play (every track of playlist "Library" whose album is "Shine")
end tell</pre>

or, via the shell:

<pre class=command>osascript -e 'tell application \"iTunes\" to play (every track of playlist \"Library\" whose album is \"Shine\")'</pre>
11 replies
Question marked as Top-ranking reply

Jul 22, 2007 5:53 PM in response to jccc

>Indirectly I've seen part of a solution to, for example, "set mytracklist to (every track of playlist 'Library' whose artist is '$artist' ...)" etc. Then I assume you must loop through and play each track.

No, you don't need to loop through the list. You can play them directly:

<pre class=command>tell application "iTunes"
play (every track of playlist "Library" whose album is "Shine")
end tell</pre>

or, via the shell:

<pre class=command>osascript -e 'tell application \"iTunes\" to play (every track of playlist \"Library\" whose album is \"Shine\")'</pre>

Jul 22, 2007 6:26 PM in response to Camelot

It didn't like the escaped quotes. It did work without the escaped quotes, however only the first found track played, followed by some random track elsewhere in the library. Response on the command line was:

<pre class=command> > osascript -e 'tell application "iTunes" to play (every track of playlist "Library" whose artist is "The Beatles")'
iTunes, iTunes, iTunes, iTunes, iTunes, iTunes, iTunes, iTunes, iTunes</pre>

That's one "iTunes" for each matching track it found. But after the first track, some non-matching track from the rest of the library plays.

Jul 22, 2007 11:14 PM in response to Camelot

Do you have shuffle turned on? It may be that shuffle
is overriding the list of songs to play.


Shuffle indeed was/is not on.

Does the above osascript line work for you -- i.e. playing an artist/album and correctly playing beyond the first track? If so, then I definitely have something strange happening on my end.

Jul 23, 2007 6:44 AM in response to jccc

This works on my machine:

osascript -e 'tell application "iTunes" to play track 1 of playlist "Cubs"'

By telling iTunes to play track 1 of a playlist, it will make that playlist the 'active' playlist, and it will continue to play all the tracks in that playlist (shuffle or not) until it is finished.

Jul 25, 2007 7:09 PM in response to casdvm

I assumed that would work for a named, pre-existing playlist, and your line does. But what I'm trying to do still doesn't work that way, behaving the same as described above (playing one found track, then going off to other random tracks):

osascript -e 'tell application "iTunes" to play track 1 of (every track of playlist "Library" whose artist is "The Beatles")'

This appears to be the only way it will work:

osascript -e 'tell application "iTunes"' -e 'if (exists playlist "temp_playlist") then' -e 'delete playlist "temp_playlist"' -e 'end if' -e 'set name of (make new playlist) to "temp_playlist"' -e 'set theseTracks to every track of playlist "Library" whose artist is "The Beatles"' -e 'repeat with thisTrack in theseTracks' -e 'duplicate thisTrack to playlist "temp_playlist"' -e 'end repeat' -e 'play playlist "temp_playlist"' -e 'end tell'

Which is fine by me I suppose. Can wrap it up into it's own script or shell script. I just wish the actual AppleScript were more graceful for doing something this straightforward.

Jul 25, 2007 8:24 PM in response to jccc

Best I've come up with below. I put this into /usr/local/bin as "playitunes" (remembering to chmod 755) for a case-insensitive search of all artists, albums and track names. I'm sure there's a more graceful way to do this, and I have no idea how to set the sort order, but it works.

----------------------------------

#!/bin/bash

osascript -e 'tell application "iTunes"' -e 'if (exists playlist "temp_playlist") then' -e 'delete playlist "temp_playlist"' -e 'end if' -e 'set name of (make new playlist) to "temp_playlist"' -e 'set theseTracks to every track of playlist "Library" whose artist contains "'"$1"'"' -e 'repeat with thisTrack in theseTracks' -e 'duplicate thisTrack to playlist "temp_playlist"' -e 'end repeat' -e 'set theseTracks to every track of playlist "Library" whose album contains "'"$1"'"' -e 'repeat with thisTrack in theseTracks' -e 'duplicate thisTrack to playlist "temp_playlist"' -e 'end repeat' -e 'set theseTracks to every track of playlist "Library" whose name contains "'"$1"'"' -e 'repeat with thisTrack in theseTracks' -e 'duplicate thisTrack to playlist "temp_playlist"' -e 'end repeat' -e 'play playlist "temp_playlist"' -e 'end tell'

----------------------

Sorry, I can't get line breaks to work in here at all, so the above is just ugly.

Jul 26, 2007 2:26 AM in response to jccc

Okay, well after a while this starts happening:

MY PROMPT: osascript -e 'tell application "iTunes" to play playlist "Radio"'

THE RESPONSE: 2007-07-26 03:50:12.127 osascript(778) CFLog (0): CFMessagePort: bootstrap_register(): failed 1100 (0x44c), port = 0x2103, name = 'Processes-0.17301505'
See /usr/include/servers/bootstrap_defs.h for the error codes.
2007-07-26 03:50:12.128 osascript(778) CFLog (99): CFMessagePortCreateLocal(): failed to name Mach port (Processes-0.17301505)
CFMessagePortCreateLocal failed (name = Processes-0.17301505 error = 0)
Abort trap

The actual Applescript being sent to iTunes doesn't matter. My first osascript attempts after a fresh boot work over and over. After iTunes has been playing a while, this starts failing with the above message.

Actually, I just checked and any attempt to invoke osascript for anything gives me the same error. I don't know if this has something to do with using the script I cobbled together (previous message above), but I think I've pretty much given up on this. I can't even get this forum text box to take the error message text correctly without munging it with its "helpful" edits.

Is there a simple way to play an album/artist in iTunes via osascript?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.