Speeding up an AppleScript?

Hi there!

I cobbled together some code to make a handy operation that separates Jpegs from the Raw files on a SD card. I have it scripted to put the Jpegs in their own folder and it’s working how I want it to. However, the script takes a very long time when processing hundreds of files. Is there a way to speed the process up?

-Aaron

For me the “Main Folder” prompt is EOS_DIGITAL → DCIM


set main_folder to (choose folder with prompt "Choose the main folder")
tell application "Finder"
    
    
    tell application "Finder"
        set newFolder to make new folder at main_folder with properties {name:"JPEGs"}
    end tell
    
    set sub_folders to folders of main_folder
    repeat with each_folder in sub_folders
        
        move (every file of each_folder whose name extension is in {"jpg"}) to newFolder with replacing
    end repeat
end tell

You can speed up the script immensely if you don’t use the Finder to filter and move the files.

Instead use the find and mv commands of the shell

set main_folder to (choose folder with prompt "Choose the main folder" default location "EOS_DIGITAL:DCIM" as alias)
tell application "Finder" to set newFolder to make new folder at main_folder with properties {name:"JPEGs"}
set destinationFolder to POSIX path of (newFolder as text)
do shell script "/usr/bin/find " & quoted form of POSIX path of main_folder & " -type f  -name '*.jpg' -print -exec /bin/mv {} " & quoted form of destinationFolder & " \\;"

Consider that you get an error if the folder JPEGs already exists.

StefanK,

This is a millions times faster!! Thank you so much!! You have gotten me unstuck with my scripts in the past!

Cheers!

-Aaron