Translate PDF using "Infix PDF Editor" and "Google Translate"

This script is a convenient plugin-like app for automated text translation in the “Infix PDF Editor”. In this form, it translates from English to Russian, the server localization is Greece. To use other localizations and languages, in the set myURL to sentence, replace the words “gr”, “en” and “ru” with the ones you need and use this plugin to improve your professional PDF translations.


set myPDF to choose file of type "pdf"
tell application "Infix PDF Editor" to activate
tell application "Infix PDF Editor" to open myPDF
display notification "Copy some PDF texts to trigger translations" with title "PDF Google Translate App" sound name "Frog"

repeat
	set sourceText to getSourceText()
	set googleTranslation to pasteSourceText_to_GoogleTranslate(sourceText)
	set googleTranslation to removeTegs_from_TranslatedText(googleTranslation)
	
	tell application "Infix PDF Editor" to activate
	set editedTranslation to googleTranslation
	set BUTTON_Returned to " Restore Original Google Translation "
	
	repeat while (BUTTON_Returned = " Restore Original Google Translation ")
		tell me to activate
		set myDialog to display dialog "Edit and Save Google Translation" default answer "" & editedTranslation buttons {" Save Edited Translation to Source Document ", " Restore Original Google Translation ", {" Cansel Current Translation "}} ¬
			default button 1
		set editedTranslation to text returned of myDialog
		set BUTTON_Returned to button returned of myDialog
		set the clipboard to editedTranslation
		
		if BUTTON_Returned is " Restore Original Google Translation " then
			set editedTranslation to googleTranslation
		else
			activate_EditedPage()
			delay 0.5
			if BUTTON_Returned is " Save Edited Translation to Source Document " then paste_Translation()
		end if
	end repeat
	
	delay 0.5
	quit_Translation()
	if result = "Quit all" then exit repeat
end repeat

on quit_Translation()
	tell me to activate
	display dialog "Continue translation?" buttons {"Continue", "Quit all"} default button "Continue"
	set BUTTON_Returned to button returned of result
	my activate_EditedPage()
	if BUTTON_Returned is "Quit all" then
		tell application "Infix PDF Editor" to quit
		tell application "CrossOver" to quit
	end if
	return BUTTON_Returned
end quit_Translation

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

on getSourceText()
	do shell script "pbcopy < /dev/null"
	set sourceText to ""
	repeat while (sourceText = "")
		set sourceText to the clipboard as string
		delay 1.0
	end repeat
	return sourceText
end getSourceText

on pasteSourceText_to_GoogleTranslate(sourceText)
	tell application "Safari"
		activate
		set myURL to "https://translate.google.gr/?
       hl=el#view=home&op=translate&sl=en&tl=ru&text=" & sourceText
		open location myURL
		set googleTranslation to ""
		repeat until (window 1 exists)
			delay 0.1
		end repeat
		tell front document
			repeat until the length of googleTranslation is not 0
				try
					set TextTeg to do JavaScript "document.getElementsByTagName('span')[67].innerHTML"
					set googleTranslation to TextTeg as text
					delay 0.1
				end try
			end repeat
		end tell
	end tell
	return googleTranslation
end pasteSourceText_to_GoogleTranslate

on removeTegs_from_TranslatedText(googleTranslation)
	set googleTranslation to my findAndReplaceInText(googleTranslation, "<span title=\"\" class=\"\">", "")
	set googleTranslation to my findAndReplaceInText(googleTranslation, "<span title=\"\">", "")
	set googleTranslation to my findAndReplaceInText(googleTranslation, "</span>", "")
	tell application "System Events" to click menu item 9 of menu 1 of menu bar item 3 of menu bar 1 of application process "Safari"
	return googleTranslation
end removeTegs_from_TranslatedText

on paste_Translation()
	tell application "System Events" to tell application process "Infix PDF Editor"
		tell window 1 to keystroke "v" using {control down, shift down}
	end tell
end paste_Translation

on activate_EditedPage()
	tell application "Infix PDF Editor" to activate
	tell application "System Events" to tell application process "Infix PDF Editor"
		repeat until (window 1 exists)
			delay 0.1
		end repeat
	end tell
end activate_EditedPage

NOTE: I updated the script as Google Translate changed its HTML.

For the future, if HTML changes again, you can do it yourself. 1) Translate some text into Google Translate 2) Run the following script to find the sequence number of the needed tag (I entered “Before access”, result in Google Translate is Russian text “Перед доступом”, then the following script uses this “Перед доступом” to find the number of the needed tag for me). I made the tag search process visual, for clarity:


tell application "Safari"
	activate
	set myParagragh to the "Before access"
	open location "https://translate.google.gr/?hl=el#view=home&op=translate&sl=en&tl=ru&text=" & myParagragh
	
	tell document 1
		set n to 0
		set Translated_Text to ""
		
		repeat until Translated_Text contains "Перед доступом"
			set n to n + 1
			set TextTeg to do JavaScript ("document.getElementsByTagName('span')[" & (n as string) & "].innerHTML")
			set Translated_Text to TextTeg as text
			set nn to n as string
			set nn to "n=" & nn & "        " & "Text= " & Translated_Text
			display dialog nn giving up after 3
		end repeat
		set nn to n as string
		display dialog "n=" & nn
		
	end tell
end tell

So I found that sequence number = 67.

What needs to be cleaned in the tag (using AppleScript Text Delimeters), this script shows:


tell application "Safari"
	tell front document
		set TextTeg to do JavaScript "document.getElementsByTagName('span')[67].innerHTML"
	end tell
end tell
--Result---> "<span title=\"\" class=\"\" contenteditable=\"false\" tabindex=\"-1\">Перед доступом</span>"