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.