Create Desktop Shortcut Using SUDO

Ask about the individual Gambas components here.
Post Reply
User avatar
Askjerry
Posts: 64
Joined: Saturday 18th August 2018 9:06pm
Location: Kyle, Texas, USA
Contact:

Create Desktop Shortcut Using SUDO

Post by Askjerry »

I am using piGPIO for Gambas3, it has a bunch of great features and is my default programming environment now.

The thing is, it needs to be run as SUDO or the component library fails to initialize.

From TERMINAL if I type SUDO GAMBAS3 it comes up and runs perfectly.

I'm a lazy man... I just want to click on a desktop ICON and have it run. Sorry to be a newbe in this area... but how to I create a desktop ICON with the command "sudo gambas3" ???

Thanks in advance!
Jerry
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Create Desktop Shortcut Using SUDO

Post by BruceSteers »

Just use the path in the Home dir

This is how i might do it...

Dim sUser As String
Shell "w -h| awk '{print $1}'" To sUser

sDesktopPath = "/home" &/ sUser &/ "Desktop"

' Write the .desktop file , lets say it's called Launcher.desktop
File.Save(sDesktopPath &/ "Launcher.desktop", sFileData)
Chown sDesktopPath &/ "Launcher.desktop" To sUser  ' give ownership to normal user
Chmod sDesktopPath &/ "Launcher.desktop" To "rwxr-x---"  ' set executable flag

If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Create Desktop Shortcut Using SUDO

Post by BruceSteers »

check out this module of mine...

There's not docs so study the code.

it lets you make either a desktop icon or a menu item.

Here is how i use it...

Just drop the MakeIcon.module into your .src folder
the main command of the module is MakeMe()
MakeMe(Optional name As String = "", Optional icn As Image = Null, Optional comment As String = "", Optional menuCats As String = "", Optional Prefix As String = "gbr3", Optional UseRoot As Boolean = False)

name = Icon name
icn = icon image
menuCats = the menu categories, if null a desktop icon is made
prefix = what's put before the exe path (like gbr3)
(if you want your app to run as root set prefix to "pkexec gbr3")
(providing you have added gbr3 to pkexec rules see my app https://forum.gambas.one/viewtopic.php?f=13&t=883 on how to do that)
(or try my app groot https://forum.gambas.one/viewtopic.php?f=13&t=921)
(NOTE: those forum topics have old and updated versions, scroll to the last posts to get the links to the latest.)

useroot = if true icon is made for root otherwise for normal user
Public Sub btnMakeLauncher_Click()

  Dim sRes As Integer = Message.Question("Make a desktop Icon or a menu Launcher?", "Desktop", "Menu", "Cancel")

  If sRes = 3 Then Return

  If sRes = 1 Then
    MakeIcon.MakeMe("ScriptEd", FMain.Icon.Image, "ScriptEd script editor")
  Else
    MakeIcon.MakeMe("ScriptEd", FMain.Icon.Image, "ScriptEd script editor", "Application;Editor;Tools;Development;Programming")
  Endif

End
this function exists in my ScriptEd text editor, my gambas updaters, (and probably some other progs)

PS. it also adds a right click option (when icon is in a panel) to use gtk/qt gui environments (like the gambas3 icon does)
if you don't want that then change the following code...
s &= "StartupNotify=True\nActions=QT4;QT5;GTK;GTK3\n\n"
"[Desktop Action QT4]\nName=GB_GUI QT4\nExec=env GB_GUI=gb.qt4 " & Prefix & " '" & MyFullPath(True) & "'\n\n"
"[Desktop Action QT5]\nName=GB_GUI QT5\nExec=env GB_GUI=gb.qt5 " & Prefix & " '" & MyFullPath(True) & "'\n\n"
"[Desktop Action GTK]\nName=GB_GUI GTK2\nExec=env GB_GUI=gb.gtk " & Prefix & " '" & MyFullPath(True) & "'\n\n"
"[Desktop Action GTK3]\nName=GB_GUI GTK3\nExec=env GB_GUI=gb.gtk3 " & Prefix & " '" & MyFullPath(True) & "'\n"
To...
s &= "StartupNotify=True\n"
If at first you don't succeed , try doing something differently.
BruceS
User avatar
Askjerry
Posts: 64
Joined: Saturday 18th August 2018 9:06pm
Location: Kyle, Texas, USA
Contact:

Re: Create Desktop Shortcut Using SUDO

Post by Askjerry »

Wow... was not expecting that.

I thought it would be something like right-click and type this...

Looks like your code was written in Gambas... so if I understand you... put that code in (modified accordingly), run it, and it will create the desktop Icon / Launcher for the program.

I'll play around with this tonight and report back with an update.

Thanks!
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Create Desktop Shortcut Using SUDO

Post by BruceSteers »

aah yes lol I have better read your post now..

Just right click on Desktop and select "Create Launcher"
give it a name
set command to pkexec gambas3
or groot gambas3
https://gitlab.com/bsteers4/groot
groot has an install file in the project folder, just make the exe (it's gambas) and install.


the previous post was if you want your program itself to create it's own launcher (i misunderstood)
Last edited by BruceSteers on Thursday 15th July 2021 5:02pm, edited 2 times in total.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Create Desktop Shortcut Using SUDO

Post by BruceSteers »

or you could drag the gambas menu launcher to your desktop and edit the properties (just add pkexec or groot to the gambas3 command)
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1523
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Create Desktop Shortcut Using SUDO

Post by BruceSteers »

Hmm i have found pkexec to not work.
My app groot does though :) 8-)
advantage with groot is you can enter root password for sudo or leave blank to run as normal.
If at first you don't succeed , try doing something differently.
BruceS
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: Create Desktop Shortcut Using SUDO

Post by stevedee »

Askjerry wrote: Thursday 15th July 2021 2:31am ...I'm a lazy man... I just want to click on a desktop ICON and have it run. Sorry to be a newbe in this area... but how to I create a desktop ICON with the command "sudo gambas3" ???
The long answer;
You can create a desktop launcher with your text editor. Maybe start by copying this text for my WifiScanner...

Code: Select all

[Desktop Entry]
Version=1.0
Name=WifiScanner
Comment=Wifi Scanner which includes Link Quality & Channel identification.
Exec=sudo /home/steve/Gambas/WifiScanner/WifiScanner.gambas
Icon=xsensors
Terminal=true
Type=Application
StartupNotify=true
Categories=Network;Utility;
...then modify Name, Comment, Exec, Icon path & Categories to suit your application's requirements. This file must be saved to the desktop with a .desktop extension, e.g. JerrysApp.desktop

To run your app from this without the need to enter a password, one option is to modify the sudoers file. See second half of my post: http://captainbodgit.blogspot.com/2014/ ... ccess.html

Or, if you know which user group already has permissions to run pigpio, you could add the user (jerry) to the group (dialout) by running a command something like this in the terminal;

Code: Select all

sudo usermod -a -G dialout jerry
...user 'jerry' should then be able to run the application.
User avatar
Askjerry
Posts: 64
Joined: Saturday 18th August 2018 9:06pm
Location: Kyle, Texas, USA
Contact:

Re: Create Desktop Shortcut Using SUDO

Post by Askjerry »

You got me on the right track... it is working now!

[

Code: Select all

Desktop Entry]
Name=GAMBAS
Comment=Gambas Programming Language
Icon=/usr/share/pixmaps/openbox.xpm
Exec=lxterminal -e sudo gambas3
Type=Application
Encoding=UTF-8
Terminal=false
Categories=None;
Thank you!
Jerry
Post Reply