frankie666

Q: 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, OS X Mavericks (10.9)

Posted on Feb 12, 2014 4:38 AM

Close

Q: Applescript to copy/export mp3 by album

  • All replies
  • Helpful answers

Page 1 Next
  • by twtwtw,Helpful

    twtwtw twtwtw Feb 12, 2014 10:00 AM in response to frankie666
    Level 5 (4,935 points)
    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"

      duplicate SomeFile to DestinationFolder

              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 (make new folder at end of folder fParent with 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

  • by frankie666,

    frankie666 frankie666 Feb 12, 2014 10:31 AM in response to twtwtw
    Level 1 (0 points)
    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 (exists folder fName of folder fParent):


    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 (make new folder at end of folder fParentwith 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

     


  • by twtwtw,

    twtwtw twtwtw Feb 12, 2014 10:44 AM in response to frankie666
    Level 5 (4,935 points)
    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 file output


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

  • by frankie666,

    frankie666 frankie666 Feb 12, 2014 12:16 PM in response to twtwtw
    Level 1 (0 points)
    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:

     

    if not (exists folder fName of folder fParent) 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 from alias "Macintosh HD:Users:frankie:Downloads:" to integer

     

    Thanks

  • by twtwtw,

    twtwtw twtwtw Feb 12, 2014 12:28 PM in response to frankie666
    Level 5 (4,935 points)
    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.

  • by Pierre L.,

    Pierre L. Pierre L. Feb 13, 2014 6:44 AM in response to frankie666
    Level 5 (4,484 points)
    Feb 13, 2014 6:44 AM in response to frankie666

    You might have a look at this thread.

  • by Pierre L.,

    Pierre L. Pierre L. Feb 13, 2014 10:55 AM in response to Pierre L.
    Level 5 (4,484 points)
    Feb 13, 2014 10:55 AM in response to Pierre L.

    Sorry… I was under the impression that you were talking about iPhoto albums, not iTunes albums.  

  • by Limnos,

    Limnos Limnos Feb 14, 2014 6:24 AM in response to frankie666
    Level 9 (53,643 points)
    Mac OS X
    Feb 14, 2014 6:24 AM in response to frankie666

    Please keep us updated if you resolve this.  I have been looking for a means to move selected files for tracks in an iTunes album to a folder labeled with the album name in a manually managed iTunes library and yours seems to be very close to what I need.

     

    Thanks!

  • by frankie666,

    frankie666 frankie666 Feb 14, 2014 7:17 AM in response to twtwtw
    Level 1 (0 points)
    Feb 14, 2014 7:17 AM in response to twtwtw

    Thanks Twtwtw, your last proposal solves the error. Now it çreates all folders which is fine but no file is transfered...

  • by twtwtw,

    twtwtw twtwtw Feb 14, 2014 8:14 AM in response to frankie666
    Level 5 (4,935 points)
    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 (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

     

    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

                        duplicate fileAlias to folderPath without replacing

              end tell

    end copyFileToFolder

  • by frankie666,

    frankie666 frankie666 Feb 15, 2014 6:09 AM in response to twtwtw
    Level 1 (0 points)
    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"

      duplicate SomeFile to DestinationFolder

              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.

  • by twtwtw,Solvedanswer

    twtwtw twtwtw Feb 15, 2014 9:40 AM in response to frankie666
    Level 5 (4,935 points)
    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.

  • by frankie666,

    frankie666 frankie666 Feb 21, 2014 9:30 PM in response to twtwtw
    Level 1 (0 points)
    Feb 21, 2014 9:30 PM in response to twtwtw

    Thanks a lot again, very helpful!

  • by Limnos,

    Limnos Limnos Mar 11, 2014 2:50 PM in response to frankie666
    Level 9 (53,643 points)
    Mac OS X
    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.

Page 1 Next