Making an executable bash file run when clicked - macos

I have a bash file that does some file manipulation. I don't want to have to open the terminal every time I run it. Is there a way to make the program run when I double click it? (Like a windows .exe file)
Thanks

You can add a ".command" extension to the filename -- then double-clicking it will automatically open Terminal and run the script in a new window. Note: this assumes you still want to watch/interact with the script via a Terminal interface; if you want to avoid this as well, wrapping the script with Platypus, AppleScript, or Automator (as Zifei and Ned suggest) would be better options.

What you need is Platypus.
Platypus is a developer tool for the Mac OS X operating system. It can be used to create native, flawlessly integrated Mac OS X applications from interpreted scripts such as shell scripts or Perl and Python programs. This is done by wrapping the script in an application bundle directory structure along with an executable binary that runs the script.

The easiest thing to do is to type: sudo chmod 755 the_file_Name.This will allow you to double click on the file in the finder.

With OS X 10.5+, you can wrap the bash shell script in an AppleScript application using the AppleScript editor or an Automator application using Automator.app (see the Automator on-line help).
You could write (and there are apps out there that do this) an OS X app that accepts arbitrary .sh files and executes them. However, that's generally a bad idea as it could open you up to attacks if you inadvertently download a malicious shell script file that is automatically opened by your web browser. Better to be explicit.

Related

How to Notarize an executable shell script (.command) file for MacOS 10.15 Catalina

This is a DIFFERENT scenario than discussed in other postings:
I have a .sh (shell script) which I've changed to a .command (executable shell script). It's signed using code sign, and being distributed on a .dmg that is signed.
However, Catalina (of course) doesn't like it -- complains it "can't be opened because Apple can't check it for malicious software". The user can still open the .command file if they right-click and select "Open" from the context menu, but the alert is still displayed, although this time with an available Open button.
I'd really like to avoid having to create a simple command line APPLICATION just for the purpose of executing a few shell script lines -- just so it can be notarized.
Anyone know of how to get around this?
Stephen
Your going to hate this but throw it into a .app bundle and call it from the .plist as the exec. Then you may proceed with notarization.

Run script in ".app" (Application Bundle) as the primary executable

I'm trying to use Mono to make a portable application that works on Windows, Mac, and Linux, but I want to have a standard DMG file that allows users to simply click and drag my application to their Applications folder on their Mac. In the best answer to this post I found that by creating an executable script beginning with #! in the MyAppName.app/Contents/MacOSX/ folder, it could run whatever I liked.
How do I get a script to actually run?
I made a blank "Hello World" Cocoa App in XCode, and copied the .app file to the Applications directory, and everything worked great. But when I replaced the executable in the MacOSX folder with a script starting with #! it didn't run the script.
I tried chmod 755 myscript, I tried deleting the old executable file and renaming my script to be the old filename, but all that served to do was stop the app from running at all. The permissions of the script, and the filename, are both correct.
So then I assumed that maybe scripts wouldn't work, and XCoded a command line utility, again that would simply spout "Hello World!", but still it didn't run.
The ".app" extension is formally called an Application Bundle, and they have some pretty good documentation, but none of it seems to tell me why my approach isn't working.
Here is the .app bundle that I'm working on. Inside the MasOSX directory you'll find hello (the Hello Word command line utility) and script (a one line script with a #! as the first line) and XCodeTest (a Hello World cocoa app).
Scripts in Unix
Well, first of all, the correct syntax for the first line of a script is
#! <path-to-script-interpreter> [<optional-args>]
For instance
#! /usr/bin/perl -wT
... perl code here
Using the open command
However, this won't help you because the open command does not support shell scripts as launchers. You need to write some C / Objective-C code and compile it into a native executable. Simply using system() or execv() inside that tool won't work either, you'd have to dlopen() the Mono runtime.
Look at monodevelop/mainbuild/MacOSX/monostub.m for an example. This is what MonoDevelop uses to launch MonoMac application.
When you write a Cocoa app with XCode, it will compile your app into native code and put the resulting binary as launcher in the Contents/MacOS directory.
The easy solution
Why don't you simply create a MonoMac application with MonoDevelop? This would automatically create the .app package for you - no need to fiddle around with scripts or custom launchers.

How to make a stand alone Mac Terminal Program?

I want to be able to save a file in a way that opening it with a regular double-click runs it. Also is there a mac equivalent of #ECHO OFF that makes it so no text shows up in terminal?
Probably the easiest thing to do would be to wrap your script or program in a tiny bit of AppleScript:
do shell script "/your/command/here"
Then save it as an application and it will behave like any other OSX application.
The shell shouldn't be printing the commands as they're run unless you're supplying the -x switch so I'm not sure what you want #ECHO OFF for.
If you save a script with the file extension .command, it will be double-clickable in the Finder.

How to launch an app on OS X with command line - The best way

I want to launch an app on OSX from a script. I need to pass some command line arguments. Unfortunately, open doesn't accept command line args.
The only option I can think of is to use nohup myApp > /dev/null & to launch my app so it can exist independently of the script that launches it.
Any better suggestions?
As was mentioned in the question here, the open command in 10.6 now has an args flag, so you can call:
open -n ./AppName.app --args -AppCommandLineArg
In OS X 10.6, the open command was enhanced to allow passing of arguments to the application:
open ./AppName.app --args -AppCommandLineArg
But for older versions of Mac OS X, and because app bundles aren't designed to be passed command line arguments, the conventional mechanism is to use Apple Events for files like here for Cocoa apps or here for Carbon apps. You could also probably do something kludgey by passing parameters in using environment variables.
An application bundle (.app file) is actually a directory. Instead of using open and the .app filename, you can move into the app's directory and start the actual machine code program located inside. For instance:
$ cd /Applications/LittleSnapper.app/
$ ls
Contents
$ cd Contents/MacOS/
$ ./LittleSnapper
That is the actual binary executable that might accept arguments (or not, in LittleSnapper's case).
In case your app needs to work on files (what you would normally expect to pass as: ./myApp *.jpg), you would do it like this:
open *.jpg -a myApp
You can launch apps using open:
open -a APP_YOU_WANT
This should open the application that you want.
open also has an -a flag, that you can use to open up an app from within the Applications folder by it's name (or by bundle identifier with -b flag). You can combine this with the --args option to achieve the result you want:
open -a APP_NAME --args ARGS
To open up a video in VLC player that should scale with a factor 2x and loop you would for example exectute:
open -a VLC --args -L --fullscreen
Note that I could not get the output of the commands to the terminal. (although I didn't try anything to resolve that)
I would recommend the technique that MathieuK offers. In my case, I needed to try it with Chromium:
> Chromium.app/Contents/MacOS/Chromium --enable-remote-fonts
I realize this doesn't solve the OP's problem, but hopefully it saves someone else's time. :)
Lots of complex answers when you can simply access Applications folder and type:
open -a [APP NAME]
This is it!
I wanted to have two separate instances of Chrome running, each using its own profile. I wanted to be able to start them from Spotlight, as is my habit for starting Mac apps. In other words, I needed two regular Mac applications, regChrome for normal browsing and altChrome to use the special profile, to be easily started by keying ⌘-space to bring up Spotlight, then 'reg' or 'alt', then Enter.
I suppose the brute-force way to accomplish the above goal would be to make two copies of the Google Chrome application bundle under the respective names. But that's ugly and complicates updating.
What I ended up with was two AppleScript applications containing two commands each. Here is the one for altChrome:
do shell script "cd /Applications/Google\\ Chrome.app/Contents/Resources/; rm app.icns; ln /Users/garbuck/local/chromeLaunchers/Chrome-swirl.icns app.icns"
do shell script "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --user-data-dir=/Users/garbuck/altChrome >/dev/null 2>&1 &"
The second line starts Chrome with the alternate profile (the --user-data-dir parameter).
The first line is an unsuccessful attempt to give the two applications distinct icons. Initially, it appears to work fine. However, sooner or later, Chrome rereads its icon file and gets the one corresponding to whichever of the two apps was started last, resulting in two running applications with the same icon. But I haven't bothered to try to fix it — I keep the two browsers on separate desktops, and navigating between them hasn't been a problem.
Beginning with OS X Yosemite, we can now use AppleScript and Automator to automate complex tasks. JavaScript for automation can now be used as the scripting language.
This page gives a good example example script that can be written at the command line using bash and osascript interactive mode. It opens a Safari tab and navigates to example.com.
http://developer.telerik.com/featured/javascript-os-x-automation-example/
osascript -l JavaScript -i
Safari = Application("Safari");
window = Safari.windows[0];
window.name();
tab = Safari.Tab({url:"http://www.example.com"});
window.tabs.push(tab);
window.currentTab = tab;
Simple, here replace the "APP" by name of the app you want to launch.
export APP_HOME=/Applications/APP.app/Contents/MacOS
export PATH=$PATH:$APP_HOME
Thanks me later.
With applescript:
tell application "Firefox" to activate
Why not just set add path to to the bin of the app. For MacVim, I did the following.
export PATH=/Applications/MacVim.app/Contents/bin:$PATH
An alias, is another option I tried.
alias mvim='/Applications/MacVim.app/Contents/bin/mvim'
alias gvim=mvim
With the export PATH I can call all of the commands in the app. Arguments passed well for my test with MacVim. Whereas the alias, I had to alias each command in the bin.
mvim README.txt
gvim Anotherfile.txt
Enjoy the power of alias and PATH. However, you do need to monitor changes when the OS is upgraded.
To Create a New Text File OR open an existing one, in any folder, using a Text/Code Editor like the Free TextMate app on MACOSX, use this command on Terminal:
open -n /Applications/TextMate.app --args "$PWD/some file.txt"
Instead of a Text File, you can use any file type, based on your app's requirements and its support for this syntax.
This command also simulates the New Text Document Here Command on Windows and has been tested on MacBook Pro 2021 and Monterey 12.2.1 successfully.

Executing Shell Scripts from the OS X Dock?

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.

Resources