user5058584
user5058584

Reputation:

Using "~" with a POSIX path in AppleScript

How can I use the ~ in the following AppleScript?

info for POSIX path of "Users/[User]/Pictures"

The script should be universal, but if I replace Users/[User] with ~, so that the script looks like that

info for POSIX path of "~/Pictures"

I'm getting an error that the directory is not available.

I'm thankful for help.

Upvotes: 4

Views: 4898

Answers (3)

ndasusers
ndasusers

Reputation: 757

Not sure if it this answer is truly universal, but since ~ is expanded by the bash shell, but I tried using the result of the expansion as the path and it worked.

info for POSIX path of (do shell script "echo ~/Pictures")

{name:"Pictures", creation date:date "Wednesday, July 8, 2015 at 6:33:09 PM", modification date:date "Wednesday, February 8, 2017 at 2:11:52 PM", size:102529438, folder:true, alias:false, package folder:false, visible:true, extension hidden:false, name extension:missing value, displayed name:"Pictures", default application:alias "Macintosh HD:System:Library:CoreServices:Finder.app:", kind:"Folder", folder window:{0, 0, 0, 0}, file type:"", file creator:""}

it is the same result for me as running like so

info for POSIX path of (path to pictures folder)

For my case, expanding the ~/ it is useful because the situation of looking for a folder in ~/ which is not standard for Mac. Example

info for POSIX path of (do shell script "echo ~/Projects")

{name:"Projects", creation date:date "Friday, June 17, 2016 at 11:08:50 AM", modification date:date "Wednesday, February 8, 2017 at 2:30:08 PM", size:130670100, folder:true, alias:false, package folder:false, visible:true, extension hidden:false, name extension:missing value, displayed name:"Projects", default application:alias "Macintosh HD:System:Library:CoreServices:Finder.app:", kind:"Folder", folder window:{0, 0, 0, 0}, file type:"", file creator:""}

But the same failed according to the previous method.

info for POSIX path of (path to Projects folder)

Syntax Error
Expected "," but found property.

Updated after comment

This worked for the "non-standard" folder in the home directory.

info for alias ((path to home folder as text) & "Projects:")

Upvotes: 1

vadian
vadian

Reputation: 285140

There is no need to use the tilde (~) in AppleScript, which is shortcut to the current user's home folder in the shell.

There are many relative paths in AppleScript

path to desktop 

is the alias specifier to the desktop folder of the current user, the equivalent to

alias "[startup volume]:Users:[current user]:Desktop"

The relative path to the pictures folder is

path to pictures folder  

other paths

path to music folder
path to applications folder
path to home folder

etc.

Please look into the dictionary of Standard Additions for further paths

For some relative paths there is a parameter to specify the domain

path to library folder from user domain -- alias "[startup volume]:Users:[current user]:Libary"
path to library folder from local domain -- alias "[startup volume]:Library"
path to library folder from system domain -- alias "[startup volume]:System:Library"

info for is a very old part of Standard Additions. It's deprecated since Leopard but it's still working. The simplest form is to pass an alias specifier:

info for (path to pictures folder )

Upvotes: 3

Paul R
Paul R

Reputation: 213039

~ is a shell thing, so it won't work in this context, but there's a much easier way anyway, using path to:

info for POSIX path of (path to pictures folder)

Upvotes: 1

Related Questions