How do I make a desktop icon to run a script from?

Forum rules
Before you post please read how to get help
SteveH_66
Level 2
Level 2
Posts: 57
Joined: Wed Feb 08, 2012 10:08 pm

How do I make a desktop icon to run a script from?

Postby SteveH_66 » Sun Apr 09, 2017 5:05 am

Newbie here, need some help. I don't know much about shell (bash?) scripting, or about creating icons on desktop/application launcher - beyond some simple basics of icon creation. But, through research on the net, then trial and error on my own, I finally managed to get a script written that lets me use 'rfkill' command to block/unblock bluetooth. It seems to work fine if I open it up in a terminal window and run it from the command line.

But now I am trying to run the shell (bash?) script by clicking on a desktop icon. I can copy the script to the desktop, and it will run if I right-click on it and select "run from terminal". But it doesn't work right, doesn't close the terminal window and seems to lock the terminal window up, the cursor just sits there. I don't know how to get it back to the command prompt when something goes wrong like this, so I just close the terminal window.

Is there a special way for creating a desktop icon or application launcher link(?) for running a script?

Is there a way to have it execute the script (correctly) by just double-clicking on the icon?

I seem to have muddled my way through getting the script written and working, but not doing so well on getting it to run from an icon. :lol: Appreciate any help someone can give me. Thanks

By the way, how do I get back to the command prompt in terminal when something goes off-the-rails like this? Thanks
System 1: Acer-Aspire V3-472P-324J. Hardware : 14" Touch Screen dual-band 2.4GHz & 5.0GHz Acer Nplify wireless LAN (802.11a/b/g/n); BT 4.0; OS : Linux Mint 17.3 Rosa; Kernel: 3.19.0-32-generic x86_64 (64 bit) MATE 1.12.0

ColdBoot
Level 3
Level 3
Posts: 196
Joined: Thu Feb 16, 2017 10:40 pm

Re: How do I make a desktop icon to run a script from?

Postby ColdBoot » Sun Apr 09, 2017 10:17 am

SteveH_66 wrote:
By the way, how do I get back to the command prompt in terminal when something goes off-the-rails like this? Thanks


By pressing Ctrl+C.

You have to make a script executable - right click on it(in file manager), than click the permissions tab and check the corresponding option there. Then it will not show you the dialog you've seen previously when run by double-clicking the icon you've created for it on the desktop.
Linux Mint Cinnamon 18.1
Intel G1820, DDR3 8GB, Nvidia GT720(2GB)

SteveH_66
Level 2
Level 2
Posts: 57
Joined: Wed Feb 08, 2012 10:08 pm

Re: How do I make a desktop icon to run a script from?

Postby SteveH_66 » Mon Apr 10, 2017 10:33 am

ColdBoot wrote:
SteveH_66 wrote:
By the way, how do I get back to the command prompt in terminal when something goes off-the-rails like this? Thanks


By pressing Ctrl+C.

You have to make a script executable - right click on it(in file manager), than click the permissions tab and check the corresponding option there. Then it will not show you the dialog you've seen previously when run by double-clicking the icon you've created for it on the desktop.


Thanks for the reply ColdBoot. The script is already executable. As far as pressing Ctrl+C in the terminal window, something isn't working right with that, if I press it I just get ^C printed out in the screen however many times I hit Ctrl+C. But I really appreciate you taking the time to look at my post and your effort to help. I guess there must be something else at work here.

As I said in the first part of my post, If I open up a terminal window first, then type in the name of the script "bluetoothOnOff" and hit enter, it pops up, asks me for my sudo password, lets me choose whether to turn it on or off (I have it set up to do 'rfkill list bluetooth' which shows bluetooth status right away), and then executes whatever choice I make, and returns to the command prompt correctly. And I can run the script over and over, popping back and forth from bluetooth on - bluetooth off - bluetooth on, etc. So the script is working fine, and it shows as executable when I right-click on it in Dolphin file manager, go to properties, and click on permissions tab the "is executable" checkbox is checked.

If I double-click on the script file in Dolphin, nothing seems to happen. If I copy the script to the desktop to use as an "icon" and double-click on it, nothing seems to happen. Can double click over and over, nothing.

That was why I posted my original topic asking if there is some special way, for a shell(?) script, you have to make an icon (on the desktop) or a link(?) (in application manager).

If I right-click on the script from within Dolphin file manager, go to options> then click on 'run in Konsole' the script opens fine, and it asks for and accepts my sudo password, and it executes 'rfkill list bluetooth' just fine giving me bluetooth status, then shows the choice line where I can pick y/n/q. When I make a selection, it goes buck-wild. Konsole freezes, and if I hit enter I just get new line after new line. If I hit Ctrl-C as you said to do, it prints ^C over and over. Here's what the Konsole screen will look like:

Code: Select all

[sudo] password for steve:
1: acer-bluetooth: Bluetooth
        Soft blocked: yes
        Hard blocked: no
Do you want to block or unblock bluetooth? (y/n/q)? n








^C
^C^C
^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C



^C

The Konsole window will have a window title of "bin : bluetoothOnOFF" instead of the usual "bin : bash" also.

The script doesn't do some things I would like right now, because I don't know anything about the scripting language.

It won't repeat and show you the bluetooth status again and ask you if you want to turn bluetooth on or off again until you hit q --- with the information I could find on the Net the only thing you can get it to do is just run once and exit after it finishes executing the choice you made.

It would be nice to be able to choose "y" and have it block and repeat the step of getting the bluetooth status and asking for your choice again, "n" and have it unblock and show bluetooth status again and ask for your choice again, or "q" and have it exit the script. That way, if you made a mistake or changed your mind, you could make the selection again and change it back, then hit "q" and exit the script when you were ready to.

As I originally asked, I would really love to get a working icon I could double-click and get the script to execute correctly.

But if anyone could help me mod the script so that it did as I just described, then I would have something really nice. I am going to post the code of the script, so everyone can get a look at what I did and have an idea of what they're working with here to maybe help me modify it. Thanks for any assistance any of you folks can give me.

Code: Select all

#!/bin/bash
#this will get user input to turn bluetooth on and off

(sudo rfkill list bluetooth)

echo -n "Do you want to block or unblock bluetooth? (y/n/q)? "
read answer
if echo "$answer" | grep -iq "^y" ;then
   (sudo rfkill block bluetooth)
elif echo "$answer" | grep -iq "^n" ;then
   (sudo rfkill unblock bluetooth)
else
   echo "Doing nothing, exiting"
fi

exit
System 1: Acer-Aspire V3-472P-324J. Hardware : 14" Touch Screen dual-band 2.4GHz & 5.0GHz Acer Nplify wireless LAN (802.11a/b/g/n); BT 4.0; OS : Linux Mint 17.3 Rosa; Kernel: 3.19.0-32-generic x86_64 (64 bit) MATE 1.12.0

User avatar
Flemur
Level 13
Level 13
Posts: 4658
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: How do I make a desktop icon to run a script from?

Postby Flemur » Mon Apr 10, 2017 3:01 pm

SteveH_66 wrote: The script is already executable. As far as pressing Ctrl+C in the terminal window, something isn't working right with that, if I press it I just get ^C printed out in the screen however many times I hit Ctrl+C.

That means it either ended or is running in background; to bring it to foreground, type 'fg'.

I haven't studied your whole post, but you probably want to run the script like this:

Code: Select all

terminal-name -e script

or

Code: Select all

terminal-name -e sh script

The terminal will appear and, unless it asks for input, disappear, usually almost immediately....so make it stay visible, ask for input at the end, just something like "OK, I'm done, hit enter".

You also might look at 'zenity' - puts up a simple GUI; here's an example
https://help.gnome.org/users/zenity/stable/info.html.en

Code: Select all

#!/bin/bash

zenity --info --text="Merge complete. Updated 3 of 10 files."
Mint 18.3 Xfce/fluxbox/pulse-less
Xubuntu 17.10/fluxbox/pulse-less
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?

ColdBoot
Level 3
Level 3
Posts: 196
Joined: Thu Feb 16, 2017 10:40 pm

Re: How do I make a desktop icon to run a script from?

Postby ColdBoot » Mon Apr 10, 2017 4:12 pm

I honestly don't know how Dophin and the new Plasma handle scripts (try asking about it in KDE section of the forum) however, you should make a shortcut to it instead of copying it to the desktop. As Flemur pinpointed, use zenity to get user input instead of console and kdialog (if it's still in use) to provide feedback from the script.

The alternate way to stop a script is to use killall command in terminal:

Code: Select all

killall your_script_name


It will return "No such process" if the script is not running.
Linux Mint Cinnamon 18.1
Intel G1820, DDR3 8GB, Nvidia GT720(2GB)

lmuserx4849
Level 5
Level 5
Posts: 500
Joined: Wed Dec 17, 2014 2:55 am

Re: How do I make a desktop icon to run a script from?

Postby lmuserx4849 » Wed Apr 12, 2017 9:55 pm

If you right-click your desktop, is there a menu option like, Create New -> Link to Application. This is going to create a FILE.desktop file in $HOME/Desktop. You can either edit it directly or right-click the icon and select Properties.

One of the Property tabs displays the command. If there are Advanced Options, check them out ("Run in terminal', "Do not close when command exits").

Check out this thread: How to run a shell script from cinnamon menu. There's info on the desktop file and what the command line could look like.

ColdBoot
Level 3
Level 3
Posts: 196
Joined: Thu Feb 16, 2017 10:40 pm

Re: How do I make a desktop icon to run a script from?

Postby ColdBoot » Thu Apr 13, 2017 9:02 am

I think the simplest way would be to create a shortcut that runs the script with super user privileges necessary to turn bluetooth on/off

Code: Select all

zenity --password | sudo -S path_to_script


All other user input needed, should be gathered via zenity as well and then there's no need for terminal display.
Linux Mint Cinnamon 18.1
Intel G1820, DDR3 8GB, Nvidia GT720(2GB)

jk50
Level 1
Level 1
Posts: 1
Joined: Fri Apr 14, 2017 3:03 am

Re: How do I make a desktop icon to run a script from?

Postby jk50 » Fri Apr 14, 2017 3:27 am

If I'm reading you right the following script should do what you want.

Code: Select all

#!/bin/bash
#this will get user input to turn bluetooth on and off
int_handler()
{
    echo -e "\r\n\r\n***Interrupted***"
    sleep 3
    # Kill the parent process of the script.
    kill $PPID
    exit 1
}
trap 'int_handler' INT
(sudo rfkill list bluetooth)
while true
do
echo -n "Do you want to block or unblock bluetooth? (y/n/q)? "
read answer
case $answer in
   [Yy]) (sudo rfkill block bluetooth)
         echo -e "\r\n*** Bluetooth has been blocked ***\r\n" ;;
   [Nn]) (sudo rfkill unblock bluetooth)
         echo -e "\r\n*** Bluetooth has been unblocked ***\r\n" ;;
   [Qq]) echo -e "\r\n*** Doing nothing, exiting ***"
         sleep 3
         exit 0 ;;
    *)   echo -e "\r\n*** What? your choices are y, n, or q ***\r\n";;
esac
(sudo rfkill list bluetooth)
done


Copy the above and save as bluetoothOnOff.sh, make sure it's executable.
To get a desktop icon,copy what's below and save as "bluetoothOnOff.desktop"
Edit line 5 of bluetoothOnOff.desktop to reflect the proper path to wherever you saved bluetoothOnOff.sh
Open dolphin, then right click on the desktop, click Unlock Widgets, then drag'n'drop bluetoothOnOff.desktop to an
empty space on desktop. Right click on desktop again , and click Lock Widgets

Code: Select all

#!/usr/bin/env xdg-open
[Desktop Entry]
Comment[en_US]=
Comment=
Exec=konsole --separate -e "/home/j/bluetoothOnOff.sh"    # edit this line to reflect proper path to bluetoothOnOff.sh
GenericName[en_US]=
GenericName=
Icon=preferences-system-bluetooth
MimeType=
Name[en_US]=bluetoothOnOFF
Name=bluetoothOnOFF
Path=
StartupNotify=true
Terminal=false
TerminalOptions=
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=none
X-KDE-SubstituteUID=false
X-KDE-Username=

SteveH_66
Level 2
Level 2
Posts: 57
Joined: Wed Feb 08, 2012 10:08 pm

Re: How do I make a desktop icon to run a script from?

Postby SteveH_66 » Fri Jun 30, 2017 12:06 am

Thanks for all that great help folks, I will try to implement your ideas when I get a chance, right now I'm trying to chase down more problems that cropped up when KDE finally just messed everything up (going pretty much to just a black screen after a couple of seconds of showing the KDE desktop) and I was forced to try to recover the best I could and went ahead and installed Mint 17.3 with MATE this time. I don't know how having MATE as my desktop now will affect your posts as to how to I can make a working desktop icon for my script on MATE. MATE seems much different than KDE. I don't think they even use the same drivers let alone the same packages/software for the different things. As a matter of fact, I don't really need the script any longer, although I am still going to try to set it up with a working desktop icon as a learning exercise. I don't need the script to turn off Bluetooth on MATE any more that works just fine from the icon on the panel bar at the bottom of the screen now. Unfortunately, while it will pair with a Bluetooth device just fine in MATE, it absolutely refuses to connect with any Bluetooth device I have in my possession. :(
System 1: Acer-Aspire V3-472P-324J. Hardware : 14" Touch Screen dual-band 2.4GHz & 5.0GHz Acer Nplify wireless LAN (802.11a/b/g/n); BT 4.0; OS : Linux Mint 17.3 Rosa; Kernel: 3.19.0-32-generic x86_64 (64 bit) MATE 1.12.0


Return to “Scripts & Bash”