Need help optimizing an Applescript

Discussion in 'Mac Programming' started by landoncox, Sep 1, 2015.

  1. landoncox macrumors newbie

    Sep 1, 2015
    Fort Worth, Texas
    I'm very new to scripting but thanks to google and forums like this, I was able to come up with a script that does exactly what I need, the only problem is that it is pretty slow and was hoping someone could help me optimize it.

    I manage and organize thousands of files that have multiple versions of the same content. The script currently looks through all the selected files, and moves them all into a folder based on the last three numbers of the filename, that way all the different versions of the same content are collected in a single folder. Again, script works great, but was wondering if there was anything i could do to speed it up. Thanks in advance!

    on run {input, parameters} -- create folders from file names and move
        set output to {} -- this will be a list of the moved files
        repeat with anItem in the input -- step through each item in the input
            set {theContainer, theName, theExtension} to (getTheNames from anItem)
                set destination to (makeNewFolder for theName at theContainer)
                tell application "Finder"
                    move anItem to destination
                    set the end of the output to the result as alias -- success
                end tell
            on error errorMessage -- duplicate name, permissions, etc
                log errorMessage
                # handle errors if desired - just skip for now
            end try
        end repeat
        return the output -- pass on the results to following actions
    end run
    to getTheNames from someItem -- get a container, name, and extension from a file item
        tell application "System Events" to tell disk item (someItem as text)
            set theContainer to the path of the container
            set {theName, theExtension} to {name, name extension}
        end tell
        if theExtension is not "" then
            set theName to text -7 thru -((count theExtension) + 6) of theName -- just the name part
            set theExtension to "." & theExtension
        end if
        return {theContainer, theName, theExtension}
    end getTheNames
    to makeNewFolder for theChild at theParent -- make a new child folder at the parent location if it doesn't already exist
        set theParent to theParent as text
        if theParent begins with "/" then set theParent to theParent as POSIX file as text
            return (theParent & theChild) as alias
        on error errorMessage -- no folder
            log errorMessage
            tell application "Finder" to make new folder at theParent with properties {name:theChild}
            return the result as alias
        end try
    end makeNewFolder
  2. neutrino23 macrumors 68000

    Feb 14, 2003
    SF Bay area
    I think that having the Finder test thousands of times whether folders exist is what is taking so much time. AS itself is really fast, at least as of Mavericks. Just recently I found that a script that used to work no longer worked because it was too fast. I needed to add a small delay at one point to let the app and the Finder get in sync.

    It would be more work, but you could run through the input list once and create a list of the versions that need folders created for them (probably a short list). Testing if a short string like "004" belongs to a list is hugely faster than having the Finder go out and check if folder "004" exists and trapping the error if it doesn't.

    Then test if those few folders described by the list exist or need to be created then run another loop to actually move the files.

    My two cents.

Share This Page