This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Add Service Menu from .sh Script or Command

Tags: None
(comma "," separated)
limotux
Registered Member
Posts
12
Karma
0
I am on EndeavourOS, KDE Plasma.

I created a script to OCR PDF files (PDF files that are just a scan and no text).
Her is the file "ocr.sh"
Code: Select all
#!/bin/bash
echo $1
ocrmypdf -l ara+eng "$1" "$1"


I gave it an Alias "ocr" so it can be executed as a command
I added

Code: Select all
alias ocr="bash /home/limo/MyScripts/ocr.sh"

to “~/.bashrc”

What I want to do now is to right click on a PDF file, pop up a service menu with entry "OCR" that does the job instead of finding the folder where the file is, finding the file, open command line... issue command ...

I made a file "OCR.desktop" and put it in
Code: Select all
/usr/share/kservices5/ServiceMenus/

Here is the file:
Code: Select all
[Desktop Entry]
Name=OCR
ServiceTypes=KonqPopupMenu/Plugin
Type=Application
MimeType=Application/pdf;
Actions=ocr
Exec=Exec="/home/limo/MyScripts/ocr.sh" %U

Both ocr.sh and ocr as a command work fine.
Still, OCR sub menu does not show up when I right click a PDF file.
What might be wrong?
How can I do it?

Thank you very much for your help.
koffeinfriedhof
Registered Member
Posts
608
Karma
4
OS
Try to use the absolute path to binary ocrmypdf. You could also log actions of your script:
Code: Select all
#!/bin/bash
exec 2> /tmp/ocrlog.err > /tmp/ocrlog
printf "Argument 1:\n" "$1"
printf "Error goes here\n" >&2
/usr/bin/ls "$1"

will create two files in /tmp, one containing errors (stderr output) and one containing the default output (stdout).
limotux
Registered Member
Posts
12
Karma
0
koffeinfriedhof wrote:Try to use the absolute path to binary ocrmypdf. You could also log actions of your script:
Code: Select all
#!/bin/bash
exec 2> /tmp/ocrlog.err > /tmp/ocrlog
printf "Argument 1:\n" "$1"
printf "Error goes here\n" >&2
/usr/bin/ls "$1"

will create two files in /tmp, one containing errors (stderr output) and one containing the default output (stdout).

Sorry! I do not follow! I am not that techie.

To use the absolute path to ocrmypdf? It already installed, already made my script and already gave an Alias.
Please explain to me as if I am 5 years old.
Sorry.

Any modifications I should do to my .desktop file? Any files to put somewhere else?
koffeinfriedhof
Registered Member
Posts
608
Karma
4
OS
Every shell you open has some environment variables. One of them is a path variable (`echo $PATH`). This are the folders where the shell looks up commands that are not internal ones. So if you type `ls` in a shell, it loops through the folders and searches for an executable file called ls that it can run. Running scripts, the PATH is normally set to the default paths depending on the distribution. To avoid not finding the executable, provide the absolute path in your script. The script reads like this:
`#!/bin/bash` is the shebang providing information with which shell the following commands should be executed.
`exec 2>…` redirects the output to files. 2 is stderr where errors are displayed, 1 is the default stdout so you can read what has happened after execution. Call it minimal logging.

Get the path to your executable using
Code: Select all
which ocrmypdf
# or
type ocrmypdf

and write the full path instead of ocrmypdf in your script.
dzon
Registered Member
Posts
493
Karma
3
Here's some pdf service menu I made for myself:
[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=application/pdf;
Actions=extracthere;extract2folder;JPEG;PNG;Dockx
X-KDE-Submenu=PDF ****
Icon=applications-utilities

[Desktop Action JPEG]
Name=...to JPEG image
Name[it]=Immagine JPEG
Name[fr]=Image JPEG
Icon=application-pdf
Exec=filename=%f; convert -density 150 %f -quality 100 ${filename%.*}.jpg

[Desktop Action PNG]
Name=...to PNG image
Name[it]=Immagine PNG
Name[fr]=Image PNG
Icon=image-png
Exec=filename=%f; convert -density 150 %f -quality 100 ${filename%.*}.png

[Desktop Action Dockx]
Name=...to Dockx
Name[it]=Immagine PNG
Name[fr]=Image PNG
Icon=application-msword
Exec=libreoffice --invisible --infilter="writer_pdf_import" --convert-to docx:"MS Word 2007 XML" ${filename%.*}.dockx

[Desktop Action extracthere]
Exec=count=0; for f in %F; do count=$((count+1)); fdir=$(dirname "$f"); done; count=$((count*100)); progress=$(kdialog --title "Extracting images..." --progressbar "" $(($count+1))); count="0"; for f in %F; do qdbus $progress setLabelText "Reading from file $(basename "${f%.*}.pdf")" > /dev/null; count=$((count+50)); qdbus $progress Set "" value $count > /dev/null; if [ $? -gt 0 ]; then notify-send -i application-pdf -a "STOP!" -t 6000 "Operation cancelled as requested."; rm -r "$tmpDir"; exit 1; fi; message=$(pdfimages -all "$f" "${f%.*}"); if [ $? -gt 0 ]; then kdialog --title "ERROR" --error "pdfimages: $message"; fi; count=$((count+50)); qdbus $progress Set "" value $count > /dev/null; if [ $? -gt 0 ]; then notify-send -i application-pdf -a "STOP!" -t 6000 "Operation cancelled as requested."; rm -r "$tmpDir"; exit 1; fi; done; sleep 0,1; qdbus $progress close > /dev/null; notify-send -i application-pdf -a "DONE!" -t 6000 "Extraction complete."; exit 0;
Icon=application-pdf
Name=Extract images here
Name[fr]=Extraire les images ici
Name[it]=Estrai le immagini qui

[Desktop Action extract2folder]
Exec=count=0; for f in %F; do count=$((count+1)); fdir=$(dirname "$f"); done; outdir=$(kdialog --getexistingdirectory "$fdir") && cd "$outdir" || exit 1; count=$((count*100)); progress=$(kdialog --title "Extracting images..." --progressbar "" $(($count+1))); count="0"; for f in %F; do qdbus $progress setLabelText "Reading from file $(basename "${f%.*}.pdf")" > /dev/null; count=$((count+50)); qdbus $progress Set "" value $count > /dev/null; if [ $? -gt 0 ]; then notify-send -i application-pdf -a "STOP!" -t 6000 "Operation cancelled as requested."; rm -r "$tmpDir"; exit 1; fi; message=$(pdfimages -all "$f" "$(basename "${f%.*}")" 2>&1); if [ $? -gt 0 ]; then kdialog --title "ERROR" --error "pdfimages: $message"; fi; count=$((count+50)); qdbus $progress Set "" value $count > /dev/null; if [ $? -gt 0 ]; then notify-send -i application-pdf -a "STOP!" -t 6000 "Operation cancelled as requested."; rm -r "$tmpDir"; exit 1; fi; done; sleep 0,1; qdbus $progress close > /dev/null; notify-send -i application-pdf -a "DONE!" -t 6000 "Extraction complete."; exit 0;
Icon=application-pdf
Name=Extract images to...
Name[fr]=Extraire les images dans...
Name[it]=Estrai le immagini in...


As far as I can tell, your exec is double in your desktop file. Also, you dn't really need a separate script for that. Entering that command straight into the desktop file should do just fine. If you follow such a structure ( and the right exec command of course) it'll look something like this: https://imgur.com/a/0HBPRiR


This realm's name is Maya. And she speaks Hertz. But Ahamkara makes a fuzz about it.
limotux
Registered Member
Posts
12
Karma
0
OK! This is the path:
Code: Select all
/home/limo/.local/bin/ocrmypdf

What should I do next?

But I want to run my script that copies the text to the same file. (in my script source $1 and target $1 as well)
The path to my script is:
Code: Select all
"/home/limo/MyScripts/ocr.sh"
limotux
Registered Member
Posts
12
Karma
0
dzon wrote:
As far as I can tell, your exec is double in your desktop file. Also, you dn't really need a separate script for that. Entering that command straight into the desktop file should do just fine. If you follow such a structure ( and the right exec command of course) it'll look something like this: https://imgur.com/a/0HBPRiR


Thanks @dzon
I noticed the error and corrected it. Thanks a lot. I was just tired trying to do it for 2 days!
Now here is the file (I took some clues from @dzon):
Code: Select all
[Desktop Entry]
Name=OCR
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=Application/pdf;
Actions=ocr
X-KDE-Submenu=PDF
Exec="/home/limo/MyScripts/ocr.sh" %U



Rebooted, still nothing showing! >:(
dzon
Registered Member
Posts
493
Karma
3
You'll need this if you want a single entry. Adjust the command (Exec) to your liking cause I can't check for myself. https://imgur.com/a/eyeplcb

[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=application/pdf;
Actions=Ocr
Icon=applications-pdf

[Desktop Action Ocr]
Name=Ocr
Icon=application-pdf
Exec=ocrmypdf -l ara+eng %F

Last edited by dzon on Sun Jul 24, 2022 2:36 pm, edited 1 time in total.


This realm's name is Maya. And she speaks Hertz. But Ahamkara makes a fuzz about it.
limotux
Registered Member
Posts
12
Karma
0
Thanks @dzon... you made my day (almost) ^-^
I copy/paste the code you gave me, right clicking I could see a sub menu item "Ocr"! ^-^ ^-^ ^-^
But clicking on it gave me an error message like "could not find program"
So I tried and
Code: Select all
Exec="/home/limo/MyScripts/ocr.sh"

and
Code: Select all
Exec="/home/limo/MyScripts/ocr.sh" %F


I right click... etc... no error message but nothing happens to the file. No text layer added! :'(

Last edited by limotux on Sun Jul 24, 2022 2:45 pm, edited 1 time in total.
dzon
Registered Member
Posts
493
Karma
3
You could try Exec= bash /home/limo/MyScripts/ocr.sh

Last edited by dzon on Sun Jul 24, 2022 2:51 pm, edited 1 time in total.


This realm's name is Maya. And she speaks Hertz. But Ahamkara makes a fuzz about it.
limotux
Registered Member
Posts
12
Karma
0
I tried
Code: Select all
Exec= bash /home/limo/MyScripts/ocr.sh

No error message, file still the same!
UPDATE:
I checked the link about "Tesseract"
ocrmypdf uses "Tesseract"

I tried ocrmypdf, and my .sh cript and the Alias I created and all worked perfectly on their own. (same file I am trying now)
EDIT:
The link says "It OCR's a document and puts it into a file that has the same name as the OCRed image file but with a txt extension".
What I want is to add text as layer over the same PDF file (which was actually done as explained above)
I don't know why it is not working in .desktop.
dzon
Registered Member
Posts
493
Karma
3
You need %F at the end of that. Can't check that stuff, you'll need to fiddle a bit I'm afraid. Anywho, I was thinking of doing it with a submenu style. That way you can add the execs separately. Like so:https://imgur.com/a/4HeVw83
Again, you'll need to set the correct command but in order to create a new pdf in the same directory you need something like:

[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=application/pdf;
Actions=Arabic;English
X-KDE-Submenu=Ocrmy
Icon=text-x-changelog

[Desktop Action Arabic]
Name=...to Arabic
Icon=text-x-changelog
Exec=filename=%f; ocrmypdf -l ara ${filename%.*}.pdf

[Desktop Action English]
Name=...to English
Icon=text-x-changelog
Exec=filename=%f; ocrmypdf -l eng ${filename%.*}.pdf


This realm's name is Maya. And she speaks Hertz. But Ahamkara makes a fuzz about it.
limotux
Registered Member
Posts
12
Karma
0
I tried
Code: Select all
Exec=filename=%f; ocrmypdf -l ara ${filename%.*}.pdf

only.

Still not working!
This seems really strange that nothing is working though trying many things!
dzon
Registered Member
Posts
493
Karma
3
Odd. Try a simple Exec=ocrmypdf -l ara %F in the desktop file.


This realm's name is Maya. And she speaks Hertz. But Ahamkara makes a fuzz about it.
dzon
Registered Member
Posts
493
Karma
3
Maybe this one: Exec=bash -c 'f="%u"; ocrmypdf "$f" "${f%.pdf}_ocr.pdf" ;


This realm's name is Maya. And she speaks Hertz. But Ahamkara makes a fuzz about it.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot], Yahoo [Bot]