Run shell script on keyboard shortcut

13,734

Solution 1

I'm pretty sure the problem is the ~. That is a shell (commandline shell) feature and the keyboard shortcuts are handled by the graphical shell. I don't think that ~ or $HOME will be expanded correctly there. So, instead of ~/path/to/script, use

/home/username/Documents/Various/SyncStuff.sh

That assumes you have made the script executable. If not, use

sh /home/username/Documents/Various/SyncStuff.sh

The sh should be enough since the $PATH is also read by graphical shells as far as I know. If that also fails, try with the full path but I doubt it will be necessary:

/bin/sh /home/username/Documents/Various/SyncStuff.sh

Solution 2

Awesome post. I've been searching for a way to do this for the last few versions of ubuntu (currently on 19.04)

Using the full path is what did it. Thank you. What worked for me was the following...

I was looking for a way to set a hotkey to insert the date / time into whatever document I had open.

I found a script online and made a few minor changes (primarily the hotkey association because I didn't like their choice since it was a key combo that was already used by ubuntu) to one I wanted and which was also available.

Created a file called 'insertTimestamp.sh' in my root directory.

I then pasted (and slightly modified) the code to my liking...

Copy/paste the code below

# ! /bin/sh
xdotool keyup "super+t"; # needed to refresh the status of the request.  simulates letting go of the hotkey super+t keys, otherwise it interferes with ctrl-v paste function
date '+%Y-%m-%d %H:%M:%S' | tr -d "\n" | xsel -i -b; # add the date/time to the clipboard
xdotool key "ctrl+v"; #simulate a ctrl-v
# xdotool keydown "super"; #unpretend the user let go of the super key

Once I pasted the above into the file and saved it, it was necessary to change the permissions of the file

chmod u+x ~/insertTimestamp.sh

Once done, you simply need to search for "keyboard" => choose Keyboard => Go to Application Shortcuts Tab => Add => For "Command" type sh the/full/path/insertTimestamp.sh (update your full path) => Set keys as super+t.

I hope this provides a decent example of how to set this up.

enter image description here

Share:
13,734

Related videos on Youtube

TomTom
Author by

TomTom

Updated on September 18, 2022

Comments

  • TomTom
    TomTom over 1 year

    I would like to run a shell script on the press of a button. The script works if I run it like usually: sh script.sh. But assigning a shortcut to it doesn't work.

    Screenshot of shortcut menu

    How would you do this in Xubuntu 14.10?

  • Hop hop
    Hop hop about 2 years
    sh /home/username/Documents/Various/SyncStuff.sh worked for me!