It works!
(*
Move tracks to album folder
based upon post "Applescript to copy/export mp3 by album"
https://discussions.apple.com/message/24855977
With final notification line modified from "Move Files To Folder" script on Dougscripts web site (no longer posted there).
Moves tracks from one or more albums into new folders named according to the source albums into a selected main folder. E.g. All selected tracks from album XYZ will end up in a folder XYZ in the main selected folder.
Comments:
1) If the album folder already exists and more files with the same album name are moved, they will be added to the existing album folder. The original album folder is not overwritten.
2) I have not tested this to see what happens if files with the same name are copied into the folder. It will probably generate an obscure Applescript error but leave the original file unaffected unless you change the bit about "replacing" in the final step.
3) Moving only works within a volume, not between volumes.
4) Don't use this to move files unless you manaually manage your iTunes music or else you may end up with broken links! See the note below about reverting this to a "copy" function as in the original script.
- "Limnos" on Apple Support Communities, March 11, 2014
*)
(*
This section does not seem to work in Tiger OS Applescript and is redundant if running the script from inside iTunes:
if application "iTunes 7.5" is not running then
display alert "iTunes is not running" message "Please activate iTunes and select items."
return
end if
*)
-- Choose destination location
(*
This version automatically begins folder selection step at the desktop:
choose folder with prompt "Choose a folder to copy the selected iTunes items into:" default location (path to desktop)
*)
-- This version takes you to the last folder used:
choose folder with prompt "Choose a folder into which to copy the selected iTunes items inside their album folder:"
set DestinationFolder to the result
set TrackLocations to {}
-- Get filepaths of the selected iTunes items
tell application "iTunes 7.5"
repeat with SomeTrack in (get the selection)
if class of SomeTrack is file track then
set TrackLocation to location of SomeTrack
set albumName to album of SomeTrack
-- finds or creates a folder in the destination folder named after the albumName
set destinationSubFolder to my checkForFolder(POSIX path of DestinationFolder, albumName)
-- copies file track file to new folder
my copyFileToFolder(TrackLocation, destinationSubFolder)
end if
end repeat
end tell
tell application "Finder" to reveal DestinationFolder
to checkForFolder(fParent, fName)
-- find or create a folder
tell application "System Events"
if not (exists folder fName of folder fParent) then
set output to path of (make new folder at end of folder fParent with properties {name:fName})
else
set output to (path of (folder fName of folder fParent))
end if
end tell
-- returns an HFS path string, which the Finder likes
return output
end checkForFolder
-- The "move" statement below can be changed back to "copy" if you want to put copies in a folder, not actually move the originals. The copies do not have entries in iTunes so you do not end up with duplicates. Moved files do preserve links to their entries in iTunes as long as you have disabled iTunes managing your media for you, allowing you to move files around without the links breaking.
to copyFileToFolder(fileAlias, folderPath)
tell application "Finder"
-- you can use with or without replacing, as you prefer. You need one or the other to avoid any 'duplicate file' error messages
move fileAlias to folderPath without replacing
end tell
end copyFileToFolder
-- This pops up a notification button that the script ran okay but doesn't tell you if everything was moved successfully.
display dialog "Script run finished, check your result." buttons {"Done"} default button 1 with icon 1 giving up after 4