0

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?

  • You would use ~/Desktop/blahblahblah. The ~ is shorthand for "the current user's home directory". – Patrick Wynne Nov 30 '18 at 0:41
  • Further to what @PatrickWynne has stated, if you are referencing these directories from within a shell script, you also have available the shell varialbe $USER, which contains the name of the home folder. So, you would write: "/Users/$USER/My Folder", for example. – CJK Nov 30 '18 at 2:02
  • Also not mentioned yet is using $HOME in place of ~ as it (~) can be problematic, and it's recommended to use $HOME over it when using do shell script in AppleScript. – user3439894 Nov 30 '18 at 10:55
1

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.

  • Some very minor notes: your first code line doesn't return an alias path; it returns an HFS path. Your second code line doesn't require coercing to text: POSIX path returns a text object. Your third code line is an unnecessary call to a shell script, as the same information can be retrieved via short user name of (system info) and also via system attribute "USER". Likewise, the home folder path can be got with system attribute "HOME" (and, of course, the usual path to home folder route). – CJK Nov 30 '18 at 7:46
  • @CJK edited to answer comment – ʀ2ᴅ2 Nov 30 '18 at 14:42
  • @CJK: asked the question – ʀ2ᴅ2 Nov 30 '18 at 14:47
  • Well done for asking the question to sort through the terminology confusion. I left a comment against the existing answer to ask if he had any sources to confirm what he's saying, because I haven't yet been able to find any myself. I'm totally willing to accept that I very well could be wrong here, and I'm certainly far less certain about it now than when I wrote it, so it'd be really good if he can remove the uncertainty for us (by which I mean me). – CJK Dec 1 '18 at 1:58
  • Having now read your edit, I still think there's confusion in terminologies here: "path to desktop folder in Script debugger returns an HFS path" which isn't correct. Leaving aside the "HFS path" vs |"alias path" dilemma for now, what we do know is that path to desktop folder will always return an alias, by which I am referring to both the AppleScript object specifier and the class called "alias". Cont'd... – CJK Dec 1 '18 at 2:13
-1

This will detect what is the username with which you/script have/has logged in and changes to that directory. you can pre-pend it to your script:

     user=`whoami`        
     cd "/Users/$user"
  • 1
    The variable $USER already exists in many shells. Please see my comment against the OP's question. Also, there would be no need to place $USER outside the double quotes. cd "/Users/$USER" is fine. – CJK Nov 30 '18 at 7:33
  • Your two line answer could be a one line answer and no need to set a new variable user as already mentioned $USER should already exist. Not that I would use your answer, but if I did I write it as cd "/Users/$(whoami)" instead. – user3439894 Nov 30 '18 at 10:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .