AppleScript shell script

applescriptcommand line

How can I run a shell script on multiple different computers with the same code? If I have it run from

/users/'my name'/desktop/etc.

then it won't work when I use it on a different computer, because the user name will be different. Is there any way to run it directly without going through the user directory?

Best Answer

The AppleScript command I think you're looking for is:

(path to Desktop Folder) as text

or

(path to Desktop Folder)

But that would return an HFS path and the code you posted is a POSIX Path so you could change it to:

set homePath to POSIX path of (path to Desktop Folder) as text

As stated in the comment there is $USER for the terminal and per memory you can still call it in AppleScript with something like:

do shell script "$USER"

If path to desktop folder doesn't work you can try just path to desktop.


Edit

Addressing the comment. I was taught HFS path and alias path were the same, will make it another question for clarity.

Building the path as text is a habit when concat strings or if I was going to take the path and call it later in a do shell. For example:

(path to desktop folder)

in Script Debugger returns an HFS path. When you combine a directory as it was shown in the OP's question a compiling error occurs with:

(path to desktop folder & "foobar:")

and a

(path to desktop folder) & "foobar:"

when I've called paths using path to I've had to write them as:

(path to desktop folder as text) & "foobar:"

I was unfamiliar to short user name of (system info) and path to home folder but that's good to know. One issue with the question is the OP marks it with terminal and AppleScript so the solution is somewhat complicated.