2

AppleScript on macOS Mojave and can't work out why this script gives the expected "TRUE" on the first folder, but instead of "FALSE" on the second folder it aborts with the error message:

[error "Finder got an error: AppleEvent handler failed." number -10000 from folder (file "Felix HD:Users:Billy:TESTING:FOLDER2"]

tell application "Finder"

    set targetFolder to POSIX file "/Users/Billy/TESTING/FOLDER1" -- Existing folder

    if exists folder targetFolder then
        say "TRUE"
    else
        say "FALSE"
    end if

    set targetFolder to POSIX file "/Users/Billy/TESTING/FOLDER2" -- Non-existing folder

    if exists folder targetFolder then
        say "TRUE"
    else
        say "FALSE"
    end if

end tell

1 Answer 1

1

I think I found the answer: removed the word 'folder' from the if statements and now it works!

If anyone could confirm this is the proper syntax that would be great.

tell application "Finder"

    set targetFolder to POSIX file "/Users/Billy/TESTING/FOLDER1" -- Existing folder

    if exists targetFolder then
        say "TRUE" -- result!!
    else
        say "FALSE"
    end if

    set targetFolder to POSIX file "/Users/Billy/TESTING/FOLDER2" -- Non-existing folder

    if exists targetFolder then
        say "TRUE"
    else
        say "FALSE" -- result!!
    end if

end tell
7
  • ...and maybe someone could tell me why the "end tell" won't stay pasted in the code box!
    – wchundak
    Dec 30, 2018 at 1:53
  • 1
    Code formatting is 4 spaces indentation on all lines, not just the first, see edit
    – grg
    Dec 30, 2018 at 2:01
  • Well done. Yes, removing the folder specifier is the correct solution, and your syntax is good. You can also say if the targetFolder exists then as a more natural-language sounding alternative, but both forms are equally sound. Preferentially, I'd use System Events to test for a file's (or folder's) existence generally, but I suppose it makes little difference, except that System Events can handle posix paths well. The other way to test for existence is a bit of a trick, and simply involves coercing to an alias inside a try block.
    – CJK
    Dec 30, 2018 at 2:50
  • CJK - thanks for the review! I'm trying a System Events version now, but having a different problem using "delete every item of folder targetFolder" which is my real purpose for this script and works in the Finder version.
    – wchundak
    Dec 30, 2018 at 3:13
  • Tip: Tag users with @ and click on the name that appears in order to ensure they are notified of a reply (which I wasn't). Regarding your second problem, I'd recommend posting a new question with your ultimate objective and the issues you're encountering. Be warned with System Events that delete actually deletes a file or folder, without sending it to the trash.
    – CJK
    Dec 30, 2018 at 4:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .