(* Shuffle Wannabe Applescript folder action for adding the spoken title of the song to the front of the audio file. Merges multiple files. By Benno Kruit bennokruit.nl Please keep my name here *) on adding folder items to this_folder after receiving added_items -- Ask to merge into playlist when adding multiple items set readname to "" if (number of items in added_items) > 1 then set mergeq to button returned of (display dialog "You added " & number of items in added_items & " tracks. Do you want to merge them into a playlist?" buttons {"Merge", "No thanks"}) if mergeq is "Merge" then set readname to text returned of (display dialog "Name of playlist:" default answer "Playlist name") end if end if if readname is "" then repeat with current_file in added_items -- Info for file set this_info to info for current_file set filename to the name of this_info set filepath to POSIX path of current_file set extension to do shell script "echo \"" & filepath & "\" |awk -F . '{print $NF}' |tr \"[:upper:]\" \"[:lower:]\" " set readname to do shell script "basename \"" & filename & "\" ." & extension -- The 'shufflized' files have a + at the back; -- otherwise the folder action would call itself and iterate. if last character of readname is not "+" then set oldname to readname set dir to do shell script "dirname \"" & filepath & "\"" set extension_list to {"m4a", "aif", "aiff", "mp3", "wav"} set AppleScript's text item delimiters to "" -- check file extension if extension is in extension_list then -- convert file if (extension is not "mp3") then set filepath to convert_to_mp3(current_file) end if set current_file to alias POSIX file filepath set this_info to info for current_file set filename to the name of this_info set extension to "mp3" -- get name without numbers set nonumbername to do shell script "echo \"" & filename & "\" | sed -e 's/^[1234567890]* //g'" set readname to do shell script "basename \"" & nonumbername & "\" ." & extension -- read and add do shell script "mkdir \"/tmp/songname\"" set readfile to (("/tmp/songname/!" & readname & ".aiff") as string) do shell script "say -v Alex -o \"" & readfile & "\" \"" & readname & "\"" convert_to_mp3(POSIX file readfile) do shell script "cp \"" & filepath & "\" \"/tmp/songname/" & filename & "\"" -- The plus sign is to prevent iteration do shell script "cd \"/tmp/songname\"; cat * > \"" & dir & "/" & oldname & "+.mp3\"; rm *" -- clean up do shell script "rmdir /tmp/songname; rm " & quoted form of filepath end if end if end repeat else set extension_list to {"m4a", "aif", "aiff", "mp3", "wav"} do shell script "mkdir \"/tmp/songname\"" repeat with current_file in added_items -- Info for file set filepath to POSIX path of current_file set extension to do shell script "echo \"" & filepath & "\" |awk -F . '{print $NF}' |tr \"[:upper:]\" \"[:lower:]\" " if extension is in extension_list then -- convert file if (extension is not "mp3") then set filepath to convert_to_mp3(current_file) end if set this_info to info for POSIX file filepath set filename to the name of this_info do shell script "cp \"" & filepath & "\" \"/tmp/songname/" & filename & "\"; rm " & quoted form of filepath end if end repeat set readfile to (("/tmp/songname/!" & readname & ".aiff") as string) do shell script "say -v Alex -o \"" & readfile & "\" \"" & readname & "\"" convert_to_mp3(POSIX file readfile) display dialog "control" -- The plus sign is to prevent iteration do shell script "cd \"/tmp/songname\"; cat * > \"" & (POSIX path of this_folder) & "/" & readname & "+.mp3\"; rm *" do shell script "rmdir /tmp/songname;" end if end adding folder items to on convert_to_mp3(fs) (* "Drop A Few My Way" for iTunes written by Doug Adams dougadams@mac.com http://dougscripts.com/itunes/ Modified by Benno Kruit *) set extension_list to {"m4a", "aif", "aiff", "wav"} set old_extension to do shell script "echo \"" & POSIX path of fs & "\" |awk -F . '{print $NF}' |tr \"[:upper:]\" \"[:lower:]\" " if old_extension is in extension_list then -- choose encoder tell application "iTunes" launch set backup_encoder to name of current encoder end tell set selected_encoder to "MP3 Encoder" set new_extension to ".mp3" set encoder_type to "MP3" set posixfs to POSIX path of fs set dirname to do shell script "dirname \"" & posixfs & "\"" set uD to (alias (POSIX file dirname)) as text -- ok, committed -- -- NOW load encoder tell application "iTunes" set current encoder to encoder selected_encoder end tell -- ok, do the dropped files copy fs to file_before_added_to_iTunes try -- skip bad files with timeout of 30000 seconds -- plenty of time -- Fix filename set finfo to get info for file_before_added_to_iTunes set new_filename_and_extension to ((text 1 thru -((length of name extension of finfo) + 2) of (name of finfo)) & new_extension) -- Add to itunes tell application "iTunes" try set orig_Track to (add file_before_added_to_iTunes) set file_after_added_to_iTunes to (location of orig_Track) on error m number n --log (m & n) -- debugging set err_mess to errTxt end try end tell -- convert track tell application "iTunes" try -- convert the original file set new_Track to item 1 of (convert file_after_added_to_iTunes) -- get path to new file set file_after_converted to location of new_Track -- delete original track from iTunes delete orig_Track -- delete the new converted track from iTunes delete new_Track -- move new file to uD (Desktop or user selected location) do shell script "mv " & (quoted form of POSIX path of file_after_converted) & " " & (quoted form of POSIX path of (uD & new_filename_and_extension)) -- the original file that was dropped -- may have been copied to the iTunes -- Music folder. Delete that copy. -- Otherwise, if the two filepaths are the same -- then don't delete. -- Never delete the original dropped file if (file_before_added_to_iTunes as text) is not (file_after_added_to_iTunes as text) then -- delete file_after_added_to_iTunes do shell script "rm " & (quoted form of POSIX path of file_after_added_to_iTunes) end if on error m number n -- log (m & return & n) -- debugging set err_mess to errTxt end try end tell end timeout on error m number n -- log (m & return & n) -- restore_encoder() end try do shell script "rm " & quoted form of posixfs -- restore encoder tell application "iTunes" to set current encoder to encoder backup_encoder return (POSIX path of (uD & new_filename_and_extension)) end if end convert_to_mp3