Sunday, December 5, 2021
  • Index
  •  » Automator
  •  » Can someone tell me why this script isnt working?

#1 2021-01-25 01:14:28 pm

steveray100
Member
Registered: 2018-10-05
Posts: 2

Can someone tell me why this script isnt working?

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

Offline

 

#2 2021-03-08 12:46:59 am

scrutinizer82
Member
Registered: 2015-08-04
Posts: 112

Re: Can someone tell me why this script isnt working?

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.

Applescript:

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

Last edited by scrutinizer82 (2021-03-09 08:17:22 am)


Scripting in Mac OS X 10.7.5, OS X 10.9.5, macOS 10.14.6

Offline

 
  • Index
  •  » Automator
  •  » Can someone tell me why this script isnt working?

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)