Applescript: Run or Call a Shell Script

To run or call a shell script from AppleScript, you can use the do shell script command. Here’s an example:

set myScriptPath to "/path/to/my/script.sh"
do shell script myScriptPath

In this example, the myScriptPath variable contains the path to the shell script that you want to run. The do shell script command then executes the script.

If your shell script takes arguments, you can include them as a string after the script path, like this:

set myScriptPath to "/path/to/my/script.sh"
set myScriptArgs to "arg1 arg2 arg3"
do shell script myScriptPath & " " & myScriptArgs

In this example, the myScriptArgs variable contains the arguments that you want to pass to the shell script, separated by spaces. The & operator is used to concatenate the script path and arguments into a single string, which is then passed to the do shell script command.

By default, the do shell script command runs the shell script using the /bin/sh shell. If you need to use a different shell, you can specify it as an optional parameter to the do shell script command, like this:

set myScriptPath to "/path/to/my/script.sh"
set myShell to "/bin/bash"
do shell script myScriptPath with administrator privileges user name "username" password "password" with administrator privileges and with shell myShell

In this example, the myShell variable contains the path to the shell that you want to use. The with shell parameter is used to specify the shell to use with the do shell script command.

The with administrator privileges user name "username" password "password" with administrator privileges parameters are used to run the shell script with elevated privileges (i.e. as an administrator). Note that you will need to replace “username” and “password” with a valid username and password for the system.

Leave a Comment