views:

59

answers:

1

I'm trying to make a script that will move every fourth item in a folder to a destination folder. So far I have:

tell application "Finder"
    set originFolder to folder choose folder
    set dest1Folder to folder choose folder
    set dest2Folder to folder choose folder
    set quarter to 1
    repeat with i from 1 to count of items in originFolder
        if quarter is 1 then
            move item i of originFolder to dest1Folder
        else
            move item i of originFolder to dest2Folder

        end if
        if quarter is 4 then
            set quarter to 0
        end if
        set quarter to quarter + 1
    end repeat
end tell

but this gives me

error "Finder got an error: Expected a reference." number -1727

Why isn't this this working and what will work?

+2  A: 

There are a couple of problems in you script:

Upon setting up the choose folder variables you have to put parentheses around choose folder in order to force the Finder to convert the alias returned by choose folder to an object specifier:

set originFolder to folder (choose folder)

The loop that moves the items will terminate with an index out of bounds error because the number of items in the originFolder changes upon each iteration through the loop, as items are being moved.

A better solution is to first compute the indexes of the files that should be moved and then have the Finder perform the move operation in one step:

tell application "Finder"
    set originFolder to folder (choose folder)
    set destFolder to folder (choose folder)
    set indexes to {}
    repeat with i from 4 to (count of items in originFolder) by 4
        copy i to end of indexes
    end repeat
    move (every item of originFolder whose index is in indexes) to destFolder
end tell
sakra