Created
January 21, 2012 14:22
-
-
Save sumerman/1652914 to your computer and use it in GitHub Desktop.
iTunes Replace Items
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (* | |
| Please note: this is my ad-hoc script and it possibly contains bugs and strange behaviour. | |
| It's also may be convenient to consolidate iTunes library after usage. | |
| *) | |
| on dedup(aList) | |
| set res to {} | |
| repeat with i from 1 to (length of aList) | |
| set cur to item i of aList | |
| if {cur} is not in res then set res to (res & {cur}) | |
| end repeat | |
| return res | |
| end dedup | |
| global allowedExtensions | |
| global theSelectedAlbums | |
| global theSelectedTracks | |
| set allowedExtensions to {"m4a", "mp3", "wav"} | |
| set theSelectedAlbums to {} | |
| set theSelectedTracks to {} | |
| on getFilesForAlbum(anArt, anAlb) | |
| repeat with i from 1 to (length of theSelectedAlbums) | |
| set cur to item i of theSelectedAlbums | |
| if (art of cur = anArt) and (alb of cur = anAlb) then | |
| return fls of cur | |
| end if | |
| end repeat | |
| error string "not found" | |
| end getFilesForAlbum | |
| tell application "iTunes" | |
| set theSelectedTracks to selection | |
| set albs to {} | |
| repeat with i from 1 to (length of theSelectedTracks) | |
| tell item i of theSelectedTracks | |
| set albs to albs & {{art:(get artist), alb:(get album), fls:""}} | |
| end tell | |
| end repeat | |
| set theSelectedAlbums to my dedup(albs) | |
| end tell | |
| -- TODO handle cancel | |
| repeat with i from 1 to (length of theSelectedAlbums) | |
| set cur to item i of theSelectedAlbums | |
| set title to "\"" & alb of cur & "\" by \"" & art of cur & "\"" | |
| set fldr to choose folder with prompt "Chose new source for the " & title | |
| tell application "System Events" | |
| set tPaths to (get path of every file of fldr where visible is true) | |
| set tExts to (get name extension of every file of fldr where visible is true) | |
| if length of tPaths is not equal to length of tExts then error string "WTF?" | |
| -- Filter out files with "non-audio" extension | |
| set rFls to {} | |
| repeat with j from 1 to (length of tPaths) | |
| set p to item j of tPaths | |
| set e to item j of tExts | |
| if e is in my allowedExtensions then | |
| set rFls to rFls & p | |
| end if | |
| end repeat | |
| set (fls of cur) to rFls | |
| end tell | |
| end repeat | |
| tell application "iTunes" | |
| set tracksAndLocs to {} | |
| repeat with i from 1 to (length of theSelectedTracks) | |
| set curTr to item i of theSelectedTracks | |
| set newLocFiles to my getFilesForAlbum(artist of curTr, album of curTr) | |
| set trackno to track number of curTr | |
| set fileLoc to (item trackno) of newLocFiles | |
| set tracksAndLocs to tracksAndLocs & {{tr:curTr, loc:fileLoc}} | |
| end repeat | |
| set listToShow to {} | |
| repeat with i from 1 to (length of tracksAndLocs) | |
| set cur to item i of tracksAndLocs | |
| set trk to (tr of cur) | |
| tell application "System Events" to set fname to name of file (loc of cur) | |
| set listToShow to listToShow & {"Relocate " & (name of trk) & " to " & fname} | |
| end repeat | |
| set uReq to choose from list listToShow with prompt "Click Cancel if you see any WRONG relocations" with empty selection allowed | |
| if uReq is false then error number -128 | |
| repeat with i from 1 to (length of tracksAndLocs) | |
| set cur to item i of tracksAndLocs | |
| set trk to (tr of cur) | |
| set nm to name of trk | |
| set alb to album of trk | |
| set gn to genre of trk | |
| set location of trk to (loc of cur) | |
| set name of trk to nm | |
| set album of trk to alb | |
| set genre of trk to gn | |
| end repeat | |
| end tell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment