Extracting Text from Google Translate

NOTE 1: It is assumed that you first copied a paragraph of text into your clipboard. Here I use the translation page from English to Russian (https://translate.google.gr/?hl=el#view=home&op=translate&sl=en&tl=ru&text=) You can use another.

NOTE 2: Automatic translation programs are powerless with translation, for example, technical literature and PDFs, so the process of professional translation looks like this: 1) Copy a paragraph, 2) Paste in Google Translator, 3) Copy the resulting translation, 4) Correct the translation in a text editor, 5) Paste into the original document. Total, we have 5 steps.

NOTE 3: You can automate all steps except the 4th, of course. There are many translation programs, but they are expensive and will never give you the flexibility your own Apple-script’s gives. And, despite their high cost, many of them violate the structure of the PDF and do not make a difference in what needs to be translated and what is not necessary. For example, this topic paid “professional” program will translate along with a piece of applescript, all fucking up

set myParagragh to the clipboard as string
set Translated_Text to ""

tell application "Safari"
	open location "https://translate.google.gr/?hl=el#view=home&op=translate&sl=en&tl=ru&text=" & myParagragh
	delay 5.0
	tell document 1
		repeat until the length of Translated_Text is not 0
			set TextTeg to do JavaScript "document.getElementsByTagName('span')[65].innerHTML"
			delay 0.5
			set Translated_Text to TextTeg as text
		end repeat
	end tell
end tell

set Translated_Text to findAndReplaceInText(Translated_Text, "<span title=\"\">", "")
set Translated_Text to findAndReplaceInText(Translated_Text, "</span>", "")
set Translated_Text to findAndReplaceInText(Translated_Text, "</span> <span title=\"\">", "")

on findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AppleScript's text item delimiters to theSearchString
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to theReplacementString
	set theText to theTextItems as string
	set AppleScript's text item delimiters to ""
	return theText
end findAndReplaceInText

This script only gives you the 2nd and 3rd steps of the professional translation process, but these steps is also the most necessary to create a full-fledged utility. In the future, I will publish a utility that will speed up the process of professional translation. For example, pdf

[del]open location is a command from Scripting Additions, and not Safari, so doesn’t make much sense in the way it’s used here. If you want to script Safari to open a new web page at a specific URL, Safari provides the make command, with which you can create a new tab and set its URL property appropriately:[/del]

tell application "Safari" to tell window 1 to make new tab with properties {URL:"http://google.com"}

I would suggest bypassing Safari altogether, and making use of Google Translate’s API, which will allow you to send it stuff in one language, and have it return it back in another, either by way of the cURL shell command, or AppleScriptObjC’s NSURLSession class methods.

It’s not that simple. The “open location” command sends the “GURL” Apple event, which is in some ways a bit like the open Apple event – applications respond to it even if they don’t list it in their dictionary. It’s a legitimate way to open a URL in a particular application that might not be the default for that URL (or file) type, and the “GURL” event was the standard method used by Launch Services for many years.

Oh yeah. For example, this topic Google Translate’s API will translate along with a piece of applescript.

Thanks for this, I wasn’t aware. I’ve seen it used a lot, of course, but learned from others that it was unreliable (by itself, a flimsy argument, but, as you said, its absence from a dictionary seemed to support the belief). However, you’re the first to provide a technical basis that clearly shows I’ve been operating from a myth for a long time. Good to have that rectified.