Issue with running a script from my desktop on Raspberry Pi - terminal

I have a simple script that I wanted to run from my desktop to make it easier. I've put it into a .desktop file and when double clicked it opens a small window saying "This text file 'alerter.desktop' seems to be an executable script. What do you want to do with it?" with the options being "execute" "execute in terminal" "Open" " cancel". When clicking either execute option, the terminal flashes for a second and then disappears. The script works fine when typed into the terminal.
File contents:
...
[Desktop Entry]
Name=alerter
Comment=Starts alerter
Exec=lxterminal -e ./docker_run.bash -c ./config/discord-alerts.yaml -a discord -w
https://discordapp.com/api/webhooks/blahblahblah
Terminal=true
Type=Application
Path=/home/pi/inventory-hunter
...
Thanks in advance for any help!

Try to wrap your exec command in this way:
sh -c "lxterminal -e ./docker_run.bash -c ./config/discord-alerts.yaml -a discord -w"
Also set terminal to false.

Right Click on the file and select Properties
Choose Terminal Application in Open with option
If not present in the list ( Got to Customise -> Accessories -> Terminal)
Open File Manager
Go to Edit -> Preferences
Check the option "Don't Ask options on launch executable file"
Good to GO !!!!!  

Related

Using a shell script to call a ROS launch file

I want create a file *.sh and run it as an executable like any other software with double click on it. The command is the follwing
roslaunch my_pro test_qt.launch
I simply run it in the terminal and it launches my software. Now I want to make it as an executable, so I tried this but it didn't work
gnome-terminal -e roslaunch my_pro test_qt.launch &
it said:
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.#
# Use “-- ” to terminate the options and put the command line to execute after it.#
How can I write my *.sh file?
EDIT
Something like that?
#!/bin/bash
gnome-terminal -e
roslaunch my_pro test_qt.launch
I know this post is old, but i recently wanted to do a similar thing (roslaunch using desktop shortcut ) and gave me some trouble the past few days so I decided to write the steps i took for anyone that might be interested in the future.
First of, I created a hidden folder in my home directory to place the script and image I was going to use for the shortcut:
mkdir .dir_name
I then placed the image I wanted to use in there and created a script using the sublime text editor ( my choice, you can use your favourite one - it makes absolutely no difference at all )
subl .dir_name/launch.sh
The launch file looks like this:
#!/usr/bin/env bash
# Launch the robot
source /opt/ros/melodic/setup.bash
source /home/user/ros_ws/devel/setup.bash
echo "Launching application, please wait!"
roslaunch your_pkg your_launch.launch
Important note: you MUST include the sourcing of the bash files, otherwise the roslaunch command is not recognised!
Important note 2: you MUST make the script executable
chmod +x .my_dir/launch.sh
The next step is to create the desktop shortcut:
subl ~/Desktop/my_shortcut.desktop
and adding the following inside the file what we just created:
[Desktop Entry]
Type=Application
Terminal=false $ true opens extra gnome-terminal
Name=Robot Launch
Icon=/home/user/.my_dir/logo.png
Exec=terminator -e "bash -c '/home/user/.my_dir/launch.sh;$SHELL'"
$Exec=gnome-terminal -e "bash -c '/home/user/.my_dir/launch.sh;$SHELL'"
Note: I am using terminator, if you are using the default gnome terminal, comment the terminator line and uncomment the gnome-terminal line.
Finally, you need to right click the desktop shortcut ( icon will appear after it has been run once ) and go to permissions tab. There you must click
allow executing file as program
Once the shortcut is now double clicked it will run. The very first time you double click the system will ask you again if you want to execute this file (for security reasons) and you must click yes ( after all its your file ;) )
That's it! Enjoy your desktop shortcut launching ros code!

How do I run a script on Linux just by double clicking it

I have a script written (which are basically the commands for the terminal for Ubuntu) on a file. Yes, the code starts with
#!/bin/bash
How do I run this script just by double clicking it? It can be run using the terminal but I want to make it more accessible through my desktop.
I was just trying to mimic a *.bat file on windows. A *.bat file on windows contains series of scripts operable on command prompt and executable just by double clicking on it.
Follow these steps:
Hit Alt+F2, type dconf-editor and hit ``Enter.
In dconfg-editor goto: org ➤ gnome ➤ nautilus ➤ preferences
Click on executable-text-activation and from drop down menu select:
launch: to launch scripts as programs.
OR
ask: to ask what to do via a dialog.
Close dconf-editor Thats it!
Source https://askubuntu.com/a/286651/113065
Edit -> preferences -> Behavior -> Executable Text Files = Run ...
It should run, but you can't see output.
An alternative way, mostly for developers, is to create self-contained desktop launchers. The idea is to use a launcher that includes a tag under which there is the script; the launcher executes a command that:
put the script into a temporary file
run the script which deletes itself
Of course, the script should delete itself, for this reason, it's a good practice to start the script with rm $0. Note that if the script needs to read the script itself, you will need to use rm $0 somewhere else...
You can use the following example stolen from my project:
#!/usr/bin/env xdg-open
[Desktop Entry]
Name=Launch Assistance
Comment=A simple app to setup remote assistance
Exec=ttt=/tmp/$(date +"%s%N").sh; tail -n+$(awk '/^#__SCRIPT__#/ {print NR + 1; exit 0; }' %k) %k > $ttt ; sh $ttt;
Icon=computer
Terminal=true
Type=Application
Categories=Network;Internet;Utilities;Administration;Settings;
#__SCRIPT__#
rm "$0"
# Put here the script
# note that if the script needs to read $0, you will have to edit it
You need to make it an executable file, either use chmod +x <filename> or go into the file properties and set it there.
Open nautilus
Behavior tab
Executable Text files -> Ask each time
Profit
Source. Tested on Ubuntu 20.04.

Run a script by double clicking on it in OSX

I have a script that I'd like to run.
I know I can open a terminal and run the command:
open -na /Applications/Skype2.app \
--args -DataPath /Users/$(whoami)/Library/Application\ Support/Skype2
But how can I run it by double clicking on the script, similar to an app or exe file in windows?
An easy way is use the Automator app to create an "Application" containing a Run Shell Script action.
If thats too GUI for you, see https://stackoverflow.com/a/5126052/153099

How to open webstorm from terminal

To edit files from terminal I use subl (for sublime text) in order to edit the file;
example: If i need to edit app.js file I use subl app.js
Is there any way I can set up webstorm to open from the terminal ?
Try in terminal 'wstorm' and 'webstorm'
If the commands don't work you can run in WebStorm: "Tools" -> "Create Command Line Launcher..."
Note: The solution works only for Linux / MacOS
Update January 2016 using Webstorm 11.0.3 on mac os x
There was no option as described in the accepted answer.
Instead, just use the already installed command line binary wstorm designed for this purpose. Location shown below:
If you actually wish to open webstorm and have it load the contents of the current working directory for example, then place a . after the command:
wstorm .
Noted, others had made similar comments in this answers section, and wished to clarify the situation.
In Webstorm 2020.1.2 you need to do it via JetBrains ToolBox Settings. To do that go to JetBrain Toolbox, click on the settings cog, open Shell Scripts and type the path: /usr/local/bin click apply. Go to your terminal, from your project folder type webstorm . Hope this helps.
As suggested by Ali Faris(comment below), if you have an error like this Shell Scripts failed: /usr/local/bin/webstorm (Permission denied): inside of the logs
Jetbrains Toobox -> settings -> show log files -> toolbox.log (for me in: ~/Library/Logs/JetBrains/Toolbox).
Change /usr/local/bin to another folder name of your choice with the correct access rights, e.g - I chose this name: ~/.jetbrains-launchers.
You can check if script is created by Jetbrains: ls ~/.jetbrains-launchers (you should see a script for each of the jetbrains applications you use).
Add this to your path if needed for example if you use zsh add this at the bottom of your .zshrc export PATH="$HOME/.jetbrains-launchers:$PATH"
Open a new terminal window and this should work.
Basically jetbrains will create script like this (in this case for webstorm cat ~/.jetbrains-launchers/webstorm):
#!/bin/bash
#Generated by JetBrains Toolbox 1.22.10970 at 2022-01-08T12:57:24.803251
declare -a ideargs=()
declare -- wait=""
for o in "$#"; do
if [[ "$o" = "--wait" || "$o" = "-w" ]]; then
wait="-W"
o="--wait"
fi
if [[ "$o" =~ " " ]]; then
ideargs+=("\"$o\"")
else
ideargs+=("$o")
fi
done
open -na "/Users/[YOUR-USER]/Library/Application Support/JetBrains/Toolbox/apps/WebStorm/ch-0/213.6461.79/WebStorm.app/Contents/MacOS/webstorm" $wait --args "${ideargs[#]}"
I also downloaded WebStorm and wanted to use a similar shortcut to open files directly from the terminal.
I was surprised to find I already had a shortcut in my command line tools for webstorm:
subl is to Sublime as wstorm is to Webstorm.
Otherwise, as anstarovoyt has kindly pointed out, you can simply create your own shortcut via "Tools" > "Create Command Line Launcher"
Another way to do that:
open -a /Applications/WebStorm.app #Open last project
open -a /Applications/WebStorm.app Desktop #Open particular folder
open -a /Applications/WebStorm.app Desktop myscript.js #Open particular file
You can add alias to your config file:
#Edit your config:
vim ~/.bashrc
#add line:
alias ws='open -a /Applications/WebStorm.app'
#Read your config file:
source ~/.bashrc
Now you can use it:
ws . myscript.js
I know this is an older thread, but trying to achieve this using Windows was kind of a pain and I wasn't able to find anything specifically designed for my purposes. I created a Bash function that you can add as an alias (for Git Bash on Windows) that works similar to the command line functions in Visual Studio Code.
Here's the link to the Gist.
If you change the integrated terminal in WebStorm to Git Bash (instructions included in the Gist), you can perform the following actions:
Create a new file in the current working directory and open it in the editor:
wstorm foo.js
Create a new file in an existing relative path and open it in the editor:
wstorm foo/bar.js
This also works with subdirectories that don't exist:
wstorm this/path/doesnt/exist/file.js
If you're working in a Git Bash terminal (not in WebStorm) and want to open WebStorm up in the current directory, you can open it similar to Visual Studio Code:
wstorm .
Note: This needs to be done in a directory with a .idea folder.
As of 2019-03-09, WebStorm 2018.3.4 on Mac does not have Tools > "Create Command Line Launcher...". However, this works:
WebStorm Preferences > Keymap > Main Menu > Tools > Create Command-line Launcher...
Right-click "Create Command-line Launcher..." > Add Keyboard Shortcut
Assign a keyboard shortcut
Close Preferences
Type the keyboard shortcut to open "Create Launcher Script"
Click Ok to run the script
You can now launch WebStorm from the terminal with webstorm and can choose a directory to open
After setting up WebStorm to create the cli launcher you actually want to run
wstorm . &
to run the IntelliJ on the background otherwise IntelliJ closes if you happen to close the terminal you have launched the app from.
In WebStorm IDE, click DOUBLE CLICK ON SHIFT and type Create Command Line Launcher then click OK from luncher script promote .
cd project_folder_path using terminal and type webstorm ./ .
that is not for Windows OS
In Ubuntu terminal type:
/var/opt/webstorm6/WebStorm-129.664/bin/webstorm.sh
Note: please see your WebStorm build version, code mine is 129.664
In the terminal, while being in the given project folder:
webstorm .
I know that this is a pretty old thread, but I recently came across this problem on Windows (I'm using the JetBrains Toolbox).
With the following steps all new and existing applications that have been installed with the Toolbox will be added to your path!
Follow these steps to achieve this:
Because of permissions, we need to create a new directory in your user. I named it .path, so that I can also store any other application there in the future. So this will be C:\Users\<PC_USER>\.path\.
The the Toolbox click on the gear icon in the top right corner.
Then click on Enable Shell Scripts and/or Generate Shell Scripts.
In the input field that is located under the switch paste your path folder. (C:\Users\<PC_USER>\.path\)
Open your Edit the system environment variables program that can be found in Windows search or the control panel.
Click on the Environment Variables... button that is located in the right corner, a new window should pop up.
In the new window select the variable that says Path in the Variable column from the top list and then click on the edit button that is situated under the top list. Another new window should pop-up.
Click on new and paste your path there. (C:\Users\<PC_USER>\.path\)
Click on Ok in Edit environment variable > Environment Variables > System Properties.
Go to C:\Users\<PC_USER>\.path\ and all your toolbox installed applications should be there.
Restart your CLI and it should work.
The wstorm command didn't work in my Git bash, so I added the following function to my .bash_profile instead:
wstorm() {
/c/Program\ Files\ \(x86\)/JetBrains/WebStorm\ 2016.2.2/bin/WebStorm.exe $PWD/$1
}
A short solution relevant to the year 2021 for Linux users.
Just execute the comand:
sudo ln -s /<your path to Webstorm directory>/bin/webstorm.sh /usr/local/bin/webstorm
Since /usr/local/bin should be in the PATH environment variable by default, you should be able to run the webstorm command from anywhere in the shell.
More details Webstorm docs
I am running Windows 10 and whipped up a batch file (ws.bat) that implements this with optional command line argument for path to load).
:: place this batch file in your path and set to your WS EXE
:: ref: https://www.robvanderwoude.com/battech_defined.php
:: author: bob#bobchesley.net
#echo off
set target=%1
if defined target (goto passedarg) else (goto noarg)
:passedarg
echo Starting WebStorm with '%target%'
"C:\Program Files\JetBrains\WebStorm 2018.3.2\bin\webstorm.exe" %target%
goto:EOF
:noarg
echo Starting WebStorm with 'Current Dir'
"C:\Program Files\JetBrains\WebStorm 2018.3.2\bin\webstorm.exe" .
Pretty simple but it works.
webstorm . doesn't work on Windows. Try this for the current folder:
webstorm $pwd
$pwd is the current folder's path

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