0

I'm writing an AppleScript that will ask a user which remote cloud service and then which bucket they would like to mount in Mac OS using rclone. But in order to run the rclone command in an AppleScript, you need to include the entire path to the app. For me that is: /usr/local/bin/rclone

I want to include, as a variable, the location of rclone using the which command in a shell script like this:

set rcloneLOC to paragraphs of (do shell script "which rclone")

But I get a script error stating "The command exited with a non-zero status." This happens even if I just try to run do shell script "which rclone" by itself. If I type which rclone into terminal, I get the result I expect.

How do I get this to work?

6
  • which won't help here, because it searches for the command in the same places that the shell would if you just used the command name directly. Specifically, both which and the shell search the directories listed in the PATH environment variable. do shell script runs its commands with a pretty basic environment, so neither will be able to find it without a full path. Commented Dec 29, 2022 at 9:48
  • I’m a little confused. When I type which rclone into terminal, no matter my path, it works. But not in the AppleScript, which you’ve explained is due to its environment (which I don’t fully understand, I’m a hobbyist at this, not a pro). So how would I find the path to rclone using AppleScript? I am hoping to give this script to a few friends and colleagues, but we all run different versions of macOS. So I want to make sure the path to rclone is found and correct. The rest of this should work fine. Commented Dec 29, 2022 at 10:56
  • 1
    PATH is different from your working path. You can check its value in your interactive shell with echo "$PATH". It's a colon-delimited list of directories to search for executables, and you'll see that it includes /usr/local/bin as one of the entries. Now open Script Editor and try running the AppleScript do shell script "echo \"$PATH\"" and you'll see that it doesn't include that directory. The difference is because your interactive shells run several setup scripts that (among other things) add more entries to the shell's PATH. I don't know the best way to do this from AppleScript. Commented Dec 29, 2022 at 11:03
  • 1
    As @GordonDavisson suggests, you can see your path using 'echo $PATH'. To change the applescript's path (and view the change) try do shell script "export PATH=/usr/local/bin:$PATH ; echo $PATH" which will prepend usrlocalbin to the path, and then echo the updated path. Note that this edit will vanish once the shell script is finished. So you need to combine the commands, like so: do shell script "export PATH=/usr/local/bin:$PATH ; which rsync". I use rsync because I don't have rclone installed. The result is "/usr/local/bin/rsync" Commented Dec 29, 2022 at 15:09
  • It seems the shell used by do shell script doesn't have the directory of rclone on its PATH, possibly because it's a very basic shell. You could try to run a more modern shell that may have rclone on its PATH. I would try do shell script "zsh -c 'which date'" Commented Dec 29, 2022 at 16:39

1 Answer 1

1

As @GordonDavisson suggests, you can view your path using echo $PATH.

To change your applescript's path (and view the change) try this:

do shell script "export PATH=/usr/local/bin:$PATH ; echo $PATH" 

The first part of the shell command (up to the semi-colon) will prepend /usr/local/bin to your default path. The second part will return your updated path. The semi-colon has the second part run after the first part is finished.

It's important to note that this change is temporary and only in effect for this shell script and only while it is operating. This is why you need the combined commands in order to see the effect.

I'll use 'rsync' as an example since I don't have rclone; substitute 'rclone' to get its path. To get its path, you combine the export command with which, like so:

do shell script "export PATH=/usr/local/bin:$PATH ; which rsync"

The result is /usr/local/bin/rsync.

To clarify a couple of things… the environment is a set of conditions that apply for each user. You can get a basic rundown of it by running man 7 environ in Terminal. There is an env command which lists your settings and allows you to edit them; man env will provide info on it. At the bottom of these man pages, you should see references to related commands which you can also look up. Meanwhile, from within Script Editor, you could run a 1-line script with do shell script "env" and see the corresponding environment for applescript's shell.

Based on Apple's documentation (or my interpretation of it), they chose this setup because it is relatively secure and portable. You know what you get every time you run a shell script. You don't need to use a more modern shell to run the which command. You can modify the environment as needed, the same way you would while using the terminal.

Finally, Apple has provided Technical Note 2065 which provides info on using shell scripts with applescript. Also, you can likely get more info here or on the unix stack exchange.

NB All of the above is just my understanding, which is limited.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.