Applescript Woefully Slow Finder.app Set Comment

2

This is the script:

set d to do shell script "date +%d-%m-%Y"
tell application "Finder"
    set dir to POSIX file ("/Volumes/Xsan/PathTo/Folder") as alias
    repeat with f in entire contents of dir
        if comment of f does not start with "Archived" then
            set comment of f to "Archived " & d
            set label index of f to 2
        end if
    end repeat
end tell

My problem is I am running this on a folder with 1000s of images and folders. Finder goes to around 90% CPU usage and it takes roughly 40 seconds PER FILE/FOLDER to set the comment and the label.

Is there some optimisation that anyone can suggest. Or alternatively, an alteration to the code that will allow a 100% Bash script implementation of this task? (If that helps with speed).

I feel there might be something in the "entire contents" command that is messing things up.

Like before it goes to make a change on a given file or folder, it is checking for tags of "Archived" on ALL of the files again after making a single change. I thought this would be cached in memory at the start.

Would love any ideas you may have!

Cheers,

James

edit: The system is Snow Leopard Server 10.6.8, Xserve2,1

James

Posted 2012-09-25T05:44:15.540

Reputation: 33

Answers

2

Try this:

    set time1 to do shell script "perl -e 'use Time::HiRes qw(time); print time'"

set d to do shell script "date +%d-%m-%Y"    
    tell application "Finder"
        set dir to POSIX file "/Volumes/Xsan/PathTo/Folder" as alias
        set eContents to entire contents of dir
        repeat with f in my eContents
            if comment of f does not start with "Archived" then
                set comment of f to "Archived " & d
                set label index of f to 2
            end if
        end repeat
    end tell

    set time2 to do shell script "perl -e 'use Time::HiRes qw(time); print time'"
    set millisec to (round ((time2 - time1) * 1000))

or

set time1 to do shell script "perl -e 'use Time::HiRes qw(time); print time'"

set d to do shell script "date +%d-%m-%Y"
tell application "Finder"
    set dir to POSIX file "/Volumes/Xsan/PathTo/Folder" as alias
    set eContents to every file of (entire contents of dir) whose comment does not start with "Archived"
    repeat with f in my eContents
        set comment of f to "Archived " & d
        set label index of f to 2
    end repeat
end tell

set time2 to do shell script "perl -e 'use Time::HiRes qw(time); print time'"
set millisec to (round ((time2 - time1) * 1000))

adayzdone

Posted 2012-09-25T05:44:15.540

Reputation: 592

1Hmm, I've run a little test on my laptop (on duplicate folder structures/files), and there doesn't seem to be a difference in overall speed. With that said, I am running this test on a MacBook Air running Mountain Lion. So there are so many variables as to not rule this solution out. – James – 2012-09-25T11:50:39.057

I would be interested in seeing the results. I have edited the script to include a timer for you. – adayzdone – 2012-09-25T11:58:52.810

I didn't think it would make a difference, but repeat with f in entire contents of dir took about twice as long for me. – Lri – 2012-09-25T12:03:33.963

That is why I assigned entire contents to a variable so the loop is not getting it every time. I also used "my" to include a script object. – adayzdone – 2012-09-25T12:07:05.017

Indeed it looks like you are right! The version with eContents variable finished in 7717 Milliseconds compared to the original code I posted, which took 17813 Milliseconds. A vast improvement! I will test this out on those folders with all the images at work tomorrow morning.

One thing: assuming that there are already files/folders in a subdirectory marked with "Archived" the eContents will already have those stored as "not needing adjustment" before it runs the set comments command right? – James – 2012-09-25T12:26:47.407

The test, (comment of f does not start with "Archived") is included within the repeat loop. I have edited the answer to include a version which filters the files first. – adayzdone – 2012-09-25T13:23:42.563