help performing web search of clipboard from shell/apple script

hello i ve put toghether a shell script to perform ocr from images ,copying the text to the clipboard and finally i d like to perform a google search of the recognized text(which is also copied to the clipboard). here s the code :

osascript -e beep
screencapture -i /Users/Al3/Desktop/ocr.png
/usr/local/bin/tesseract /Users/Al3/Desktop/ocr.png /Users/Al3/Desktop/ocr
cat /Users/Al3/Desktop/ocr.txt | pbcopy
searchTerm="$(pbcopy)"
open "https://www.google.com/search?q=$searchTerm"
rm -rf /Users/Al3/Desktop/ocr.txt
rm -rf /Users/Al3/Desktop/ocr.png

but it s not perfoming a google search as i d like
can someone help? i even tried combinig it with apple script without success:

set url1 to "https://www.google.com/search?client=firefox-b-d&q="

tell application "System Events"
	set myquery to the clipboard
end tell


tell application "Firefox Nightly"
	activate
	set theURL to url1 & myquery
	open location theURL
end tell

thank you very much for the help.
ps : the ocr part of the script works perfectly and recognized text gets copied to the clipboard without issue.

Try this:

tell application id "org.mozilla.nightly"
	activate
	open location theURL
end tell

FWIW, Firefox has limited applescript support. I find that it rarely responds to just its name.

There is another post on getting it to open urls here:

https://macscripter.net/viewtopic.php?pid=205950#p205950

Firefox does have a shell executable (inside its application package). It doesn’t come with a man page but you can access its help this way:

% /Applications/Firefox\ Nightly.app/Contents/MacOS/firefox-bin -h

You can open a URL with this command:

% /Applications/Firefox\ Nightly.app/Contents/MacOS/firefox-bin --new-window https://macscripter.net/

You have a lot of chaos here.

The osascript command does not execute shell commands. It is designed to execute scripts or AppleScript’s do shell scripts.

The beep command has nothing to do with MAC. Here is start point to build your workflow . Not tested, because I have not tesseract installed. Note, it is for executing in the terminal:


osascript <<EOF
do shell script "afplay /System/Library/Sounds/Ping.aiff"
do shell script "screencapture -i /Users/Al3/Desktop/ocr.png"
do shell script "/usr/local/bin/tesseract /Users/Al3/Desktop/ocr.png /Users/Al3/Desktop/ocr.txt"
set searchTerm to read ("/Users/Al3/Desktop/ocr.txt" as POSIX file)
tell application id "org.mozilla.firefox" to «event WWW!OURL» ("https://www.google.com/search?q=" & searchTerm)
do shell script "rm -rf /Users/Al3/Desktop/ocr.txt"
do shell script "rm -rf /Users/Al3/Desktop/ocr.png"
EOF

Since I don’t have tesseract, you have to debug the code yourself, step by step. This is best done in the Script Editor. To do this, temporarily throw out osascript and add each do shell script command one at a time. For example, to check that BEEP and Screenshot Capturing are working:


do shell script "afplay /System/Library/Sounds/Ping.aiff" --> debugged, works 
do shell script "screencapture -i /Users/Al3/Desktop/ocr.png" --> debugged, works 
-- add here next code line (next do shell script in your case)

The OP did mention that the OCR part works and the desired text does end up on the clipboard.

Assuming the OP has just typo, and /Users/Al3/Desktop/ocr.txt supposed to be the same file with /Users/Al3/Desktop/ocr, and assuming the tesseract writes correctly text to it, I updated my osascript in the post #4. I removed pbcopy as well. Now the script should work as expected. At least, without osascript (that is, as plain apple-script) it works.

I will repeat it here.

For running from the Terminal:


osascript <<EOF
do shell script "afplay /System/Library/Sounds/Ping.aiff"
do shell script "screencapture -i /Users/Al3/Desktop/ocr.png"
do shell script "/usr/local/bin/tesseract /Users/Al3/Desktop/ocr.png /Users/Al3/Desktop/ocr.txt"
set searchTerm to read ("/Users/Al3/Desktop/ocr.txt" as POSIX file)
set theURL to "https://www.google.com/search?q=" & searchTerm
tell application id "org.mozilla.firefox" to «event WWW!OURL» theURL
do shell script "rm -rf /Users/Al3/Desktop/ocr.txt"
do shell script "rm -rf /Users/Al3/Desktop/ocr.png"
EOF

For running from the script editor, or, as application:


do shell script "afplay /System/Library/Sounds/Ping.aiff"
do shell script "screencapture -i /Users/Al3/Desktop/ocr.png"
do shell script "/usr/local/bin/tesseract /Users/Al3/Desktop/ocr.png /Users/Al3/Desktop/ocr.txt"
set searchTerm to read ("/Users/Al3/Desktop/ocr.txt" as POSIX file)
set theURL to "https://www.google.com/search?q=" & searchTerm
tell application id "org.mozilla.firefox" to «event WWW!OURL» theURL
do shell script "rm -rf /Users/Al3/Desktop/ocr.txt"
do shell script "rm -rf /Users/Al3/Desktop/ocr.png"

thank you very much for the help, with your version i ve got this error: i cannot transform file “Macintosh HD:Users:Al3:Desktop:ocr.txt” into file type (i hope i traslated correctly from italian) also i ve used “Firefox Nighlty” instead of id “org.mozilla.firefox” because i got error ,maybe because i use firefox nighlty

This error occurs because your tesseract utility can’t create this text file on the desktop. That is, file doesn’t exist on the moment you try to read from it. The reasons may be several. Maybe, need some delay after tesseract code line. Other possible reason is security restictions. Grant to tesseract utility access to desktop folder. If you execute osascript from Terminal, then grant access to desktop folder to osascript and to Terminal.app as well. Or, better, grant to all of them full disk access, for the future.

So, you got error. But what version of 2 suggestions you ran? And is created ocr.txt on the Desktop or not?

thanks again for helping so i made a little mistake the correct syntax is

[do shell script "screencapture -i /Users/Al3/Desktop/ocr.png"
do shell script "/usr/local/bin/tesseract /Users/Al3/Desktop/ocr.png /Users/Al3/Desktop/ocr"
set searchTerm to (the POSIX path of (path to home folder) & "Desktop/ocr.txt")
set searchTerm1 to paragraphs of (read searchTerm)
set theURL to "https://www.google.com/search?q=" & paragraphs of (read searchTerm)
tell application "Firefox Nightly" to activate (open location theURL)
do shell script "rm -rf /Users/Al3/Desktop/ocr.txt"
do shell script "rm -rf /Users/Al3/Desktop/ocr.png"
/applescript] as u can see in original code i put "ocr.txt" and tesseract was making a ocr.txt.txt file on desktop
the cose i ve just posted workde perfeclty for safari but not for firefox unfortunately i dont really know why because tell appliccation firefox to pen location blabla has always worked fine,any idea?
[quote=KniazidisR]

[quote=ale82to]
thank you very much for the help, with your version i ve got this error: i cannot transform file "Macintosh HD:Users:Al3:Desktop:ocr.txt" into file type
[/quote]

This error occurs because your [b]tesseract[/b] utility can't create this text file on the desktop. That is, [b]file doesn't exist on the moment you try to read from it[/b]. The reasons may be several. Maybe, need some delay after tesseract code line. Other possible reason is security restictions. Grant to [b]tesseract[/b] utility [b]access to desktop folder[/b]. If you execute [b]osascript[/b] from Terminal, then grant [b]access to desktop folder[/b] to [b]osascript[/b] and to [b]Terminal.app[/b] as well. Or, better, grant to all of them [b]full disk access[/b], for the future.

So, you got error. But what version of 2 suggestions you ran? And is created [b]ocr.txt[/b] on the Desktop or not?
[/quote]

As for Firefox: if you have the latest version like mine, it’s not clear why you changed the tell application id “org.mozilla.firefox” to “event WWW! OURL” theURL to something I didn’t suggest. If, not then listen at least the @Mocman’s suggestion to not refer to Firefox by name but by id:

tell application id “org.mozilla.nightly”

As for paragraphs, you need to search Google for a specific paragraph (text), not a list of texts. For example,


set searchTerm1 to paragraph 1 of (read searchTerm)
set theURL to "https://www.google.com/search?q=" & searchTerm1

And, what kind of code is this?:

tell application “Firefox Nightly” to activate (open location theURL)???

First activate, then open. In 2 separate code lines. This way you try to activate the document. activate command isn’t designed to activate documents, but to activate applications.

so this should be the right code

osascript <<EOF

do shell script "screencapture -i /Users/Al3/Desktop/ocr.png"
do shell script "/usr/local/bin/tesseract /Users/Al3/Desktop/ocr.png /Users/Al3/Desktop/ocr"
set searchTerm to read ("/Users/Al3/Desktop/ocr.txt" as POSIX file)
set theURL to "https://www.google.com/search?q=" & searchTerm
tell application id "org.mozilla.nightly" to «event WWW!OURL» theURL
do shell script "rm -rf /Users/Al3/Desktop/ocr.txt"
do shell script "rm -rf /Users/Al3/Desktop/ocr.png"
EOF

but the web search is not working for firefox nighlty

damn firefox none seems to work i tried

osascript <<EOF
do shell script "screencapture -i /Users/Al3/Desktop/ocr.png"
do shell script "/usr/local/bin/tesseract /Users/Al3/Desktop/ocr.png /Users/Al3/Desktop/ocr"
set searchTerm to read ("/Users/Al3/Desktop/ocr.txt" as POSIX file)
set theURL to "https://www.google.com/search?q=" & searchTerm
tell application id "org.mozilla.nightly" to activate (open «event WWW!OURL» theURL)
do shell script "rm -rf /Users/Al3/Desktop/ocr.txt"
do shell script "rm -rf /Users/Al3/Desktop/ocr.png"
EOF

it brings firefox nighlty yo the front but there s no way it permorms the expect web search!!


do shell script "screencapture -i /Users/Al3/Desktop/ocr.png"
do shell script "/usr/local/bin/tesseract /Users/Al3/Desktop/ocr.png /Users/Al3/Desktop/ocr"
set searchTerm to read ("/Users/Al3/Desktop/ocr.txt" as POSIX file)
set theURL to "https://www.google.com/search?q=" & searchTerm
tell application id "org.mozilla.nightly" to activate -- 2 separate lines, and this event is open,
tell application id "org.mozilla.nightly" to «event WWW!OURL» theURL  -- so no need open (open).... 
do shell script "rm -rf /Users/Al3/Desktop/ocr.txt"
do shell script "rm -rf /Users/Al3/Desktop/ocr.png"

I have to go at work. I will install Firefox Nightly tomorrow, to play with it and to see what is problem with it.

thank you very very muchi think this with right syntax may work give it a try i just need how to pass theURL As a search term to damn firefox

osascript <<EOF
do shell script "screencapture -i /Users/Al3/Desktop/ocr.png"
do shell script "/usr/local/bin/tesseract /Users/Al3/Desktop/ocr.png /Users/Al3/Desktop/ocr"
set searchTerm to (the POSIX path of (path to home folder) & "Desktop/ocr.txt")
set searchTerm1 to paragraphs of (read searchTerm)
set theURL to paragraphs of (read searchTerm)
tell application "Firefox Nightly" to  activate
tell application "Firefox Nightly" to  open location "https://www.google.com/search?client=firefox-b-d&q=$theURL"
do shell script "rm -rf /Users/Al3/Desktop/ocr.txt"
do shell script "rm -rf /Users/Al3/Desktop/ocr.png"
EOF

this is working for safari and not firefox

osascript <<EOF
do shell script "screencapture -i /Users/Al3/Desktop/ocr.png"
do shell script "/usr/local/bin/tesseract /Users/Al3/Desktop/ocr.png /Users/Al3/Desktop/ocr"
set searchTerm to (the POSIX path of (path to home folder) & "Desktop/ocr.txt")
set searchTerm1 to paragraphs of (read searchTerm)
set theURL to "https://www.google.com/search?q=" & searchTerm1
tell application "Firefox Nightly" to open location theURL
tell application "Safari" to open location theURL
do shell script "rm -rf /Users/Al3/Desktop/ocr.txt"
do shell script "rm -rf /Users/Al3/Desktop/ocr.png"
EOF

Hi, again.

I installed FireFox Nithly to see what the problem is. It turned out that there is no problem if you work correctly with paragraphs of text.It is better to search for each paragraph separately. Also, you need to take into account that each paragraph can consist of several words. For stability, you need to add some delay after the search as well. The following worked for me without problems:


do shell script "screencapture -i /Users/Al3/Desktop/ocr.png"
do shell script "/usr/local/bin/tesseract /Users/Al3/Desktop/ocr.png /Users/Al3/Desktop/ocr"
set theParagraphs to paragraphs of (read ("/Users/Al3/Desktop/ocr.txt" as POSIX file))
tell application id "org.mozilla.nightly" to activate
repeat with aParagraph in theParagraphs
	set searchTerm to ""
	repeat with aWord in (words of aParagraph)
		set searchTerm to searchTerm & "+" & aWord
	end repeat
	set theURL to "https://www.google.com/search?q=" & searchTerm
	tell application id "org.mozilla.nightly" to open location theURL
	delay 1
end repeat
do shell script "rm -rf /Users/Al3/Desktop/ocr.txt"
do shell script "rm -rf /Users/Al3/Desktop/ocr.png"

Amaing thankyou very very very much! is it possible to open just one tab of firefox whit all search term instead a tab for each paragraph?

Yes, it is possible:


-- capture screenshot of selected area of the screen
do shell script "screencapture -i /Users/Al3/Desktop/ocr.png"
-- perform OCR recognition of the text 
do shell script "/usr/local/bin/tesseract /Users/Al3/Desktop/ocr.png /Users/Al3/Desktop/ocr"
-- read the text from TXT file, created by tesseract 
set searchTerm to read ("/Users/Al3/Desktop/ocr.txt" as POSIX file)

-- Remember AppleScript's text item delimiters to restore later
set TID to AppleScript's text item delimiters
-- Replace every SPACE with "+"
set AppleScript's text item delimiters to space
set searchTerm to text items of searchTerm
set AppleScript's text item delimiters to "+"
set searchTerm to searchTerm as text
-- Replace every LINEFEED with "++++"
set AppleScript's text item delimiters to linefeed
set searchTerm to text items of searchTerm
set AppleScript's text item delimiters to "++++"
set searchTerm to searchTerm as text
set AppleScript's text item delimiters to TID

-- perform Google search
tell application id "org.mozilla.nightly" to activate
set theURL to "https://www.google.com/search?q=" & searchTerm
tell application id "org.mozilla.nightly" to open location theURL

-- delete unneeded files
do shell script "rm -rf /Users/Al3/Desktop/ocr.txt"
do shell script "rm -rf /Users/Al3/Desktop/ocr.png"