Max Anderson
Max Anderson

Reputation: 33

Applescript: on adding folder items to; renaming files without the loop

I created a droplet which would allow me to rename files from an input box.

on adding folder items to este_folder after receiving este_file


    display dialog "what's their name?" default answer ""

    set text_returned to text returned of the result & ".jpg"

    display dialog text_returned

    tell application "Finder"
        set the name of file este_file to text_returned
    end tell

end adding folder items to

It works fine, but it creates a loop where I have to hit cancel again to stop the script, as it thinks a new file has been added. I would like to just rename it once; and then not have the second dialog box pop up again. I have tried rerouting the file to another folder:

on adding folder items to este_folder after receiving este_file
    display dialog "what's their name?" default answer ""

        set text_returned to text returned of the result & ".jpg"

        display dialog text_returned

        tell application "Finder"
            set the name of file este_file to text_returned
        end tell
    repeat with anItem in este_file
            tell application "Finder"
                 set destFolder to "Macintosh HD:Users:maxwellanderson:Desktop:BetterinTexas" as alias
                move anItem to folder destFolder
            end tell
        end repeat
end adding folder items to 

But that doesn't work either-it doesn't process the renaming portion of the script. Any suggestions on what I should do to get rid of the second dialog box?

Upvotes: 0

Views: 382

Answers (1)

CJK
CJK

Reputation: 6092

The script is being called twice because renaming the file within the watched folder is—for all intents and purposes—like adding a new file into the folder. Therefore, it's called once when the file is actually added; and called a second time when it's renamed.

Moving the file as you suggested will work, but you have to move the file before you rename it. Therefore, shove the code that moves the file near the top of the script, then the renaming bits at the bottom.

As a side-note, I notice you have a repeat with loop to handle multiple file moves, but only one statement that handles a single file renaming. One of these is not like the other. If this watched folder received multiple files at the same time, it would most likely rename them all to the same name, thus potentially over-writing multiple files. If the watched folder only ever receives one file at a time, anyway, then the repeat with loop is redundant.

This code is modelled on yours and will handle a single file move-and-rename (but not a group of files—or, more accurately, as I stated above, it would rename multiple files to the same name, thus overwriting all but the last one in the list):

    on adding folder items to este_folder after receiving este_file

        set destFolder to POSIX file "/Users/maxwellanderson/Desktop/BetterinTexas" as alias

        set text_returned to text returned of ¬
            (display dialog "what's their name?" default answer "") ¬
                & ".jpg"

        display dialog text_returned

        tell application "Finder" to ¬
            set the name of ¬
                (move file este_file to destFolder) ¬
                    to text_returned

    end adding folder items to

If you need it to handle multiple files, then you can wrap everything from set text_returned to to text_returned in a repeat with loop as you've done in your second code block. This will sequentially bring up dialog boxes—one per file—and move/rename the files accordingly.

If you have any questions, or need clarification, leave a comment and I'll get back to you.

Upvotes: 2

Related Questions