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

Previous Page 2
  • by frankie666,

    frankie666 frankie666 Mar 11, 2014 3:02 PM in response to Limnos
    Level 1 (0 points)
    Mar 11, 2014 3:02 PM in response to Limnos

    Limnos, could you please post your final script? Could be of interest for me. Thanks.

  • by Limnos,

    Limnos Limnos Mar 11, 2014 4:53 PM in response to frankie666
    Level 9 (53,643 points)
    Mac OS X
    Mar 11, 2014 4:53 PM in response to frankie666

    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

  • by nightsystem_fr,

    nightsystem_fr nightsystem_fr Jan 20, 2016 7:45 AM in response to Limnos
    Level 1 (0 points)
    Jan 20, 2016 7:45 AM in response to Limnos

    Hello, I have a very big problem with my solution!

     

    -- Choose destination location

    choose folder with prompt "Choose a folder to copy the selected iTunes items into:"

     

    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 genre_text to genre of SomeTrack

      set track_genre to my textEchange(genre_text, "/", "-")

      if track_genre is "" then

      set track_genre to "Non Défini"

      end if

      set track_year to the year of SomeTrack

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

      if track_year is 0 then

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

      else

      set DestinationFolder to my checkForFolder(POSIX path of DestinationFolder, track_genre)

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

      end if

      -- copies file track file to new folder

      -- my copyFileToFolder(TrackLocation, destinationSubFolder)

      end if

      end repeat

    end tell

     

    to checkForFolder(fParent, fName)

      -- find or create a folder

      tell application "System Events"

      if not (exists folder fName of folder fParent) then

      -- display dialog fName

      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 alias

      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

      move fileAlias to folderPath without replacing

      end tell

    end copyFileToFolder

     

    to textEchange(leTexte, ancien, nouveau)

      set sauvTID to text item delimiters -- par politesse

      set text item delimiters to ancien

      set listeElements to text items of leTexte

      set text item delimiters to nouveau

      set reponse to listeElements as text

      set text item delimiters to sauvTID

      return reponse

    end textEchange

     

     

    It return "Duplicate name error" when the directory already exists: error "Erreur dans System Events : Nom dupliqué." number -48

    Can you HELP Me, please?

     

    Thank you so much in advance,

    Best Regards

Previous Page 2