I want to be able to double click to open a files type (HTML) in a particular app (SeaMonkey composer).
Double clicking the file opens SeaMonkey Browser, but I want it to open in Seamonkey Composer. The only way to do it is with the following command line
seamonkey -editor "filename.html"
So, how can I use apple script or automator to open my html files in composer ?
Save the following script as Application in Script Editor:
on run filesList
repeat with fileRef in filesList
do shell script "seamonkey -editor " & quoted form of POSIX path of fileRef
end repeat
end run
Select View > Show Bundle Contents and give it a custom bundle ID. You can then change the file association as above.
The above assumes the seamonkey command is itself just a launcher; if it's actually the full application (which may be the case as it's obviously not a native Mac app), the middle line'll need tweaked a bit:
do shell script "nohup seamonkey -editor " & quoted form of POSIX path of fileRef & " >/dev/null 2>&1"
That should allow the shell script to exit as soon as the seamonkey process is launched, leaving Seamonkey running until you quit it from its GUI.
If you want to use AppleScript to do this, this simple script should do the job:
set filePath to ((path to documents folder) as text) & "filename.html"
tell application "Seamonkey Composer" to open filePath
I don't have the Seamonkey Composer app to test, but it works with BBEdit.
Note that the open command must have a full path to the file.
I need to do a .command file script/batch. Launching it (double-click) it has to to those things:
Open a terminal window (A)
Launching a command that open the folder where the file is (maybe this cd "dirname "$0"")
Launch a command
Open a terminal window (B)
Launching same command at point 2
Launch a command
Given that you explicitly want to create terminal windows, consider creating an application using AppleScript:
Open Script Editor (up to 10.9, AppleScript Editor)
Paste the code below.
Save as an application (via the pop-up list in the Save As dialog) to the desired folder.
# Determine the folder in which this app is located.
set thisFolder to do shell script "dirname " & quoted form of POSIX path of (path to me)
# Sample commands to execute in the new windows.
set cmds to {"date", "echo $$"}
tell application "Terminal"
# Create 2 new windows, change to the
# this app's folder, and execute the respective command.
repeat with i from 1 to 2
do script "cd " & quoted form of thisFolder & "; " & item i of cmds
end repeat
# Activate Terminal.app
activate
end tell
The reason that I recommend using an application over a *.command file is that the latter would itself open in a Terminal window first, before creating the desired windows, which is visually disruptive (and, depending on your Terminal.app preferences, may leave the extra window open).
Alternatively, you could turn that into a virtue and use the *.command file's own window as your 1st terminal window, and only create one additional one.
My goal is to execute a shell script by double clicking on an OS X .app, and the Terminal to be visible to the user (my script has a CLI GUI).
Any ideas? I've tried appify but I can't get it to show the Terminal window.
Many thanks.
For those interested, I found the following solution which involves using an Apple Script command.
First you need to create a launcher.sh script.
Use the code below. The many quotes below allow your app to run from a path with spaces.
#!/bin/bash
scriptPath=$(dirname "$0")'/yourscript.sh'
osascript -e 'tell app "Terminal" to do script "\"'"$scriptPath"'\""'
Use appify to generate an app bundle.
Type this in the Terminal to do it:
appify launcher.sh "Your app name"
From Finder,
right-click on your generated app bundle, Show package contents, and paste yourscript.sh into /Contents/MacOS/
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.
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.