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?
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.
AppleScript Code:
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, nobrowser 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 usingdo 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 ofdo 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.)