Access Special Folders via AppleScript

There are certain folders which are not that easily accessible via AppleScript (or any other programming language for that matter) because the path to them has to be constructed by incorporating the (short) name of the current user account.

One of the most used constructs would be accessing the folder Documents located in the user’s $HOME, for example. The easiest way is the following string of commands:

set myDocumentFolder to path to documents folder as string

The result is the colon-delimited path as string, e.g. MacHD:Users:username:Documents:.

If you need the path to hand it over to a shell script or any other process requiring the Unix way of wording a path, use this approach:

set myDocumentFolder to POSIX path of (path to documents folder as string)

No surprise here, the answer will be something like this: /Users/username/Documents.

But what about accessing the user’s Library folder? If we continue like this:

set myUserLib to path to library folder as string

All we get is MacHD:Library:. Is there a way to tell AppleScript which Library folder we are really interested in? There sure is:

set myUserLib to path to library folder from user domain as string

Firing this command results in the path we are looking for: MacHD:Users:username:Library:. Telling AppleScript the domain we are interested in does the trick.

There are three domains at our disposal (there are actually some more, but those will be the subject of another post):

  • user
  • local
  • system

To cut a long story short, here are examples for the other two domains and the corresponding results:

  • set myLocalLib to path to library folder from local domain as string returns MacHD:Library:
  • set mySystemLib to path to library folder from system domain as string returns MacHD:System:Library:

It seems, that using from local domain is superfluous in regard of the folder Library, but good programming practice (and the resulting peace of mind) implies, that we should use the construct and be save from surprises later. 😉

Post a Comment

Your email is never published nor shared. Required fields are marked *, comments are moderated.

*
*