I am using KUbuntu 18.04.1
I am trying to extract text from screenshot using spectacle (Screenshot app of KUbuntu) & gocr (OCR tool)
If I give the commands one after another in Konsole (Terminal of KUbuntu)
First:
spectacle -r -b -n -o /home/UserName/Documents/Translate/input.jpg
Second:
gocr -i /home/UserName/Documents/Translate/input.jpg | xsel -b
It Works.
if I create a bash file screen_ts.sh and put the code
#!/bin/bash
# Dependencies: gocr xsel
# Take Screenshot and Save it to a File
spectacle -r -b -n -o /home/UserName/Documents/Translate/input.jpg
# Extract text from the saved file
gocr -i /home/UserName/Documents/Translate/input.jpg | xsel -b
exit
Open Konsole and type ./screen_ts.sh , it works
But, if I Make the file executable and double click, it take screenshot but does not save the text in clipboard.
Again if I create a custom shortcut using
Custom Shortcut > Trigger Alt + Q
& Action /home/UserName/Documents/Translate/screen_ts.sh
it take screenshot but does not save the text in clipboard. Same behavior as double click.
What is going on here?
From man xsel:
By default, this program outputs the selection without modification if both standard input and standard output are terminals (ttys). Otherwise, the current selection is output if standard output is not a terminal (tty), and the selection is set from standard input if standard input is not a terminal (tty). If any input or output options are given then the program behaves only in the requested mode.
This is a bit misleading, but there is an interesting comment in the source code:
Checking stdin/stdout for being a tty is NOT reliable to tell what the user wants. This is because child processes inherit the file descriptors of their parents; an xsel called in a script that is e.g. daemonized (not attached to a tty), or called with a redirection or in a pipeline will have non-tty file descriptors on default. The redirection/piping issue also applies to "grouped" or "compound" commands in the shell (functions, subshells, curly-brace blocks, conditionals, loops, etc.). In all these cases, the user must set the mode of operation explicitly.
...and that's the solution: tell xsel
explicitly to read from stdin
by adding the -i
option:
gocr -i /home/UserName/Documents/Translate/input.jpg | xsel -b -i
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments