tell application “System Events” to keystroke “c” using {command down}
delay 1 – Without this, the clipboard may have stale data.
tell application “Firefox”
open location “https://translate.google.ca/?hl=en&tab=wT”
activate
end tell
tell application “System Events” to keystroke “v” using {command down}
delay 1
end tell
This script copies the selected text, goes to the website (Google Translate), but it will not do a paste.
Model: mid2012 Macbook Pro
AppleScript: came with Mojave
Browser: Firefox 84.0
Operating System: macOS 10.14
Hi,
Per Standard Additions (which are scripting additions) dictionary set the clipboard and the clipboard commands are enclosed in app’s tell blocks, the system clipboard is set after activating the app. Try my code below. Notes are included.
tell application "Firefox"
activate
-- set the clipboard to "How's your mood today?" -- a test key-phrase. The acquisition of text-to-translate can be done setting the clipboard programmatically or traditionally with the system hot keys "⌘-C". The html query string contains properties values of which you can set to any source and target language: "sl" stands for "source language", "tl" for "translation language". The codes for all languages are standard: "en" - English, "de" - German, "it" - Italian, "fr" - French. For the full list of ISO 639-1 compliant language codes look at "https://www.loc.gov/standards/iso639-2/php/code_list.php".
set QueryString to the clipboard
delay 1
open location "https://translate.google.ca/?hl=en&tab=wT&sl=auto&tl=it"
if (the clipboard) contains QueryString then
tell application "System Events"
set frontmost of application process "Firefox" to true
repeat until the name of window 1 contains "translate"
delay 0.1
end repeat
keystroke "v" using {command down}
end tell
else
display alert "Nothing to paste" message "Copy text and try again." as informational giving up after 3
end if
end tell