Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
up vote 2 down vote accepted

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))
share|improve this answer
1  
Hmm, 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 Sep 25 '12 at 11:50
    
I would be interested in seeing the results. I have edited the script to include a timer for you. – adayzdone Sep 25 '12 at 11:58
    
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. – user495470 Sep 25 '12 at 12:03
    
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 Sep 25 '12 at 12:07
    
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 Sep 25 '12 at 12:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .