Link to home
Start Free TrialLog in
Avatar of Jamie Garroch (MVP)
Jamie Garroch (MVP)Flag for United Kingdom of Great Britain and Northern Ireland

asked on

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?!
Avatar of strung
strung
Flag of Canada image

Is there any possibility that you have a trailing space in the folder name?
ASKER CERTIFIED SOLUTION
Avatar of strung
strung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jamie Garroch (MVP)

ASKER

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
  ypMacApplications
  ypMacAppSupport
  ypMacDesktop
  ypMacDocuments
  ypMacHome
  ypMacLibrary
  ypMacMovies
  ypMacMusic
  ypMacPictures
End Enum

Public Enum ypMacDomain
  ypUser
  ypLocal
  ypSystem
End Enum

Open in new window