-1

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

3
  • 1
    Using POSIX paths with the Finder will also throw an error. Commented Feb 11, 2024 at 3:19
  • What would you recommend as an alternative? I've found it doesn't like when I select an iCloud location, the POSIX path seemed to solve this. However I am a total newbie to coding - as you may have guessed lol Commented Feb 12, 2024 at 8:57
  • System Events knows about POSIX paths, but for the Finder the easiest would be to just use the alias that choose folder returns instead of coercing it to a POSIX path. If you need a POSIX path somewhere else for something like do shell script, you can coerce it then. See Aliases and Files in the AppleScript Language Guide. Commented Feb 12, 2024 at 15:04

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That works great, however as you mentioned I'm having issues with the rest of the script, I can't get the script to move the file to the said folder or rename the file and keep the original extension. It is worth noting that the script will activate on any file that is placed in downloads/downloaded, so keeping the original extension is paramount
I answered the question you asked. To ask a new question, click the Ask Question button at the top right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.