MacOS – AppleScript: ‘exists’ returns true every time

applescriptmacos

On every exists in AppleScript the result is true. For example:

set a to (POSIX path of (((path to home folder from user domain) as text) & "skfhshfkh:" & "hfjhsfhsj:"))
if exists a then
    display dialog "AppleScript wrongly returns true ..."
end if

The result is

tell current application
    path to home folder from user domain
        --> alias "Macintosh HD:Users:[User]:"
end tell
tell application "Script Editor"
    exists "/Users/[User]/skfhshfkh/hfjhsfhsj/"
        --> true
    display dialog "AppleScript wrongly returns true ..."
        --> {button returned:"OK"}
end tell
Result:
{button returned:"OK"}

This path does not exist so where is the mistake? Is it my mistake or is it a bug in AppleScript?

Thanks for any help.

Best Answer

I am by no stretch of the imagination an Applescript expert, but this is what I have gleaned from my own fumbling over the years.
I'd appreciate corrections if there are any inaccuracies, or better methods.. but with some 'why it works' for newbies ;-)

What you're actually doing is asking Script Editor if the string "/Users/[User]/skfhshfkh/hfjhsfhsj/" exists… which it does, you just told it that it did ;-)
Script Editor doesn't know what a file is, nor how to look for one, so it's treating the string as a string not a path.

The simplest way to do this is use either the Finder or System Events which have their own 'exists' routine.

In this first example, you can input as either POSIX file or just file, & adjust your syntax accordingly.

So, this should work, for a file…

set msg to "No sign of it"
tell application "Finder"    
    --if exists POSIX file "/volumes/MacintoshHD/Users/[user]/Desktop/testFile.rtf" then
    --or
    --if exists file ((path to home folder as text) & "Desktop:" & "testFile.rtf") then
    --or
    --if exists file "MacintoshHD:Users:[user]:Desktop:testFile.rtf" then
    --but NOT
    --if exists POSIX file ((path to home folder as text) & "Desktop:" & "testFile.rtf") then
        set msg to "Yeah, found it!"
    end if
end tell

display dialog msg

Displaying the dialog outside the Finder tell prevents Finder coming to the front to just to present it.

Alternatively, you could set your path outside of the exists function itself.
This would be my preferred method, & uses System Events instead of Finder...

set theFile to ((path to home folder as text) & "Desktop:" & "testFile.rtf")
set msg to "No sign of it."

tell application "System Events"
    if exists file theFile then
        set msg to "Yeah, found it!"
    end if
end tell

display dialog msg

Note that using System Events, you must put the dialog outside the tell, as System Events will not display the dialog itself.

In either of these examples, trying to start with a POSIX file & attempting to concatenate the additional path by using & "Folder:" & "Filename" won't do what you expect.
This is one point on which I don't know why it doesn't, but it doesn't.

One last note - if you are looking for a file, then all the above works.
If you are looking for a folder, then use this…

set theFolder to ((path to home folder as text) & "Desktop:" & "testFolder:")
set msg to "No sign of it."

tell application "System Events"
    if exists folder theFolder then
        set msg to "Yeah, found it!"
    end if
end tell

display dialog msg