Copy Selected Text, Paste on Google Translate App (Fluid App) and Translate It with Automater/Service

applescriptautomatorfluid.app

I have a Google Translate app created with Fluid App. I want to translate any selected text with this my Google Translate Fluid App.

So, for this purpose how do I create a service with Automator/AppleScript?

Best Answer

Here is the AppleScript code I use in a Run AppleScript action, in an Automator Service workflow in conjunction with my Google Translate Fluid App, which is named Google Translate.

Create an Automator Service workflow with the settings as shown in the image below, while replacing the default AppleScript code with the AppleScript code found below the Automator image. Note that I saved it as Translate with Google Translate and this is what shows on the Services menu and context menu when I have selected text that I want to translate.

Automator Service workflow


AppleScript Code:

on run {input, parameters}

    set textToTranslate to item 1 of input as string
    set windowName to "Google Translate"
    set targetURL to "https://translate.google.com/"

    tell current application
        do shell script "open -a " & quoted form of windowName
    end tell

    tell application "Google Translate"
        repeat while (loading) of front browser window
            delay 0.5
        end repeat
        if (get title of front window) does not contain windowName then
            open location targetURL
            repeat while (loading) of front browser window
                delay 0.5
            end repeat
        end if
        tell front browser window
            activate
            try
                do JavaScript "document.getElementById('source').value = \"" & textToTranslate & "\";"
            on error
                set the clipboard to textToTranslate as string
                delay 1
                activate
                do JavaScript "document.getElementById('source').value = ''; document.getElementById('source').focus();"
                tell application "System Events"
                    keystroke "v" using command down
                end tell
            end try
        end tell
    end tell

end run

Note: My Translate with Google Translate Automator Service tries to use JavaScript to change the value of the source text area, as this is IMO a better method then literally programmatically pasting the text to translate to the source text area, however if it can't, it then copies the selected text to the clipboard and then pastes it to the source text area. Before pasting, via programmatically pressing ⌘V, JavaScript is used to both clear the source text area and set focus to it, so the paste will occur as intended in the right location.

The use of do shell script "open -a " ... is necessary because a Fluid App with default Preferences, one created with Fluid, by default when opened from the macOS UI shows its Home Page. However, when programmatically opened via AppleScript, example case, tell application "Google Translate" to activate, only the Dock Tile appears, no browser window, i.e. no Home Page is displayed. This is in part why I originally coded it the way I did. However, since your comment, I found out that if I programmatically opened via AppleScript using do shell script "open -a " ..., it opens with a browser window, not just the Dock Tile and as such was able to recode the Service differently because the use of do shell script "open -a " ... also would bring a minimize window back up without the need to account for that with additional coding.

Hopefully the new code will eliminate two Google Translate tabs from appearing when starting from a closed state. (Although depending on timings, it may occasionally happen.)