1

The AppleScript below is supposed to click the first element with the magnifier class on a predefined web page.

The script runs successfully if you comment out the JavaScript line, do JavaScript "document.getElementsByClassName('magnifier')[0].click();"

From the Chrome console, this line of JavaScript executes successfully.

But if you include the JavaScript line, the following error occurs:

The action “Run AppleScript” encountered an error: “The operation couldn’t be completed. (com.apple.Automator error -212.)”

Anyone have any ideas what the problem could be?

on run {input, parameters}
    
    set searchString to input as text
    
    set AppleScript's text item delimiters to space
    set searchString to text items of searchString
    set AppleScript's text item delimiters to ""
    set searchString to searchString as text
    
    tell application "Google Chrome"
        tell front window
            set curTabIndex to active tab index
            set URL of (make new tab) to ¬
                "https://www.yellowbridge.com/chinese/dictionary.php?searchMode=C&word=" & ¬
                searchString
            set active tab index to curTabIndex
            
            delay 2.0
            
            do JavaScript "document.getElementsByClassName('magnifier')[0].click();"
        end tell
    end tell
    
end run 
2

do JavaScript is for Safari. execute javascript is for Google Chrome, e.g.:

tell application "Google Chrome" to tell active tab of front window to ¬
    execute javascript "document.getElementsByClassName('magnifier')[0].click();"

That said, where you have it placed in the script may not be where you really want it.

Here is some example AppleScript code run in Script Editor with Google Chrome opened with at least one window.

It waits for the target URL to finish loading before clicking the magnifying glass and then sets the active tab back to the original active tab.

set input to {"桿"}

set searchString to input as text

set AppleScript's text item delimiters to space
set searchString to text items of searchString
set AppleScript's text item delimiters to ""
set searchString to searchString as text

tell application "Google Chrome"
    tell front window
        set curTabIndex to active tab index
        set URL of (make new tab) to ¬
            "https://www.yellowbridge.com/chinese/dictionary.php?searchMode=C&word=" & ¬
            searchString
        repeat until (loading of active tab is false)
            delay 1
        end repeat
        tell active tab to execute javascript ¬
            "document.getElementsByClassName('magnifier')[0].click();"
        delay 0.5
        set active tab index to curTabIndex
    end tell
end tell 

Update to address comment.

The same conditions mentioned above applies to this example AppleScript code:

set input to {"桿"}

set searchString to input as text

set AppleScript's text item delimiters to space
set searchString to text items of searchString
set AppleScript's text item delimiters to ""
set searchString to searchString as text

tell application "Google Chrome"
    tell front window
        set curTabIndex to active tab index
        set URL of (make new tab) to ¬
            "https://www.yellowbridge.com/chinese/dictionary.php?searchMode=C&word=" & ¬
            searchString
        set active tab index to curTabIndex
        repeat until (loading of last tab is false)
            delay 1
        end repeat
        tell last tab to execute javascript ¬
            "document.getElementsByClassName('magnifier')[0].click();"
    end tell
end tell

Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted.

| improve this answer | |
  • thanks so much again! this worked. is there anyway to have the javascript execute without switching tabs? – Crashalot Sep 27 at 23:11
  • put another way, is it possible for applescript to run on tabs that aren't active? – Crashalot Sep 27 at 23:15
  • @Crashalot, See example AppleScript code under Update to address comment. in the updated answer. Remember this is just example code, if need be it can be written differently to target a different tab by programmatically determining the target tab and use variables as necessary, ect. – user3439894 Sep 27 at 23:51
  • this worked! you're amazing. thanks so much for your prompt responses. :) – Crashalot Sep 28 at 0:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .