Getting user playlist id into a variable

Hi there!

I’m trying to get the user playlist id into a variable, problem is I can’t call it with its property, tried making it into a string but no go.

Here the script:

tell application "iTunes"
	set TheLibraryRef to (every user playlist)
	set thetest to (item 1 of TheLibraryRef)
end tell

Here is the result:

“user playlist id 7881 of source id 39 of application “iTunes””
Now how do I get 7881 into a plain variable?

Thanks!

Micha

Give this a try, playlist_id should be the value you are looking for.

tell application "iTunes"
	set TheLibraryRef to (every user playlist)
	try
		set thetest to (item 1 of TheLibraryRef) as string
	on error errMsg
		set playlist_id to word 6 of errMsg
	end try
end tell

It’s a bit fiddly. AppleScript doesn’t allow you to look inside reference objects, so you can’t get the id from the reference itself. Fortunately, iTunes objects have an undocumented ‘id’ property which you can use to get their id numbers; unfortunately, iTunes’ dictionary doesn’t define an ‘id’ keyword [1], which means that AppleScript can’t compile the following code:

tell app "iTunes"
    set playlistRef to user playlist id 7881 of source id 39
    set playlistID to id of playlistRef -- typically cryptic syntax error occurs here
end tell

You can get around this by making sure the ‘id’ keyword appears outside the application tell block:

tell app "iTunes"
    set playlistRef to user playlist id 7881 of source id 39
end tell
set playlistID to id of playlistRef --> 7881

A bit messy, but it works. (Modulo the usual caveats of using undocumented features, of course.)

HTH

[1] Feel free to file a bug report with Apple on this.

Hi,

this cannot compile like your other example, because AppleScript itself has no id keyword

error message: Expected expression but found property or key form.

Maybe you have installed some OSAX, which provides an id keyword

There is an ‘id’ keyword defined in AppleScript’s dictionary on 10.4 (older OSes I don’t know about), although it doesn’t do anything useful within AppleScript itself. The second example compiles fine here. If it won’t compile for you, use the raw form:

 set playlistID to «property ID  » of playlistRef

(Note: there should be not one but two spaces after ‘ID’ in the above code; the forum software doesn’t preserve them properly.)

Weird, it works indeed, thanks :slight_smile:

Yo!

Thanks guys! Learned something here, especially the error catching which enables to grab the string.

Thanks again.

Micha