Let’s say I want to locate all files of type “.mp3” within a home folder and move them somewhere.
So, if I start with:
do shell script "find /Users/anyone -name "*.mp3""
I get a result that I’m not sure how to manipulate. I get this:
“/Users/anyone/Music/Bob Dylan/Blood On The Tracks/Buckets Of Rain.mp3
/Users/anyone/Music/Bob Dylan/Blood On The Tracks/Idiot Wind.mp3
/Users/anyone/Music/Bob Dylan/Blood On The Tracks/If You See Her, Say Hello.mp3
/Users/anyone/Music/Bob Dylan/Blood On The Tracks/Lily, Rosemary And The Jack Of Hearts.mp3”
I want to be able to move each .mp3 file to a folder I choose earlier in the script. I’m not quite sure how to tell AppleScript or a shell script to move (not copy) those files.
There are faster ways of doing this, as Camelot rightly demonstrates, and you may get a permissions error if this tries to search your hidden trash folders but this should work:
set target_folder to (choose folder with prompt "Where should the files be moved:") as alias
set the_files to paragraphs of (do shell script "find ~/ -name "*.mp3"")
set the_errors to {}
tell application "Finder"
repeat with this_file in the_files
try
move ((contents of this_file) as POSIX file) to target_folder
on error the_error
set end of the_errors to the_error
end try
end repeat
end tell
if the_errors = {} then
display dialog "All done." buttons {"OK"} with icon 1 giving up after 10
else
set AppleScript's text item delimiters to return
set the_errors to the_errors as string
set AppleScript's text item delimiters to ""
display dialog "All done with errors:" & return & the_errors buttons {"OK"} with icon 2
end if