Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
Apple Computing Products:
macOS - Operating System
Script to duplicate Apple Lossless iTunes library as MP3 files
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="nick_harambee" data-source="post: 1116970" data-attributes="member: 27767"><p>Hi,</p><p></p><p>I have written the applescript below to duplicate my Apple Lossless iTunes library as a MP3 Library. I wanted the script to be able to do the following things:</p><p></p><p>1. For all Apple Lossless tracks in iTunes Library (at the moment the script is just set to work with a selection for testing purposes) check if track already exists in MP3 folder with more recent modification date and:</p><p>a If yes, do nothing</p><p>b If same track with earlier modification date convert and replace track (so that tag changes are transferred)</p><p>c If track doesn't exist convert track.</p><p></p><p>2. If there are any MP3 files in the target folder that aren't in the source folder (i.e. the iTunes Music Folder), delete these files. This is for the scenario when I delete files from my iTunes Library that I no longer want, and I want the equivalent MP3 file to be deleted as well. So once the script has finished running there should be an exact match between the Apple Lossless library and the MP3 folder.</p><p></p><p>The script below does everything I want it to do with the following caveats:</p><p></p><p>1. I am not sure how to go about deleting the MP3 files as in part 2. I guess it would have to be an additional section of script which compares each file by name in the folder/subfolder in the iTunes Music Library with the equivalent folders in the target music folder, deleting any files found in the target music folder that don't exist in the iTunes Music folders, but I'm not sure if this is the best/only method, and how I would go about scripting this.</p><p></p><p>2. Because I need to specify the conversion twice (once if no file exists in target folder and once if it does but with an older modification date) I am wondering about using a subroutine method here. I tried doing so, but no conversions happened even though some should have happened. I wonder if this is because the relevant variables aren't picked up properly by the script when it's a subroutine. I can leave things as they are, just thought it would be good to make the script neater.</p><p></p><p>[CODE]tell application "iTunes"</p><p> activate</p><p> </p><p> set sel to selection</p><p> set encoderBackup to name of current encoder</p><p> set this_folder to "Macintosh HD:Users:nick:Desktop:music:" as text</p><p> set current encoder to encoder "MP3 Encoder"</p><p> </p><p> </p><p> repeat with this_track in sel</p><p> set trackComposer to this_track's composer</p><p> set trackAlbum to this_track's album</p><p> set trackName to this_track's name</p><p> tell application "Finder"</p><p> </p><p> set path1 to "Macintosh HD:Users:nick:Desktop:music:" & trackComposer as text</p><p> </p><p> if folder path1 exists then</p><p> {}</p><p> else</p><p> make new folder at this_folder with properties {name:trackComposer}</p><p> end if</p><p> </p><p> set path2 to this_folder & trackComposer & ":" & trackAlbum as text</p><p> </p><p> if folder path2 exists then</p><p> {}</p><p> else</p><p> make new folder at path1 with properties {name:trackAlbum}</p><p> end if</p><p> </p><p> if exists file (path2 & ":" & trackName & ".mp3") then</p><p> set file2 to (path2 & ":" & trackName & ".mp3") as alias</p><p> set modDate to modification date of file2</p><p> set modDate2 to this_track's modification date</p><p> if modDate is greater than modDate2 then</p><p> {}</p><p> else</p><p> tell application "iTunes"</p><p> try -- skip on failure</p><p> set new_track to item 1 of (convert this_track)</p><p> set loc to new_track's location</p><p> set dbid to new_track's database ID</p><p> delete artworks of new_track</p><p> </p><p> -- move the file to new location</p><p> do shell script "mv " & (quoted form of POSIX path of loc) & " " & (quoted form of POSIX path of path2 as string)</p><p> </p><p> -- delete the track</p><p> delete new_track</p><p> end try</p><p> end tell</p><p> </p><p> end if</p><p> else</p><p> tell application "iTunes"</p><p> try -- skip on failure</p><p> set new_track to item 1 of (convert this_track)</p><p> set loc to new_track's location</p><p> set dbid to new_track's database ID</p><p> delete artworks of new_track</p><p> </p><p> -- move the file to new location</p><p> do shell script "mv " & (quoted form of POSIX path of loc) & " " & (quoted form of POSIX path of path2 as string)</p><p> </p><p> -- delete the track</p><p> delete new_track</p><p> end try</p><p> end tell</p><p> </p><p> end if</p><p> end tell</p><p> </p><p> end repeat</p><p> </p><p> set current encoder to encoder encoderBackup</p><p> display dialog "done"</p><p>end tell[/CODE]</p><p></p><p>Can anyone help with either points?</p><p></p><p>Thanks</p><p></p><p>Nick</p></blockquote><p></p>
[QUOTE="nick_harambee, post: 1116970, member: 27767"] Hi, I have written the applescript below to duplicate my Apple Lossless iTunes library as a MP3 Library. I wanted the script to be able to do the following things: 1. For all Apple Lossless tracks in iTunes Library (at the moment the script is just set to work with a selection for testing purposes) check if track already exists in MP3 folder with more recent modification date and: a If yes, do nothing b If same track with earlier modification date convert and replace track (so that tag changes are transferred) c If track doesn't exist convert track. 2. If there are any MP3 files in the target folder that aren't in the source folder (i.e. the iTunes Music Folder), delete these files. This is for the scenario when I delete files from my iTunes Library that I no longer want, and I want the equivalent MP3 file to be deleted as well. So once the script has finished running there should be an exact match between the Apple Lossless library and the MP3 folder. The script below does everything I want it to do with the following caveats: 1. I am not sure how to go about deleting the MP3 files as in part 2. I guess it would have to be an additional section of script which compares each file by name in the folder/subfolder in the iTunes Music Library with the equivalent folders in the target music folder, deleting any files found in the target music folder that don't exist in the iTunes Music folders, but I'm not sure if this is the best/only method, and how I would go about scripting this. 2. Because I need to specify the conversion twice (once if no file exists in target folder and once if it does but with an older modification date) I am wondering about using a subroutine method here. I tried doing so, but no conversions happened even though some should have happened. I wonder if this is because the relevant variables aren't picked up properly by the script when it's a subroutine. I can leave things as they are, just thought it would be good to make the script neater. [CODE]tell application "iTunes" activate set sel to selection set encoderBackup to name of current encoder set this_folder to "Macintosh HD:Users:nick:Desktop:music:" as text set current encoder to encoder "MP3 Encoder" repeat with this_track in sel set trackComposer to this_track's composer set trackAlbum to this_track's album set trackName to this_track's name tell application "Finder" set path1 to "Macintosh HD:Users:nick:Desktop:music:" & trackComposer as text if folder path1 exists then {} else make new folder at this_folder with properties {name:trackComposer} end if set path2 to this_folder & trackComposer & ":" & trackAlbum as text if folder path2 exists then {} else make new folder at path1 with properties {name:trackAlbum} end if if exists file (path2 & ":" & trackName & ".mp3") then set file2 to (path2 & ":" & trackName & ".mp3") as alias set modDate to modification date of file2 set modDate2 to this_track's modification date if modDate is greater than modDate2 then {} else tell application "iTunes" try -- skip on failure set new_track to item 1 of (convert this_track) set loc to new_track's location set dbid to new_track's database ID delete artworks of new_track -- move the file to new location do shell script "mv " & (quoted form of POSIX path of loc) & " " & (quoted form of POSIX path of path2 as string) -- delete the track delete new_track end try end tell end if else tell application "iTunes" try -- skip on failure set new_track to item 1 of (convert this_track) set loc to new_track's location set dbid to new_track's database ID delete artworks of new_track -- move the file to new location do shell script "mv " & (quoted form of POSIX path of loc) & " " & (quoted form of POSIX path of path2 as string) -- delete the track delete new_track end try end tell end if end tell end repeat set current encoder to encoder encoderBackup display dialog "done" end tell[/CODE] Can anyone help with either points? Thanks Nick [/QUOTE]
Verification
Post reply
Forums
Apple Computing Products:
macOS - Operating System
Script to duplicate Apple Lossless iTunes library as MP3 files
Top