Jamie Garroch (MVP)
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:
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:
I also tried Finder but this is returning the same inconsistent results:
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:"
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:"
While this returns false:tell application "System Events" to return exists disk item "Macintosh HD:Library:Application Support:Microsoft:Office:"
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:"
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?!
Is there any possibility that you have a trailing space in the folder name?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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:
And the enumerations for it:
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
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