iMax
iMax

Reputation: 587

AppleScript / FolderAction - Rename files when dropped in folder > Endless Loop

I am trying to do the following with a FolderAction and AppleScript:

Everytime I drop a file on a specific folder it should be renamed and then moved into another folder.

The problem is I get something like an infinite loop (I think) because when the file gets renamed the folder assumes that there is a new file in the folder and so on.

I really don't know how to avoid this and stop the endless loop. Here ist my script:

global newName
set newName to ""

on adding folder items to theAttachedFolder after receiving theNewItems
    -- Get the name of the attached folder
    tell application "Finder"
        set theName to name of theAttachedFolder

        -- Count the new items
        set theCount to length of theNewItems

        -- Display an alert indicating that the new items were received
        activate

        -- Loop through the newly detected items
        repeat with anItem in theNewItems

            set oldFileName to name of anItem

            -- Rename the file
            set the name of anItem to "NewFile" & oldFileName

            -- Move the file to other folder
            move anItem to "Macintosh HD:Users:blabla:blabla"

        end repeat
    end tell

    tell application "Finder"
        delete files of folder "Macintosh HD:Users:user:thisfolder
    end tell

end adding folder items to

Upvotes: 0

Views: 494

Answers (1)

vadian
vadian

Reputation: 285069

You are right, renaming the file triggers the folder action again.

The solution is to change the order: First move then rename.

        -- Move the file to other folder
        set movedItem to move anItem to "Macintosh HD:Users:blabla:blabla"

       -- Rename the file
        set the name of movedItem to "NewFile" & oldFileName

Upvotes: 1

Related Questions