Updating references in Music after copying files to a new location
This tip is in response to the thread: Move un-consolidated files in Apple Music… - Apple Community.
The aim of the script below is to allow you let Music know the new location of files that you've copied to a new path, so that it doesn't break when you delete the files from the old path and then empty the trash. Before starting files should exist in both the source and destination folders, and these should be entered into the script in the appropriate location. Once that is done you can select some or all of the tracks that need to be updated and call the script. On completion the script should indicate how many tracks were updated, and report on any errors, such as missing files, or any there weren't in the source or destination locations.
Copy the script below the line into the Script Editor and save it as SwitchLinks.
__________________________________________________________________________________
-- SwitchLinks - V1.0 - © Steve MacGuire - 2021-11-16
-- Update references in Music from a source folder to a destination folder for selected tracks
-- The files must exist on both paths for the change to take place
-- E.g. Music is linked to files at <Media Folder 1>/<Various Subfolders>/<File>.<Ext>
-- The same files exist at <Media Folder 2>/<Various Subfolders>/<File>.<Ext>
-- where the paths are identical within the two media folders
-- Running the script with selected tracks will swap the reference in Music from folder 1 to folder 2
set sourcePath to "" -- set to source media folder
set destPath to "" -- set to destination media folder
if not my folderExists(sourcePath) then
display dialog "Please edit the script to set a valid source folder." with title "SwitchLinks"
else if not my folderExists(destPath) then
display dialog "Please edit the script to set a valid destination folder." with title "SwitchLinks"
else
-- tell application "iTunes"
tell application "Music"
if selection is not {} then
set mySelection to selection
set m to 0
set p to 0
set u to 0
set s to 0
set rootLen to count sourcePath
repeat with aTrack in mySelection
set p to p + 1
set aPath to location of aTrack as text
set aPath to POSIX path of aPath
if aPath = "" then -- check for missing source files
set m to m + 1
else if (count aPath) > rootLen then
set root to characters 1 thru rootLen of aPath as text
-- display dialog root & return & sourcePath
if root = sourcePath then -- check file is in the source folder
set branch to characters (rootLen + 1) thru -1 of aPath as text
set rootBranch to destPath & branch
-- display dialog "OldPath = " & aPath & return & return & "New Path = " & rootBranch
if my fileExists(rootBranch) then -- and copy is in the destination folder
set location of aTrack to rootBranch -- if so update the link
set u to u + 1 -- and increment update cound
else
-- display dialog "Track not found at " & rootBranch
set m to m + 1 -- otherwise update missing file count
end if
else
-- display dialog "Track at " & aPath & " is not in " & sourcePath
set s to s + 1 -- skip files not in source folder
end if
else
-- display dialog "Track at " & aPath & " is not in " & sourcePath
set s to s + 1 -- skip files not in source folder
end if
end repeat
set msg to (p as string) & " item"
set msg to msg & my plural(p, "s were", " was")
set msg to msg & " processed."
set msg to msg & return & (u as string) & " item"
set msg to msg & my plural(u, "s were", " was")
set msg to msg & " updated."
if m > 0 then
set msg to msg & return & (m as string) & " item"
set msg to msg & my plural(m, "s were", " was")
set msg to msg & " missing (not in target folder)."
end if
if s > 0 then
set msg to msg & return & (s as string) & " item"
set msg to msg & my plural(s, "s were", " was")
set msg to msg & " skipped (not in source folder)."
end if
display dialog msg with title "SwitchLinks"
end if
end tell
end if
-- check that a file exists at a given path
on fileExists(theFile) -- (String) as Boolean
tell application "System Events"
if exists file (my replace(theFile, "/", ":")) then
-- display dialog theFile & " exists!"
return true
else
-- display dialog theFile & " does not exist!"
return false
end if
end tell
end fileExists
-- check that a folder exists at a given path
on folderExists(theFolder) -- (String) as Boolean
tell application "System Events"
if exists folder (my replace(theFolder, "/", ":")) then
-- display dialog theFolder & " exists!"
return true
else
-- display dialog theFolder & " does not exist!"
return false
end if
end tell
end folderExists
-- return plural or singular form dependent on V
on plural(V, p, s)
if V = 1 then
return s
else
return p
end if
end plural
-- find : Text to be found
-- replace : Text to replace with
-- someText : Text to be searched
on replace(someText, find, replace)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set someText to text items of someText
set text item delimiters of AppleScript to replace
set someText to "" & someText
set text item delimiters of AppleScript to prevTIDs
return someText
end replace