Apple Script incorrectly reporting existence of folders

This seems so simple yet it is baffling me! I am using a simple Apple Script, called from VBA on Office:Mac 2011 to determine if a local folder exists or not.

My application is storing data in a folder below Application Support, for example:

Macintosh HD:Library:Application Support:myApp

But even after the myApp folder has been created, the following basic script returns false:

tell application "System Events" to return exists disk item "Macintosh HD:Library:Application Support:myApp:"

Open in new window

So I then went looking at other folders below "Application Support" and the script returns true for some of them and false for others, with no obvious reason or pattern.

For example, this returns true:

tell application "System Events" to return exists disk item "Macintosh HD:Library:Application Support:Microsoft:"

Open in new window

While this returns false:

tell application "System Events" to return exists disk item "Macintosh HD:Library:Application Support:Microsoft:Office:"

Open in new window

Yet both folders exist.

I also tried Finder but this is returning the same inconsistent results:

tell application "Finder" to return exists folder "Macintosh HD:Library:Application Support:Microsoft:Office:"

Open in new window

I don't believe I need to use the Posix format (and associated \\ escape for spaces in the path) for in-script calls so what is going on?!
LVL 15
Jamie GarrochSenior Technical Consultant at BrightCarbonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Is there any possibility that you have a trailing space in the folder name?
Also, are you sure you are looking in the right Library Folder? On my computer, there is a Microsoft folder in the Application Support folders in both the Library folder at the root of the hard drive and the Library folder in the User's home folder, but the Office folder is only in the User's home folder.

In other words, on my computer,

Macintosh HD:Library:Application Support:Microsoft: exists, but

Macintosh HD:Library:Application Support:Microsoft:Office:  does not.


Macintosh HD:Users:Username:Library:Application Support:Microsoft:Office: exists.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jamie GarrochSenior Technical Consultant at BrightCarbonAuthor Commented:
Doh. I'm so not familiar with the use of "domains" on a Mac file system! That's what it was strung. So I've now written a function to return the path for various folder aliases such as home, desktop, library and included a "domain" attribute in the function which determines if the path returned should be local, system or user.

Thanks for showing me the light!

Just in case the function would be useful for other readers:

Function GetMacFolder(Folder As ypMacFolderAlias, Domain As ypMacDomain, POSIX As Boolean) As String
  Dim AppleScript As String
  Dim PathScript As String
  Dim PathPosix As String
  Dim strDomain As String
  Select Case True
    Case Domain = ypLocal: strDomain = "local"
    Case Domain = ypSystem: strDomain = "system"
    Case Domain = ypUser: strDomain = "user"
  End Select
  AppleScript = "tell application ""Finder"" " & Chr(13)
  Select Case True
    Case Folder = ypMacDesktop: AppleScript = AppleScript & "return (path to desktop folder) as string" & Chr(13)
    Case Folder = ypMacDocuments: AppleScript = AppleScript & "return (path to documents folder) as string" & Chr(13)
    Case Folder = ypMacHome: AppleScript = AppleScript & "return (path to home folder) as string" & Chr(13)
    Case Folder = ypMacLibrary: AppleScript = AppleScript & "return (path to library folder from " & strDomain & " domain) as string" & Chr(13)
    Case Folder = ypMacAppSupport: AppleScript = AppleScript & "return (path to application support folder from " & strDomain & " domain) as string" & Chr(13)
  End Select
  AppleScript = AppleScript & "end tell"
  PathScript = MacScript(AppleScript)
  If POSIX Then GetMacFolder = MacPathToPosix(PathScript) Else GetMacFolder = PathScript
End Function

Open in new window

And the enumerations for it:

Public Enum ypMacFolderAlias
End Enum

Public Enum ypMacDomain
End Enum

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Swift Programming

From novice to tech pro — start learning today.