dougscripts.com

AppleScript Changes in iTunes 4.5

iTunes 4.5 was released on April 28, 2004. New features include Party Shuffle, iMix Playlist publishing on the iTunes Music Store, printing of various playlist and track data as lists or as CD case inserts, introduction of Apple's Lossless Encoder, and improved iTMS search from your library.

 

Overview of AppleScript Features

Two new major feature additions to iTunes' AppleScript capabilities are the new print and search commands. More on these below.

The algorithm of "Party Shuffle" is not scriptable. That is, you cannot set its source and display criteria. However, you can access the "Party Shuffle" playlist like so:

tell application "iTunes"
	set partyP to playlist "Party Shuffle"
	--
	tell partyP
		count every track
		name of every track
		smart
	end tell
	duplicate track 1 of library playlist 1 to partyP
end tell

[upm] click to open in Script Editor (???)

"Party Shuffle" is of the class user playlist. If you experience localization issues with the name reference (eg: playlist "Party Shuffle"), user playlist 1 seems to always return the correct reference to it.

Probably sensible to let "Party Shuffle" do its own thing and use search to do-up some customized playlist building.

Also noticed that when you select tracks in the "Party Shuffle" playlist and select "New Playlist from Selection", iTunes does not name the new playlist as usual. Here's an AppleScript that will allow you to take a snapshot of "Party Shuffle", copying its tracks to a user-named playlist:

tell application "iTunes"
	set partyP to playlist "Party Shuffle"
	set newP to text returned of (display dialog "Name of \"Party Shuffle\" copy:" default answer (get name of partyP))
	make new user playlist with properties {name:newP}
	duplicate every track of partyP to playlist newP
	try
		set view of front window to playlist newP
	end try
end tell

[upm] click to open in Script Editor (???)

You cannot script iMix, the iTunes Music Store Playlist publishing feature.

Apple's new Apple Lossless Encoder shows up in your Importing Preferences, selectable like the previous four encoders. In general, here's how I select an encoder:

tell application "iTunes"
	-- store current encoder
	set nameOfCurEncoder to current encoder
	set current encoder to encoder "Lossless Encoder"
	
	-- do some stuff with encoder...then restore old encoder
	
	set current encoder to nameOfCurEncoder
end tell

-- or, via choose box

tell application "iTunes"
	set myChoice to (choose from list (get name of encoders)) as string
	if myChoice is false then return
	
	-- store current encoder
	set nameOfCurEncoder to current encoder
	set current encoder to encoder myChoice
	
	-- do some stuff with encoder...then restore old encoder
	
	set current encoder to nameOfCurEncoder
end tell

[upm] click to open in Script Editor (???)

That last one is usually safer because it uses iTunes' own list of encoders. I don't know if there are localization issues with using strings, like "MP3 Encoder", "Lossless Encoder", etcetera.

Printing

New in iTunes 4.5 is a print command in the Standard Suite that will enable you to print track and album listings based on a particular playlist reference, and also print CD inserts, like the new Print command in the File menu. In fact, it looks like you can emulate the entire iTunes Print process with AppleScript. This is basically how to print a track listing of the selected playlist:

tell application "iTunes"
	-- selected playlist
	set thisP to view of front window
	print thisP kind track listing
end tell

[upm] click to open in Script Editor (???)

The print command uses a playlist reference, "thisP" above, and the kind parameter takes track listing, album listing, or cd insert. Additionally you can display the print dialog, before printing, and establish some printing properties using the properties from print settings.

tell application "iTunes"
	-- selected playlist
	set thisP to view of front window
	print thisP kind track listing with properties {copies:2} with print dialog
end tell

[upm] click to open in Script Editor (???)

You can even assign a theme:

tell application "iTunes"
	-- selected playlist
	set thisP to view of front window
	print thisP kind track listing with properties {copies:1} theme "User ratings"
end tell

[upm] click to open in Script Editor (???)

The print setting properties are listed in the Dictionary. They are read-only, but I got "copies" to work. The themes are not listed anywhere. Just use the names as they are listed in the print dialog:

kindtheme
cd insertText only
Mosiac
White mosaic
Single cover
Text only (Black & White)
Mosaic (Black & White)
Single side (Black & White)
Large playlist (Black & White)
track listingSongs
User ratings
Dates played
Custom
album listingSongs by album

Search

The new search command has some pretty cool potential. According the Dictionary entry, the search command is "identical to entering search text in the Search field in iTunes". Here's a script that creates a new playlist using the results of a search for all the tracks by the currently playing artist (manually, you'd search, select all, make new playlist, name new playlist):

tell application "iTunes"
	if player state is playing then
		set art to (get artist of current track)
		set newP to (make new playlist with properties {name:("All " & art & " Tracks")})
		
		repeat with aTr in (search library playlist 1 for art)
			duplicate aTr to newP
		end repeat
		
	end if
end tell

[upm] click to open in Script Editor (???)

I didn't use the "only" parameter in the example above. (Also, I thought that duplicate could take a list as a direct object—search returns a list of track references—but I guess not; I had to distribute the tracks via the repeat loop.)

The syntax looks like this:

search [playlist reference] for [text string to search for] only [choice of albums, all, artists, composers, displayed, and songs, with all being the default if you don't set the "only" parameter]

Setting the "only" parameter enables you to limit or broaden your search accordingly. The displayed setting refers to the columns displayed, not the tracks displayed.

A search of the selected playlist might look like this:

tell application "iTunes"
	set searchText to text returned of ¬
		(display dialog "Enter search text:" default answer "")
	set searchResults to search (view of front window) for searchText only displayed
	-- do something with searchResults...
end tell

[upm] click to open in Script Editor (???)

Scriptable iTMS Previews

A feature of iTunes 4.5 is the ability to drag the :30 second previews from the iTunes Music Store to your own playlist in iTunes. (When you drag a Preview to a playlist you get the "Price" column in the playlist, too. Artwork is available on purchase.) The tracks behave like any other track, so you can access them, sort of, with AppleScript.

Preview tracks are of the class URL track, although you cannot access the address property. Other track properties of Preview tracks that are not accessible are bit rate, size, sample rate, and location.

I've been running Google Lyric Search, AMG EZ Search, Export As HTML Table and stuff like that on Previews with success.

"Party Shuffle"

The new "Party Shuffle" playlist is classed as a user playlist, so it has a smart property. Since the "Party Shuffle" playlist is dynamic, like Smart Playlists, wouldn't you expect its smart property to be true? It isn't, though.

Many times I will require a list of user playlists with Smart Playlists filtered out—ie: live-updating renders their contents transient and unpredictable—and the smart property of the playlist is how to do it:

tell application "iTunes"
	set myPlaylists to the user playlists whose smart is false
end tell

[upm] click to open in Script Editor (???)

The whose is the filter, and the expression that follows it must be true or the target object is restricted from the result.

But when you run that a reference to "Party Shuffle" also gets listed in the result. As I said, I don't want playlists whose contents are dynamic. So you have to filter "Party Shuffle":

tell application "iTunes"
	set myPlaylists to the user playlists whose smart is false and name is not "Party Shuffle"
end tell

[upm] click to open in Script Editor (???)

Since we're talking about filtering playlists: Depending on how you feel about "Purchased Music" playlist, for users who maintain it, you may want to filter that in the same way.

I haven't figured out an expedient way to determine if any playlist is "Party Shuffle" except by using its name. I suppose this is fine for those of us using English. (Mais, pouvez-vous me dire le mot fran¨ais?) I prefer an internal reference (user playlist id 6214 of source id 34 of application "iTunes") rather than referencing a playlist by name. A text string just doesn't feel reliable.

more to come...

Site contents © 2001 - 2024 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Privacy.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
The one who says "it cannot be done" should not be interrupting the one who is doing it.