4

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).

3 Answers 3

3

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 
Sign up to request clarification or add additional context in comments.

Comments

2

How to move media files (that are not "organized" by iTunes) to a different location while keeping the iTunes library database (iTunes Library.itl) intact:

  1. Move files to new location (e.g., mv ~/MyMusic /Volumes/Huge/)
  2. Create symlink in old location pointing to new location
    (ln -s /Volumes/Huge/MyMusic ~/MyMusic)
  3. Start iTunes (if not already running)
  4. Select all tracks that were moved.
  5. Run this AppleScript:

    -- Mark missing tracks (and update database with real path of existing -- tracks) in iTunes -- Instructions: -- * symlink new file location to old location (old location points to -- new location, file exists) -- * select tracks to scan/update -- * run the script -- * missing tracks are marked with (!) and all other track paths have -- been updated to the real path (symlinks eliminated) in iTunes tell application "iTunes" set fx to fixed indexing set fixed indexing to true set Sel to the selection repeat with i from 1 to number of items in Sel set trk to item i of Sel set loc to (get location of trk as text) end repeat set fixed indexing to fx end tell 
  6. All tracks should now point to the correct location, and the symlink(s) can be removed. This can be verified by selecting Get Info on a track that was moved and verify that the path points to the new location.

If you didn't get the symlink correct iTunes will display (!) beside each missing track. To fix the issue, simply create a symlink in the old location pointing to the new location and run the script again. Hint: the Get Info dialog can be used to determine the old location of a missing track.

This worked on iTunes 11.0.5

1 Comment

I had to add a line set location of trk to loc but then it worked fine.
1

Adding to the previous answer by millerdev I updated the script to work with MacOS Catalina and the new Music app. Just create a $HOME/Library/Music/Scripts directory and place it there.

tell application "Music" set fx to fixed indexing set fixed indexing to true set Sel to the selection repeat with i from 1 to number of items in Sel set trk to item i of Sel set l to location of trk set location of trk to l end repeat set fixed indexing to fx end tell 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.