I have a bash scrips pasted into "Run shell script " Action in Automator apple script under 10.9.3 and the command is copy command a dll to system folder /Library/Audio/Plug-Ins/VST/ that is read only.
I need to prompt the user that runs the script to enter the password. what is the way to to that?
Thanks!
If it's short enough, you can instead use Run AppleScript, and use:
do shell script "command" with administrator privileges
That will prompt the user for administrator username and password in the GUI and then run "command" as root.
Update per comment below:
I missed the part about copying into a privileged folder. You can't use Automator's "Run Shell Script", because it doesn't support running commands as administrator (it's not interactive, so sudo doesn't work). So there are a couple of different ways you could solve the problem.
To run your Bash script as an administrator, save your Bash script as a typical script you run in Terminal. After the Automator action to choose the file, use the "Run AppleScript" action. Then put in the code below. You'll see no prompt and no Terminal window; it will just execute. (If you do want a prompt, you can get one in the GUI by omittinguser name "username" password "password".) The folder the user chose will be the argument to the Bash script.
on run {input, parameters}
do shell script ("sudo /path/to/yourscript '" & POSIX path of input & "') ¬
with administrator privileges user name "username" password "password"
end run
An alternative approach: A Terminal window will open with the prompt "Password:", and the user will enter their password (presumably, it's an administrator). Then your script will run as administrator and be able to copy into the privileged folder.
on run {input, parameters}
tell application "Terminal"
do script "sudo /path/to/yourscript '" & POSIX path of input & "'"
end tell
end run
Or, if you really want to put your Bash script in Automator under "Run Shell Script" as you have currently, and the copy instruction is at the end, you could take the copy instruction out, and have it instead echo the folder the user chose as its final instruction. This will return it to the next Automator action. After Run Shell Script, put in a Run AppleScript with a do shell script command as shown in the first example above, that contains a cp command in place of /path/to/yourscript, with administrator privileges.
Related
I have the following shell script that I normally run successfully as a system service (created in automator:
#!/bin/bash
for f in "$#"
do ln -s "$f" "${f}.deleteThisExtensionAfterMoving"
echo "${f}.deleteThisExtensionAfterMoving"
done
However, when trying to use this to make symlinks in folders that I'm not the owner of, it fails.
I tried saving it as a script, then using applescript in automator as I've seen described elsewhere here:
on run {input, parameters}
do shell script ("sudo ~/Documents/z_misc/makesymlink '" & POSIX path of input & "'") with administrator privileges
end run
However, upon running the service, I see a "command not found" error.
When I tried saving the script as a .sh file and using that instead, it still didn't work. When I try running from terminal without the preceding "sudo" command, I get a "permission denied" error.
When I try running from terminal without the preceding "sudo" command, I get a "permission denied" error.
The "permission denied" error when attempting to execute a shell script (or binary executable) is typically caused because the file is not set as executable. Use the following command in Terminal:
chmod +x file
Or:
chmod +x /path/to/file
Quoting if there are spaces or escaping spaces with a backslash (\), but not both.
However, the "permission denied" error can also be caused if there is a privileges issue with where the shell script or binary executable is located.
Keeping executables in you Documents folder or any of the default folders, except for Public, within your Home folder can be problematic to Automator actions and for use in Terminal.
I created a bin folder in my Home folder and place my shell scripts and binary executables that I do not want elsewhere, e.g. /use/local/bin, and have added "$HOME/bin" to my PATH.
Here is what I'd use in the Run AppleScript action in my Automator Service/Quick Action:
Example AppleScript code:
on run {input, parameters}
repeat with thisItem in input
set thisItem to the quoted form of the POSIX path of thisItem
do shell script ("$HOME/bin/makesymlink " & thisItem) with administrator privileges
end repeat
end run
As coded, this allows for multiple Finder items to be passed to the Automator Service/Quick Action.
That all said, without seeing the contest of the makesymlink file there is not anything else I can think off at the moment.
From the linked Technical Note below:
Note: Using sudo(8) with with administrator privileges is generally unnecessary and creates security holes; simply remove the sudo.
Have a look at Technical Note TN2065 do shell script in AppleScript
If you are going to use the do shell script command, then I suggest you read the entire Technical Note.
I am using AppleScript to run a shell script as admin.
do shell script custom_command with prompt custom_prompt with administrator privileges
which gives me this prompt that is fine, but I would like to be able to change the icon to something other than a terminal icon. Any help would be appreciated.
The badge on the authentication dialog comes from the application that is using the command.
To get a custom badge for the icon you can use a helper AppleScript application to run your script, but if the osascript is all you are doing, you can just do everything by using the app (otherwise all you are doing is using an osascript to run an AppleScript that runs an osascript). To create an application for the osascript, edit your script in the Script Editor, for example:
do shell script "echo 'this is a test' >> ~/Desktop/Testing.txt" with administrator privileges
then save it as an application (leave the options unchecked). Rename a copy of the icon file you want to use to applet.icns, open the app bundle, and replace the existing icon file in /Contents/Resources/. You can then run the application from the Terminal by using open /path/to/your/app
The main icon will still be the padlock, but the badge will now be your application icon.
To pass arguments to an application is a little different than an osascript, but a little bit of AppleScriptObjC takes care of that:
use framework "Foundation"
use scripting additions
# the first argument item is the applet/droplet executable path, so we'll just skip that
set args to rest of (arguments of current application's NSProcessInfo's processInfo() as list)
if args is not {} then
do shell script "echo " & first item of args & " >> ~/Desktop/Testing.txt" with administrator privileges
end if
You can run that by using open /path/to/your/app --args “this is a test”
Could someone tell me what is wrong with this:
tell application "Terminal"
activate
tell application "System Events"
keystroke "sudo"
keystroke return
keystroke "purge"
keystroke return
end tell:
end tell
Not sure why you want to use Automator and Applescript and things when you can do that perfectly well in a bash shell script.
If you copy this file and save it on your Desktop as purgenow
#!/bin/bash
sudo /usr/sbin/purge
then start Terminal and make the script executable like this
chmod +x ~/Desktop/purgenow
you will be able to double-click it and run the script.
The command with the sudo, should really only work if you have adminstrator rights on your account, or are logged in as root, as normally you'd be prompted for a password, you'll have a hard time entering the password for the shell - utility from AppleScript.
Instead, you can excempt the whole sudo command, as you can have the do shell script event take care of that for you, either by specifying the password by performing a do shell script with administrator privileges and enter the account name that holds the administrator rights, and password, when prompted.
You can also specify predicates to the do shell script event that lets you specify both username and password, to get rid of the prompt for password dialog, and have the purge command run automatically with administrator privileges.
It may under some circumstances be feasible to prepend the do shell scriptevent with tell me, for the case that the do shell script event is called from within an application block, (which is bad style really, and should be avoided whenever possible).
do shell script "/usr/sbin/purge" with administrator privileges use name "Admin" password "password"
You can read about the do shell script event in the Standard Additions dictionary of Script Editor. There is also a Technical Note, you may wish to skim: Technical Note TN2065: do shell script in AppleScript.
Please refer to this question first,
unable to read opensslv.h:No such file or directory
Based on that I need to run the following three line Terminal commands using AppleScript,
/tmp/ssl/openssl-1.0.1h/Configure darwin64-x86_64-cc ––prefix=/usr no-threads shared
make -f /tmp/ssl/openssl-1.0.1h/Makefile
sudo make -f /tmp/ssl/openssl-1.0.1h/Makefile install
I tried two methods I created text files with .command and .sh extensions and added the above three lines. Then tried to run it from AppleScript as,
do shell script "/Users/Username/Desktop/RunScript.sh"
But got this error,
error "/Users/Username/Desktop/RunScript.sh: line 1: /tmp/ssl/openssl-1.0.1h/Configure: No such file or directory
/Users/Muhriz/Desktop/InstallOpenSSL.sh: line 2: make: command not found sudo: no tty present and no askpass program specified" number 1
This could work,
tell application "Terminal" to activate
tell application "Terminal"
do script ("/tmp/ssl/openssl-1.0.1h/Configure darwin64-x86_64-cc ––prefix=/usr no-threads shared") in window 1
do script ("make -f /tmp/ssl/openssl-1.0.1h/Makefile") in window 1
do script ("sudo make -f /tmp/ssl/openssl-1.0.1h/Makefile install") in window 1
end tell
But it asks for password in Terminal at the third line and waits for user response. The password dialog shown from the AppleScript (when using with administrator privileges) is OK. But it must no ask for password via Terminal when running the commands. It needs to ask only once when the AppleScript is executed and run all sudo related commands without asking for password in Terminal.
What code needs to be used to run from AppleScript?
Perform the following before running your scripts.
chmod a+x /Users/Username/Desktop/RunScript.sh
xattr -r -d "com.apple.quarantine" /tmp/ssl/openssl-1.0.1h
do shell script "/Users/Username/Desktop/RunScript.sh"
That doesn’t work because you can’t pass a path to the “do shell script” command, you can only pass it the contents of the actual script.
If you want to run a bash script that is contained in its own file, you can use TextEdit to open the bash script file and set the contents of the file to a variable, which you can then pass to “do shell script.”
tell application "TextEdit"
set theDesktopPath to the path to the desktop folder as text
set theShellScriptPath to theDesktopPath & "RunScript.sh"
open file theShellScriptPath
set theShellScript to the text of document 1
set theScriptResult to do shell script theShellScript
make new document
set the text of document 1 to theScriptResult
end tell
How do I set up a shell script to execute from the Mac OSX dock? It seems that simply creating a shortcut will open the file in my editor. Is there a flag I need to set somewhere to tell it to run instead of opening it for editing?
You could create a Automator workflow with a single step - "Run Shell Script"
Then File > Save As, and change the File Format to "Application". When you open the application, it will run the Shell Script step, executing the command, exiting after it completes.
The benefit to this is it's really simple to do, and you can very easily get user input (say, selecting a bunch of files), then pass it to the input of the shell script (either to stdin, or as arguments).
(Automator is in your /Applications folder!)
If you don't need a Terminal window, you can make any executable file an Application just by creating a shell script Example and moving it to the filename Example.app/Contents/MacOS/Example. You can place this new application in your dock like any other, and execute it with a click.
NOTE: the name of the app must exactly match the script name. So the top level directory has to be Example.app and the script in the Contents/MacOS subdirectory must be named Example, and the script must be executable.
If you do need to have the terminal window displayed, I don't have a simple solution. You could probably do something with Applescript, but that's not very clean.
On OSX Mavericks:
Create your shell script.
Make your shell script executable:
chmod +x your-shell-script.sh
Rename your script to have a .app suffix:
mv your-shell-script.sh your-shell-script.app
Drag the script to the OSX dock.
Rename your script back to a .sh suffix:
mv your-shell-script.app your-shell-script.sh
Right-click the file in Finder, and click the "Get Info" option.
At the bottom of the window, set the shell script to open with the terminal.
Now when you click on the script in the dock, A terminal window will pop up and execute your script.
Bonus: To get the terminal to close when your script has completed, add exit 0 to the end and change the terminal settings to "close the shell if exited cleanly" like it says to do in this SO answer.
I know this is old but in case it is helpful to others:
If you need to run a script and want the terminal to pop up so you can see the results you can do like Abyss Knight said and change the extension to .command. If you double click on it it will open a terminal window and run.
I however needed this to run from automator or appleScript. So to get this to open a new terminal the command I ran from "run shell script" was "open myShellScript.command" and it opened in a new terminal.
As long as your script is executable and doesn't have any extension you can drag it as-is to the right side (Document side) of the Dock and it will run in a terminal window when clicked instead of opening an editor.
If you want to have an extension (like foo.sh), you can go to the file info window in Finder and change the default application for that particular script from whatever it is (TextEdit, TextMate, whatever default is set on your computer for .sh files) to Terminal. It will then just execute instead of opening in a text editor. Again, you will have to drag it to the right side of the Dock.
In the Script Editor:
do shell script "/full/path/to/your/script -with 'all desired args'"
Save as an application bundle.
As long as all you want to do is get the effect of the script, this will work fine. You won't see STDOUT or STDERR.
I think this thread may be helpful: http://forums.macosxhints.com/archive/index.php/t-70973.html
To paraphrase, you can rename it with the .command extension or create an AppleScript to run the shell.
As joe mentioned, creating the shell script and then creating an applescript script to call the shell script, will accomplish this, and is quite handy.
Shell Script
Create your shell script in your favorite text editor, for example:
mono "/Volumes/Media/~Users/me/Software/keepass/keepass.exe"
(this runs the w32 executable, using the mono framework)
Save shell script, for my example "StartKeepass.sh"
Apple Script
Open AppleScript Editor, and call the shell script
do shell script "sh /Volumes/Media/~Users/me/Software/StartKeepass.sh" user name "<enter username here>" password "<Enter password here>" with administrator privileges
do shell script - applescript command to call external shell commands
"sh ...." - this is your shell script (full path) created in step one (you can also run direct commands, I could omit the shell script and just run my mono command here)
user name - declares to applescript you want to run the command as a specific user
"<enter username here> - replace with your username (keeping quotes) ex "josh"
password - declares to applescript your password
"<enter password here>" - replace with your password (keeping quotes) ex "mypass"
with administrative privileges - declares you want to run as an admin
Create Your .APP
save your applescript as filename.scpt, in my case RunKeepass.scpt
save as... your applescript and change the file format to application, resulting in RunKeepass.app in my case
Copy your app file to your apps folder
Exact steps to achieve that in macOS Monterey 12.3
Open Automator
File -> New
Choose Application
Go to Library -> Utilities
Double-click Run Shell Script
Type in whatever command you want to run. For example, try the command to toggle Dark Mode:
osascript -e 'tell app "System Events" to tell appearance preferences to set dark mode to not dark mode'
File -> Save
Drag the saved file to the Dock, done!
pip install mac-appify
I had trouble with the accepted solution but this command worked for me.
Install
pip install mac-appify
Run
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/appify ~/bin/webex_start.sh ~/Desktop/webex.app
Adding to Cahan's clear answer ... to open a shell script from the dock without passing any arguments to it, try:
open [name of your script].scpt"
example:
open "//Users/user/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/myScript.scpt"
Someone wrote...
I just set all files that end in ".sh" to open with Terminal. It works
fine and you don't have to change the name of each shell script you
want to run.