on run argv - pass itunes track property

I’d like to have an applescript that I can call from the commandline like this:

  trackProperty.applescript "album artist"

and then in my applescript instead of having a line

set foundProperty to (get album artist of t)

have something like

set foundProperty to ( get trackProperty of t)

The idea being that the script would be able to get any property, allowing things like

trackProperty.applescript “artist”
trackProperty.applescript “album”
trackProperty.applescript “album rating”

I’m getting the parm I’m passing fine -

if (count of argv) is 1 then
set trackProperty to item 1 of argv
log ("Looking for " & trackProperty)

displays
Looking for album artist

but

set myProperty to (get trackProperty of t)

gets a descriptor type mismatch.

I tried various combinations of set trackProperty to item 1 of argv as property or as item but haven’t hit on the right way - and google isn’t helping today.

What am I missing?

Thanks in advance.

1 Like

Well, this does what I want but it is less elegant than I hoped…

                    if trackElement is "album artist" then set myElement to (get album artist of t)
                    if trackElement is "album" then set myElement to (get album of t)
                    if trackElement is "artist" then set myElement to (get artist of t)
                    etc. etc. etc.
                    log (myElement)

Very belatedly, this should do what is desired.

It uses osascript to enable calling the script from the shell’s command line. Basically, it requires prepending ‘osascript’ to your command, like so:

osascript trackProperty.applescript "album artist"

Note that the quotes are required only when the string contains a space. So for example, you could enter ‘album’ or ‘artist’ without any quotes and it will return the appropriate property.

As is, it requires that the terminal’s working directory contain the applescript. You could probably monkey around with the shell in some manner (variable, alias, function) to expand on that. Your example uses a text file but you could also save as a regular script and use ‘scpt’ instead of ‘applescript’. I included the name property as well.

Note that the script has two parts: the ‘run’ handler, and a generic handler which is called by that run handler.

The run handler here simply runs argv through the generic ‘extract’ handler.

The ‘extract’ handler takes the passed string and looks for it in a prepared list of itunes property labels as well as its index. It then finds the corresponding property in a prepared list of properties and gets its value. It returns only a single property value, whichever might be entered first.

use scripting additions

on run argv
	return extract(item 1 of argv)
end run

on extract(perp)
	tell application "iTunes"
		set ct to current track
		
		-- properties to track
		set pack to {name, album, artist, album artist} of ct
		-- entry string for corresponding pack item
		set pump to {"name", "album", "artist", "album artist"}
		-- counter
		set numo to {1, 2, 3, 4}
		
		repeat with p from 1 to length of numo
			if perp is equal to contents of item p of pump then
				exit repeat
			end if
		end repeat
		
		item (item p of numo) of pack
	end tell
end extract

If any other track properties are of interest, these can be added to the extract handler. If the first entered string is not contained with the pump list, a -1728 error will result.

NB While this is an old post, the subject is interesting to me; hopefully it is to others as well. For me, it also solves the matter of getting a property based on a string, especially as an argument.

I add here a multi-property variant of the above script. The ‘extract’ handler remains unchanged so I will include only the run handler as well as an example shell command. Swap this run handler in for the other. It handles single property entries as well. Note the change in script name.

osascript trackProperties.applescript name artist album "album artist"

This command will return all four passed properties, each on its own line. As argv assumes spaces as delimiters, multi-word properties must be enclosed in quotes. Single quotes seem to work fine.

Basically, it runs each argv list item through the extract handler and makes a list of the resulting properties. Once done, it joins the list items around the delimiter and returns the result.

on run argv
	set paras to {}
	repeat with arg in argv
		set tmp to ex(contents of arg)
		set end of paras to tmp
	end repeat
	set text item delimiters to linefeed
	return paras as text
end run

NB These should work fine with Music instead of itunes but I can’t test that.

Try
track’s valueForKey:trackPropetyName

You could also use run script to evaluate statements created with the passed terms, for example:

on run argv
	set results to {}
	repeat with term in argv
		try
			set end of results to (run script "tell application \"Music\" to return " & term & " of current track")
		on error errmess
			set end of results to "Invalid term " & quoted form of term -- or blank string, whatever
		end try
	end repeat
	return results
end run

The osascript -ss option can be used to get the results in source form, which may or may not make the list easier to parse (quoted strings, etc). And yes, it does work with Music.