Applescript folder actions

59 Views Asked by At

Anyone know why my buttons aren't working for a folder action in applscript? This is designed to launch from Automator and keep the Downloads folder clean with options to rename duplicates then move the renamed file or move duplicate to bin.

However clicking any of the buttons causes the script to stop without the file being moved.

Any tips on how to correct this?

on run {input, parameters}
    tell application "Finder"
        set fileToMove to item 1 of input
        set fileName to name of fileToMove
        set chosenFolder to POSIX path of (choose folder with prompt "Select a folder to move " & fileName & " ")
        try
            move fileToMove to folder chosenFolder
            -- Make sound play only on success
        on error
            -- Display notification with title "File Not Moved" subtitle "File with that name already exists at that location"
            set buttonReturned to display alert "Duplicate File Found" message "Would you like to move " & fileName & " to the Bin or Keep & Rename?" buttons {"Move Duplicate to Bin", "Keep & Rename", "Cancel"} default button "Keep & Rename" cancel button "Cancel"
            if buttonReturned is "Move Duplicate to Bin" then
                delete fileToMove
            else if buttonReturned is "Keep & Rename" then
                set newName to the text returned of (display dialog "Set New File name for: " & fileName & "" default answer "")
                set the name of fileToMove to newName
                move fileToMove to folder chosenFolder
            end if
        end try
    end tell
    
    do shell script "afplay /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume Mount.aif'"
    open currentWindow
end run
 

When I click the respective button, I want the action to occur

ie when button move to bin, it moves the original to the bin when button button keep and rename is clicked it should ask for a new name, then rename the file to the input name and then move the renamed file

1

There are 1 best solutions below

2
matt On

This construct is incorrect:

set buttonReturned to display alert "Duplicate File Found" message "Would you like to move " & fileName & " to the Bin or Keep & Rename?" buttons {"Move Duplicate to Bin", "Keep & Rename", "Cancel"} default button "Keep & Rename" cancel button "Cancel"
if buttonReturned is "Move Duplicate to Bin" then

That if will never be true, because the output of display alert is not text; it is a dictionary containing a button returned element. You need to ask that dictionary for that button returned element.

The first line should therefore look like this:

 set buttonReturned to button returned of (display alert "Duplicate File Found" message "Would you like to move " & "what" & " to the Bin or Keep & Rename?" buttons {"Move Duplicate to Bin", "Keep & Rename", "Cancel"} default button "Keep & Rename" cancel button "Cancel")

Actually, to avoid the parentheses, I would write it like this:

display alert "Duplicate File Found" message "Would you like to move " & "what" & " to the Bin or Keep & Rename?" buttons {"Move Duplicate to Bin", "Keep & Rename", "Cancel"} default button "Keep & Rename" cancel button "Cancel"
set buttonReturned to button returned of result

Now your if tests will work. This will not solve all the issues with your script, but it will solve the one you asked about.