I created an AppleScript for a sequence of copy-paste keystrokes and delays:
Example AppleScript code:
tell application "System Events"
keystroke "k" using command down
delay 0.1
keystroke "a" using command down
delay 0.1
keystroke "c" using command down
delay 0.1
tell application "Google Chrome"
if it is running then
quit
else
activate
open location "http://translate.google.com"
delay 1
activate
delay 0.7
end if
end tell
tell application "System Events"
keystroke "v" using command down
delay 0.7
keystroke "c" using control down
end tell
end tell
I've exported it from Scripts Editor as an Application, and it works fine when I click on it.
When I try to execute it via a shortcut set at Systems Preferences > Keyboard > Shorcuts > Services, nothing happens; I just see the cog-wheel appearing briefly at the top bar. I already granted permission for the script-app at Systems Preferences > Security & Privacy > Accessibility and already checked if there are hotkeys conflicts at the Terminal typing: defaults find NSServicesStatus
or defaults find '@~$]'
(the shortcut I've tried to use was Command+Alt+Shift+].
Would you maybe have any suggestion of where can I check what I'm doing wrong?
You should reduce GUI scripting to have more stable code. Workaround is shown here:
my copyTextToClipboard()
set sourceText to (the clipboard) as string
my performGoogleTranslate(sourceText)
my getTranslatedTextToClipboard()
on copyTextToClipboard()
tell application "System Events"
keystroke "k" using command down
delay 0.1
keystroke "a" using command down
delay 0.1
keystroke "c" using command down
delay 0.1
end tell
end copyTextToClipboard
on performGoogleTranslate(sourceText)
-- following translates from English (en) to Russian (ru)
-- you can put other languadges settings
set myURL to "https://translate.google.gr/?
hl=el#view=home&op=translate&sl=en&tl=ru&text=" & sourceText
tell application "Google Chrome"
activate
set URL of active tab of window 1 to myURL
end tell
end performGoogleTranslate
on getTranslatedTextToClipboard()
-- HERE you need some mouse tool to move the mouse pointer over
-- the "Copy" button of Google Translate and to click it
end getTranslatedTextToClipboard