KDE: klipper action script - bash

So KDE's clipboard manager - klipper - allows one to write a script to be applied to clipboard contents matching regexp. Say, I want klipper to download an image through bash script.
Here's a klipper QRegExp:
^http://.*\.(png|svg|gif|jpg|jpeg)
I know that this regexp works - klipper notifies me every time I copy image URL to clipboard. Then, here's a bash script
#!/bin/bash
# let's name it clip.bash
name=`basename $1`
curl -o ~/Downloads/$name $1
I put this script to the PATH (I tried to feed this script with a image URL my self - it works), and finally I specify an action the following way:
clip.bash \%s
everything's fine and taken care about - but it doesn't work!
So my question is: "how to make klipper download an image through the bash script?"

First thoughts:
Are you sure about the backslash before the '%'? I haven't access to KDE right now, but I'm not sure you need it.
Are you sure that klipper "sees" your change to the PATH variable? You could try to use absolute path (something like "/home/../clip.bash")
If those don't work, you can try to log some debug info from your script. For example:
#!/bin/bash
name=`basename $1`
echo "curl -o ~/Downloads/$name $1" 1>&2
Run
tail ~/.xsession-errors
to see what command your script have just tried to execute.

Related

Variable for a right-clicked item (say, a jpg) in a bash script?

I have a very simple bash script that I run often from the cli, but I've found it's frustrating to have to open a terminal, identify the right file, and run it and think the easiest way would be to run it as an option from a right-click. I am running Ubuntu 18.04 LTS.
The script is just erasing exif data, leaving the orientation tags, essentially this:
exiftool -all= -tagsfromfile # -Orientation file-*.jpg
Is there a way to have the script identify which image I'm right clicking on? I'm at a loss what to put in the file-*.jpg part which will be a variable for "whatever image I'm right-clicking on right now."
Tried searching for a good while on how to do this but am clearly either not using the right search terms or else this isn't done very often. Thank you in advance for any help!
if you want your script to run in file manager right-click menu you have to change your script and define file(s) as arguments. this happens simply by changing your file section with $1 to $n as the parameter(s).
as far as I know ubuntu uses nautilus as an file manager.
you can run nautilus-actions-config-tool either from your terminal or from dash and define your script a name and a command to run. you can follow this link for illustration learning :
ubuntu nautilus defile script in menu
for example :
#!/bin/bash
if [ "$1" != "" ]; then
echo "Positional parameter 1 contains value $1"
else
echo "Positional parameter 1 is empty"
fi
for all arguments :
#!/bin/bash
if [[ "$#" -gt 0 ]]; then
for arg in "$#"; do
echo $arg
done
fi
here is the image that shows the script worked
I know the question is a little older, but I can provide you with the solution.
You have to set up FileManager-actions, an extension for GNOME/Nautilus (but it also works for other file managers).
Setup filemanager-actions on Ubuntu 20.04
sudo apt update
sudo apt install filemanager-actions
Then run fma-config-tool and create a new action.
When creating an action, please ensure that:
[v] Display item in selection context menu
is flagged; otherwise, you will not see the context menu during the file selection.
Prepare the script you want to execute
Prepare a script that does what you need. Touch in /tmp, mv it in /usr/bin and give it execute permissions:
touch /tmp/your-script
# edit it with your editor
sudo mv /tmp/your-script /usr/bin/
sudo chmod +x /usr/bin/your-script
In your script, you can reference the filename using $1.
FILENAME=$1
echo $FILENAME
In the variable FILENAME you will find the selected file name.
Configure Nautilus-action command
To let nautilus pass the filename, insert the script path and the argument string in the' command' tab.
To fully answer your question, to let Nautilus pass the filename as a script argument, you have to specify %f.
At this point, quit the Nautilus instance and open it again:
nautilus -q
nautilus
Now, let's have a try! Right-click on a file and check out the result!
Appendix 1 - Filemanager-actions formats
%f A single file name, even if multiple files are selected.
%F A list of files. Each file is passed as a separate argument to the executable program.
%u A single URL. Local files may either be passed as file: URLs or as file path.
%U A list of URLs. Each URL is passed as a separate argument to the executable program.
%d Base directory
Here you can find a comprehensive list.
Appendix 2 - Sources
Check out my post blog in which I actually realize something similar: https://gabrieleserra.ml/blog/2021-08-14-filemanager-actions-ubuntu-20-04.html
Reference to all possible formats for FileManager-actions: https://askubuntu.com/a/783313/940068
Realize it in Ubuntu 18.04: https://askubuntu.com/a/77285/940068

What's wrong with this youtube-dl automatic script?

first of all, I'm quite new with bash scripting and I'm just starting to learn, evidently there's something wrong with this script, but I don't know what it is...
I created a bash script to automate downloading videos with youtube-dl:
#!/bin/bash
echo url:
read url
export url
youtube-dl -f 'bestvideo[height<=360]+worstaudio/worst[height<=360]' $url
The idea is that I type in the command line the name of the script, e.g.: "360" and it will ask for a url (e.g.: a Youtube video), I paste it and youtube-dl downloads it with the stated parameters. It works like a charm...
Now, I want to make the script more complex and I think I need to convert the youtube-dl command to a variable (of course, being a newbie, I might be wrong, but let's assume I'm right for a moment...)
#!/bin/bash
video="youtube-dl -f 'bestvideo[height<=360]+worstaudio/worst[height<=360]'"
echo url:
read url
export url
$video $url
When I try this, it throws me an error: "ERROR: requested format not available
"
I don't know what's wrong... I'd like to solve the problem with the least changes to the code as possible and I repeat, I'd like to know what's wrong with the current code so I can learn from it.
Thank you very much in advance!
It's explained in detail here here: I'm trying to put a command in a variable, but the complex cases always fail!
First always double-quote your variables, unless you know exactly what will happen if you don't.
You don't need to export that variable: you're not invoking any other program that needs to use it.
When you want to re-use a command, think about putting it in a function:
#!/bin/bash
function video {
youtube-dl -f 'bestvideo[height<=360]+worstaudio/worst[height<=360]' "$1"
}
read -p "url: " url
video "$url"
Actually, I would do this:
add that function to your ~/.bashrc,
source that file: source ~/.bashrc
then you can use it from the command line:
video 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
Remove the single quote from the -f parameter it will work.
For eg.
video="youtube-dl -f bestvideo[height<=360]+worstaudio/worst[height<=360]"

Bash shells script 101 - variable definition, pbcopy, etc

I am trying to create a (my first) bash script, but I need a little help. I have the following:
#!/bin/bash
echo "Write a LaTeX equation:"
read -e TeXFormula
URIEncoded = node -p "encodeURIComponent('$(sed "s/'/\\\'/g" <<<"$TeXFormula")')"
curl http://latex.codecogs.com/gif.latex?$URIEncoded -o /Users/casparjespersen/Desktop/notetex.gif | pbcopy
I want it to:
Require user input (LaTeX equation)
URIEncode user input (the top result in Google was using node.js, but anything will do..)
Perform a cURL call to this website converting equation to a GIF image
Copy the image to the placeholder, so I can paste it to a note taking app like OneNote, Word, etc.
My script is malfunctioning in the following:
URIEncoded is undefined, so there is something wrong with my variable definition.
When I copy using pbcopy the encrypted text content of the image is copied, and not the actual image. Is there a workaround for this? Otherwise, the script could automatically open the image and I could manually Cmd + C the content.
URIEncoded is undefined, so there is something wrong with my variable
definition.
The line should read
URIEncoded=$(node -p "encodeURIComponent('$(sed "s/'/\\\'/g" <<<"$TeXFormula")')")
without spaces around the = sign, and using the $() construct to actually perform the command, otherwise, the text of the command would be assigned to the variable.
When I copy using pbcopy the encrypted text content of the
image is copied, and not the actual image. Is there a workaround for
this? Otherwise, the script could automatically open the image and I
could manually Cmd + C the content.
pbcopy takes input from stdin but you are telling curl to write the output to a file rather than stdout. Try simply
curl http://latex.codecogs.com/gif.latex?$URIEncoded | pbcopy
or, for the second option you describe
curl http://latex.codecogs.com/gif.latex?$URIEncoded -o /Users/casparjespersen/Desktop/notetex.gif && open $_

unix tee command not working as expected

While i'm using the following command in unix command prompt everything is working fine,log fiel is creating fine.
ls -l|echo "[`date +%F-%H-%M-%S`] SUCCESS - FILES"|tee -a logger2.log
but using the same thing in side the shell script it is showing error
No such file or directory.
I'm not getting what is the problem here!!
If I read between the lines: you want a list of files, followed by a date and a message?
try:
{ ls -l ; echo "[$(date "+%F-%H-%M-%S")] SUCCESS - FILES" ; } |tee -a logger2.log
That should give you in 'logger2.log' of the current directory the lines
.................... file
.................... file2
(ie the list of all files and dirs in the current dir, EXCEPT those starting with a ".")
[2013-12-26-..-..-..] SUCCESS - FILES
Please note that, if you put nothing in front of the script, it could be started by a different shell than the one you use when testing. It depends what/who does invoke the script : if it is a crontab, it will probably be interpreted by "sh" (or by "bash" in "sh-compatibility" mode)...
Please tell us what the above gives you as output, and how you start the script (by a user, on the prompt? or via a crontab [which one: /etc/crontab ? or a user's crontab? which user?], etc. And what error messages you see.

How to call a variable between double qoutes in bash?

I'm trying to execute this command in a script:
axel "$1"
Where "$1" is a URL sent to this command in a script by the firefox plugin FlashGot. However,the URL is long and it keeps cutting it off short. The only way to overcome this is to enclose the URL in single or double quotes...eg. "http://...."
Thanks, in advance.
EDIT:
Ok, so an example of the URL is http://audio-sjl-t1-2.pandora.com/access/Letting%20Go%20-%20Isaac%20Shepard%2Emp4a?version=4&lid=290476474&token=Z6TTYtio6FYbhzesbxzPyWA%2F%2Bfa2uT5atbV8L0QF%2FMubHshmLJ1hgkN6B8SMZe74V8Q1feGMNmkmyTJO343qYkQ3aklQVKo4mDE2VVl1nkYk05gu0%2BBfP3WtxTCrn8r0gz0wwDgMfzQd68fBcmOTKtB%2FjR2kqVs9ZY7tZQUuabjGcP84ws%2BuIsuTqkKkHyrWaaLkGhk71GoPng2IMrm0L%2B6MeyHu6bvWn%2FoqNhXNerpFLpRZqXZ8JrX9uKVkDmkeQxUVV5%2F8y8uv2yYpG3P5tx1mfAY6U7ZteDLCfCT4JQWzlZscpl7GmtW4gf64KBExGA98xucIp%2Bt1x%2Bjru2Jt%2F7PVeeKWGv2en0%2Fetf1CQWjVUbDoWy4q9cEnYOc7rkpX
Well, it keeps cutting it off at
http://audio-sjl-t1-2.pandora.com/access/Letting%20Go%20-%20Isaac%20Shepard%2Emp4a?version=4
and that is all is getting sent to axel.
I added an echo command in the script:
#!/bin/bash
cd /home/caleb/Desktop
echo "$1"
axel "$1"
I can see the debug of a script by sending a URL to it through the terminal:
./axel.sh <URL>
The only error message I see is because of the shortened URL.
Here's the output of the script above:
http://audio-sjl-t1-2.pandora.com/access/Letting%20Go%20-%20Isaac%20Shepard%2Emp4a?version=4
Initializing download: http://audio-sjl-t1-2.pandora.com/access/Letting%20Go%20-%20Isaac%20Shepard%2Emp4a?version=4
HTTP/1.1 400 Bad Request
axel "$1" should work and I'm not surprised axel ""$1"" doesn't work because that's equivalent to axel $1.
To debug this we'll need an error message or something, because saying "it doesn't work" doesn't help at all.
You say the script is called from Firefox. I'm not sure if you can easily see the error message, maybe you can't. I have an idea for that. Let's call your script script.sh. Create a wrapper script script-wrapper.sh like this:
#!/bin/bash
log=/tmp/script.log
for arg; do
echo arg="'$arg'" | tee $log
done
/path/to/script.sh >>$log 2>&1
Make this script executable, trigger it from Firefox, and then look at the log, which will include both the output and error output of your original script. If you still can't figure out what is wrong, then edit your question, and paste in the content of /tmp/script.log so we can debug.
UPDATE
Based on your update, it looks like the script does not receive the URL correctly. In particular, it looks like the URL is not quoted properly when you pass it to the script. It's not surprising that the cut-off happens right in front of a & character, as that means something to the shell. You should call your script like this:
./axel.sh "http://....?version=4&lid=..."
But this is not happening, it looks it's getting called without the double-quotes, which will result in the behavior you're observing.
Just using
#!/bin/sh
axel "$1"
will work. If it isn't, you're going to need to give a lot more information...

Resources