How to run Script per shortcut - bash

I have a Terminal cmd running that executes a function from the .bash_profile file (as described here).
From the description, I added the function to the ~/.bash_profile (can be found here /home/user/.bash_profile)
From my Terminal everything works !
But how do I create a shortcut in Mac OS that executes this Terminal cmd ??
I tried Apple Automator (--> but error: Cmd not found)
I tried Alfred (--> but same error: Cmd not found)
How can I make this Terminal cmd execute from a keyboard-shortcut ??
Do I need to place the script-functions in another file (other than bash_profile ?) - or what is here to do ?
Here is an image of the Automator trial:

Place the functions in separate files, such as suspend-script.sh, resume-script.sh, and kill-script.sh; put them in a common directory like /home/user/scripts. To keep using them in your .bash_history, do:
# in .bash_history
source "suspend-script.sh"
source "resume-script.sh"
source "kill-script.sh"
Make sure you set the executable bit on each script (chmod +x /home/user/scripts/*.sh). Then, use the following for shortcut keybindings:
/home/user/scripts/suspend-script.sh thing_to_suspend
/home/user/scripts/resume-script.sh thing_to_resume
/home/user/scripts/kill-script.sh thing_to_kill
Since it's a shortcut to a function, you have to pick one script to suspend/resume/kill (the thing* above).
For example, if you wanted to suspend/resume/kill the top process, you would use the following:
/home/user/scripts/suspend-script.sh top
/home/user/scripts/resume-script.sh top
/home/user/scripts/kill-script.sh top

Open Automator, choose Application, add a Run Shell Script action and put in your Shell command between quotes (if you have a file, you can just drag and drop it).
Other than playing it, now you can save it (as an app anywhere) and even set the icon. Then you got an app and then you can define a keyboard shortcut for it in system-preferences.app
This should work, at least it does for me.
make sure your script is executable: sudo chmod u+x <your-scriptname>
and also add the filename extension .command to your filename. So the name of the script should be filename.command.
When you then click the script in Finder, it gets executed.
I hope that works!

Related

Make .sh file to .exe file [duplicate]

First off I'm using Mac.
Next, I need to execute this "file.sh" we will call it. Everytime I need to execute it I have to open Terminal and type:
cd /Users/Jacob/Documents/folderWithFileInIt
bash file.sh
This is okay, but I feel like it would be a lot quicker if I make the file execute on double click, don't you think?
So my question is, how do I make this file executable via double click?
My ideas were either:
a) type something like chmod into terminal and change permissions?
b) make a file, put code I wrote above in it ^ and then make that file executable?
c) make an automation somehow to do this?
Which way is best, or is there an even better way?
By default, *.sh files are opened in a text editor (Xcode or TextEdit). To create a shell script that will execute in Terminal when you open it, name it with the “command” extension, e.g., file.command. By default, these are sent to Terminal, which will execute the file as a shell script.
You will also need to ensure the file is executable, e.g.:
chmod +x file.command
Without this, Terminal will refuse to execute it.
Note that the script does not have to begin with a #! prefix in this specific scenario, because Terminal specifically arranges to execute it with your default shell. (Of course, you can add a #! line if you want to customize which shell is used or if you want to ensure that you can execute it from the command line while using a different shell.)
Also note that Terminal executes the shell script without changing the working directory. You’ll need to begin your script with a cd command if you actually need it to run with a particular working directory. E.g. you can use cd "$(dirname "$0")" to set the current working directory to the directory where your shell script lies.
Remove the extension altogether and then double-click it. Most system shell scripts are like this. As long as it has a shebang it will work.
You can just tell Finder to open the .sh file in Terminal:
Select the file
Get Info (cmd-i) on it
In the "Open with" section, choose "Other…" in the popup menu
Choose Terminal as the application
This will have the exact same effect as renaming it to .command except… you don't have to rename it :)
Launch Terminal
Type -> nano fileName
Paste Batch file content and save it
Type -> chmod +x fileName
It will create exe file now you can double click and it.
File name should in under double quotes.
Since i am using Mac->In my case content of batch file is
cd /Users/yourName/Documents/SeleniumServer
java -jar selenium-server-standalone-3.3.1.jar -role hub
It will work for sure
you can change the file executable by using chmod like this
chmod 755 file.sh
and use this command for execute
./file.sh
nano ~/FILENAME
Write your bash script and exit nano with Ctrl + x and hit y
Make file an executable
chmod 700 ~/FILENAME
Bingo, the file turns an executable, double click to launch
Works without .sh extension or shebang (#!) prefix.

On Mac OSX how can I associate a script file with Terminal so double clicking the icon launches Terminal?

I have a python script that I can run in the usual way using Terminal. For example, launch Terminal, cd to the directory, then type ./xxx.py where xxx.py is the name of the file that contains the script.
Now I want to make an icon on the desktop that launches Terminal and runs the python file when I double click it. How do I do that? I thought I could make a shell file with the cd and the launch command and then associate that shell file with Terminal. But I can't seem to associate it with Terminal.
Somebody said to name the shell file with the suffix '.command' but that causes it to launch Flash Builder. I don't know where that associate is set. I can't manually associate Terminal because I can't find it. It isn't in /Applications.
On your Desktop you coul create a file with the following content
#!/bin/bash
python PATH_TO_YOUR_PYTHON_FILE
Then you must make it executable via
chmod u+x FILE_ON_DESKTOP
Alternatively you can create a symbolic link to your python file and make it executable.
You can find Terminal in /Applications/Utilities

Mac execute bash script

I'm on a Mac, and have a bash script that works very nicely.
I'd like to make it so that a double-click will run it, but I don't know the "open with" operand. Please, what am I missing?
You'll need to make the file an executable.
On the first line, before any of your code put in a shebang
#!/usr/bin/env bash
REST OF YOUR CODE HERE
Next, you'll need to change the permissions. On the terminal run:
chmod +x your_bash_file
Finally, you will need to make sure OS X opens the file using the Terminal and not the application that created the file e.g. your favourite text editor. You can accomplish this in 1 of two ways:
Save the file with no file extension (eg. bash_file, instead of bash_file.sh)
Or, choose File -> Get Info and set Open with: to Terminal.app
You should now be able to click on the script to execute it!

How do I make this file.sh executable via double click?

First off I'm using Mac.
Next, I need to execute this "file.sh" we will call it. Everytime I need to execute it I have to open Terminal and type:
cd /Users/Jacob/Documents/folderWithFileInIt
bash file.sh
This is okay, but I feel like it would be a lot quicker if I make the file execute on double click, don't you think?
So my question is, how do I make this file executable via double click?
My ideas were either:
a) type something like chmod into terminal and change permissions?
b) make a file, put code I wrote above in it ^ and then make that file executable?
c) make an automation somehow to do this?
Which way is best, or is there an even better way?
By default, *.sh files are opened in a text editor (Xcode or TextEdit). To create a shell script that will execute in Terminal when you open it, name it with the “command” extension, e.g., file.command. By default, these are sent to Terminal, which will execute the file as a shell script.
You will also need to ensure the file is executable, e.g.:
chmod +x file.command
Without this, Terminal will refuse to execute it.
Note that the script does not have to begin with a #! prefix in this specific scenario, because Terminal specifically arranges to execute it with your default shell. (Of course, you can add a #! line if you want to customize which shell is used or if you want to ensure that you can execute it from the command line while using a different shell.)
Also note that Terminal executes the shell script without changing the working directory. You’ll need to begin your script with a cd command if you actually need it to run with a particular working directory. E.g. you can use cd "$(dirname "$0")" to set the current working directory to the directory where your shell script lies.
Remove the extension altogether and then double-click it. Most system shell scripts are like this. As long as it has a shebang it will work.
You can just tell Finder to open the .sh file in Terminal:
Select the file
Get Info (cmd-i) on it
In the "Open with" section, choose "Other…" in the popup menu
Choose Terminal as the application
This will have the exact same effect as renaming it to .command except… you don't have to rename it :)
Launch Terminal
Type -> nano fileName
Paste Batch file content and save it
Type -> chmod +x fileName
It will create exe file now you can double click and it.
File name should in under double quotes.
Since i am using Mac->In my case content of batch file is
cd /Users/yourName/Documents/SeleniumServer
java -jar selenium-server-standalone-3.3.1.jar -role hub
It will work for sure
you can change the file executable by using chmod like this
chmod 755 file.sh
and use this command for execute
./file.sh
nano ~/FILENAME
Write your bash script and exit nano with Ctrl + x and hit y
Make file an executable
chmod 700 ~/FILENAME
Bingo, the file turns an executable, double click to launch
Works without .sh extension or shebang (#!) prefix.

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