Executing a shell script on double click - shell

I have a shell script which i constantly use to open my jidea IDE
i created a soft link of the script to my desktop which is expected
to act as a desktop shortcut.
But it will also in-turn execute as a
terminal instance.
I tried to disown the terminal instance after it
was executed, which did not work.
Is there any-other way in which i can execute this as a background process maybe?
Any help is appreciated...
Thanks for your time! :)

First be sure your script has the execution permission.
If you are using nautilus here how to fix
Open nautilus
go to Edit > Preferences > Behavior
checkExecutable text files and select Run executable text files when they are opened

Related

How to create AutoHotKey which starts script (i.e .bat) using Cmder command line arguments?

I am trying to create an autohotkey which starts a script (i.e .bat):
#!g::
Run "Path\To\script\script.bat" debug
Return
The AutoHotKey works properly. However, I would like the .bat to be opened using Cmder instead of the default windows cmd.
I tried to set the Cmder as the default cmd by going to Settings-> Integration -> Default term and by checking the "Force ConEmu as default terminal for console applications", but this did not change anything in my particular case, and the script is still being ran in windows cmd.
Assuming Cmder can take a command line parameter, make it explicit:
Run "Path\To\exe\Cmder.exe Path\To\script\script.bat" debug
Else, run Cmder and then use AHK to file open the script.
EDIT: Based on OP's solution, could try:
Run "Path\To\exe\Cmder.exe /Task ""Path\To\script\script.bat debug"""
or
Run "Path\To\exe\Cmder.exe /Start ""Path\To\script"" /Task ""script.bat debug"""
The exact syntax and whether and where to surround things with quotes is tbd, but try and see.
In the end I used below script to make it work:
; Win+Alt+y - Start script
#!y::
Run "d:\cmder\Cmder.exe" "d:\somePath\FolderContainingTheBat\"
sleep 1000
send script.bat debug{enter}
Return
EDIT
Another solution for this does not involve AutoHotKey and works only with default windows cmd. I did the following:
Create a .bat file which contains the following
script.bat debug
Create shortcut for the newly created bat file and assign to it a "keyboard shortcut": Right click on the created shortcut -> Properties -> "Shortcut" tab -> Shortcut key
I made it work with this
Run, C:\path\Cmder\vendor\conemu-maximus5\ConEmu64.exe C:\path\to\script.exe

Executing a command-line .exe file

I have a .exe file converted from a .jar.
It is a command based application, so I have to start it with a batch script. Here is the batch script:
#echo off
cd C:\desktop\plant-text-adventure-win
start planttextadventure
pause
When I double click on the batch script, this happens: Windows could not find 'planttextadventure'. Please confirm if you have input the correct name and retry.
I don't know what is happening, I have no idea about cmd as I use Mac, but I can confirm I have an executable called planttextadventure.exe in a folder called plant-text-adventure-win.
You should test your batch file by executing it within a shell.
Simply enter within the start menu the command cmd to open up a shell. Within this black box you could now simply enter the commands from your batch script and lookout for some error message.
If you look at your script I would guess that the cd command (to change the current directory) is not correct. Maybe you should replace it with
cd %USERPROFILE%\Desktop\plant-text-adventure-win
because the desktop folder is on a default installation not directly under the root drive but within the user profile available.
Another solution to get this thing to work, is by opening the windows explorer, going to the .exe file you wish to execute and drag & drop the .exe file with a right mouse click onto the desktop.
Then a context menu appears and you select the option Create shortcut here.

How to run this Terminal line in Automator's Run Shell Script?

I am a Terminal newbie and installed aria2 on my Mac. When I need to use aria2, I open up Terminal and enter "aria2c" without touching anything else, it works great.
Then I want to create an application to run this line with Automator so I can just drop it to the dock and click to start aria2. I tried but no luck, any help please!
Fixed it with the following:
/usr/local/aria2/bin/aria2c

Close the command prompt window programmatically

I have a set of commands in batch file which is used to run the JAR file.It is working perfectly.
After running the program, the command prompt window doesn't get closed. If I manually close the window, then the application closes as well.
So I want to close the command window without affecting the application.
thanks in advance.
Either use exit or make a new method to go to and have nothing in it. The exit way is better though. :)
My psychic debugging powers tell me you're launching the JAR file using a java command instead of a javaw command.
Use javaw instead and the command window will close when the batch file exits.

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