![]() Registered Member ![]()
|
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"
I gave it an Alias "ocr" so it can be executed as a command I added
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
Here is the file:
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. |
![]() Registered Member ![]()
|
Try to use the absolute path to binary ocrmypdf. You could also log actions of your script:
will create two files in /tmp, one containing errors (stderr output) and one containing the default output (stdout). |
![]() Registered Member ![]()
|
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? |
![]() Registered Member ![]()
|
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
and write the full path instead of ocrmypdf in your script. |
![]() Registered Member ![]()
|
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.
|
![]() Registered Member ![]()
|
OK! This is the path:
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:
|
![]() Registered Member ![]()
|
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):
Rebooted, still nothing showing! ![]() |
![]() Registered Member ![]()
|
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.
|
![]() Registered Member ![]()
|
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
and
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.
|
![]() Registered Member ![]()
|
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.
|
![]() Registered Member ![]()
|
I tried
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. |
![]() Registered Member ![]()
|
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.
|
![]() Registered Member ![]()
|
I tried
only. Still not working! This seems really strange that nothing is working though trying many things! |
![]() Registered Member ![]()
|
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.
|
![]() Registered Member ![]()
|
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.
|
Registered users: Bing [Bot], Google [Bot], Sogou [Bot], Yahoo [Bot]