views:

171

answers:

1

I would like to modify the filesystem path for tracks on itunes programmatically, so that I can apply a string transformation to some of the tracks locations (which are now stored in a different places on the filesystem).

I've tried using AppleScript to update the location property of the relevant tracks but I get an end-of-file error when calling "set mytrack's location to ..."

I've seen various other hacks online that involve exporting the entire track db, modifying it in XML, and then reimporting it - but that seems to lose too much metadata (such as playlists).

+1  A: 

It would really help to see more of your code. Of particular interest is the value you are using and how it is derived. It would also be useful to see the exact error message you get (you should be able to copy the text out of the AppleScript error dialog sheet if you are running the program from Script Editor/AppleScript Editor).

The dictionary entry for the file track class shows its location property being a writable alias value. The problem you are probably running into is that you are not using an alias for the value.

The following code shows how one might change a track's location using an interactive prompt from choose file (which returns an alias):

set m to path to music folder
tell application "iTunes"
    set trk to first item of selection
    set l to location of trk
    if class of l is alias then
        set m to l
    end if
    set {d, a, n} to {database ID, artist, name} of trk
    choose file with prompt "Choose the file to use for " & d & ": " & a & "—" & n default location m
    set location of trk to result
end tell

The choose file method is not what you want though, since you are doing some kind of automated, string based pathname translation.

When working with pathnames in AppleScript, there are two kinds that you might use: POSIX and HFS. POSIX pathnames have slash delimited components (and allow colons inside any component). HFS pathnames have have colon delimited components (and allow slashes inside any component), and they usually start with a volume name component.

To convert a POSIX pathname stored in a variable str to an AppleScript alias, use the following expression:

POSIX file str as alias

To convert an HFS pathname stored in a variable str to an AppleScript alias, use the following expression:

alias str

For example:

tell application "iTunes"
    set trk to first item of selection
    set l to location of trk
    set newPath to my computeNewPath(POSIX path of l)
    set location of trk to POSIX file newPath as alias
end tell

to computeNewPath(pp)
    -- presumably something interesting happens here
    return pp
end computeNewPath
Chris Johnsen
I was missing the cast to alias type in my code. Thanks!
Ben Clifford