Applescript to copy/export mp3 by album

Dear all,

I would like with applescript copy all selected album from iTunes to a USB key (to be used in my car) but I would like the mp3 are copied in specific folders (according the album name included in the tags of mp3). I have slightly modified a script found on the net (see below) but all mp3's are copied in the same folder and I dont know how to read the alcum tag and create a specific folder to copy all mp3 related to the same album. As you can see, I am new with applescript... Any idea to help me. Thanks a lot. Frankie666


+++++++++++++++++


if application "iTunes" is not running then

display alert "iTunes is not running" message "Please activate iTunes and select items."

return

end if


-- Choose destination location

choose folder with prompt "Choose a folder to copy the selected iTunes items into:" default location (path to desktop)


set DestinationFolder to the result

set {TrackLocations} to {{}}


-- Get filepaths of the selected iTunes items

tell application "iTunes" to repeat with SomeTrack in (get the selection)

try -- skip items that are not file tracks (no location)

set the end of TrackLocations to location of SomeTrack


end try

end repeat


-- Copy all selected MP3s

repeat with SomeFile in TrackLocations

tell application "Finder"

duplicate SomeFile to DestinationFolder

end tell

end repeat


tell application "Finder" to reveal DestinationFolder

iTunes-OTHER, OS X Mavericks (10.9)

Posted on Feb 12, 2014 4:38 AM

Reply
18 replies

Feb 12, 2017 3:20 PM in response to Limnos

Awesome!! thank you! I got this to work for genres below. Im trying to created a folder tree based on the comma separated values of my Genre field.

I would like the script to read the genre field and create folders based on the genre and subfolders based on the subfolders inside.

For example.

[ main genre, sub genre, sub sub genre]

the folder tree would look like.

main genre

sub genre

sub sub genre

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"

repeat with SomeTrack in (get the selection)

if class of SomeTrack is file track then

set TrackLocation to location of SomeTrack

set genreName to genre of SomeTrack



-- finds or creates a folder in the destination folder named after the genreName


set destinationSubFolder to my checkForFolder(POSIX path of DestinationFolder, genreName)


-- 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 (makenewfolderat end of folderfParentwith 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


movefileAliastofolderPath 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

Feb 12, 2014 10:00 AM in response to frankie666

I think you just need to get the album name from iTunes and create a new folder from it at the destination folder.


if application "iTunes" is not running then

display alert "iTunes is not running" message "Please activate iTunes and select items."

return

end if


-- Choose destination location

choose folder with prompt "Choose a folder to copy the selected iTunes items into:" default location (path to desktop)


set DestinationFolder to the result

set TrackLocations to {}


-- Get filepaths of the selected iTunes items

tell application "iTunes"

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(DestinationFolder, albumName)


-- copies file track file to new folder

my copyFileToFolder(TrackLocation, destinationSubFolder)

end if

end repeat

end tell


-- Copy all selected MP3s

repeat with SomeFile in TrackLocations

tell application "Finder"


duplicateSomeFiletoDestinationFolder

end tell

end repeat


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 POSIX path of (makenewfolderat end of folderfParentwith properties {name:fName})

else

set output to (POSIX path of (folder fName of folder fParent))

end if

end tell



-- returns a POSIX path

return output

end checkForFolder


to copyFileToFolder(fileAlias, folderAlias)

tell application "Finder"

copy fileAlias to folderAlias

end tell

end copyFileToFolder

Feb 12, 2014 10:31 AM in response to twtwtw

Thanks twtwtw for your prompt feedback. I got the following error... Any idea?


error "Erreur dans System Events : Il est impossible de rendre alias \"Macintosh HD:Users:frankie:Downloads:\" en type integer." number -1700 from alias "Macintosh HD:Users:frankie:Downloads:" to integer


In the following proc (existsfolderfNameoffolderfParent):

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 POSIX path of (makenewfolderat end of folderfParentwith properties {name:fName})

else

set output to (POSIX path of (folder fName of folder fParent))

end if

end tell



-- returns a POSIX path

return output

end checkForFolder


Feb 12, 2014 10:44 AM in response to frankie666

Well, I have two ideas. First is that you speak French, which probably isn't related to the problem but leaves me having to use my poorly remembered knowledge of the language to translate that error. But I think I know the problem regardless - that handler returns a POSIX path, and the Finder has an intrinsic dislike for POSIX paths. Try changing the last line of the handler to:



-- returns an alias

return POSIX fileoutput

Which ought to return a alias object that the Finder will approve of.

Feb 12, 2014 12:16 PM in response to twtwtw

You are right, I speak french... The error is not coming from POSIX, it is coming from this line:


ifnot (existsfolderfNameoffolderfParent) then


And the translation of this error is....


error "Error in System Events : Impossible to return alias \"Macintosh HD:Users:frankie:Downloads:\" type integer." number -1700 fromalias "Macintosh HD:Users:frankie:Downloads:" tointeger


Thanks

Feb 12, 2014 12:28 PM in response to frankie666

Well, you speak English far better than I speak French, so thanks for the translation. 🙂


With respect to the error, the problem (as I see now) is that we are passing an alias to the handler where the handler expects a POSIX path string. Easiest solution may be to send the handler the POSIX path it expects, like so:


set destinationSubFolder to my checkForFolder(POSIX path of DestinationFolder, albumName)

otherwise you'll need to revise or remove the folder keywords in the handler to get it to work correctly.

Feb 14, 2014 8:14 AM in response to frankie666

YeeeEEEEeeee! I made a complete newb mistake, which is truly embarrassing. The Finder command to copy files (as I've told so many other people in the past) is duplicate, not copy. facepalm...😊


I tweaked and tested the handlers I've sent you. these work on my machine.


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 (makenewfolderat end of folderfParentwith 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


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

duplicatefileAliastofolderPath without replacing

end tell

end copyFileToFolder

Feb 15, 2014 6:09 AM in response to twtwtw

Awsome, it is now working perfectly! Thanks a lot. I have slightly modify the code to create directories like "artist - album" instead of "album" and I have removed the section below which is useless (I guess) :


-- Copy all selected MP3s

repeat with SomeFile in TrackLocations

tell application "Finder"


duplicateSomeFiletoDestinationFolder

end tell

end repeat


A last question about the "checkForFolder(fParent, fName)" procedure (don't know if you call it procedure or function in applescript). What is exactly POSIX and what are the 2 "set output to..." lines?

Thanks again.

Feb 15, 2014 9:40 AM in response to frankie666

In applescript they're called "handlers". Vive le différence!


With respect to the other question, see the user tip I just created. If only you knew how often I've had to explain that POSIX path thing. 😢 The 'set output' lines catch the path (in your final version the HFS path) of the folder (be it newly created or found in the file system), so that the path can be returned in the last line.

Mar 11, 2014 2:50 PM in response to frankie666

Thanks for this discussion. I used the bits and pieces to make a working script which will move (instead of copy; change duplicate to move) files to a folder with the album name of the tracks (or folders if tracks from several albums are chosen at once). If a folder with the album name already exists in the destination folder it just adds to that one.


In my version I removed the defaulting to desktop for destination folder so it defaults to whichever folder was last used as the host folder for the album folders instead of having to change this from the desktop each time.


Checking if iTunes is running is redundant. Since the script relies upon using iTunes for track selection I can't see how you can use it without iTunes running (and my version of Applescript didn't recognize "running"). I simply put it in my iTunes scripts library and always invoke it from inside iTunes.


I added a line at the end which produces a button so I know the script has finished running.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript to copy/export mp3 by album

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.