1

The following Automator script is supposed to open a Chrome tab and look up a Chinese word.

The service opens the Chrome tab when Latin characters are chosen, but mysteriously fails to open a tab when Chinese characters are chosen.

Works

  1. Open https://en.wikipedia.org/wiki/List_of_Chinese_classifiers.
  2. Highlight Latin character.
  3. Run service.

Fails

  1. Open https://en.wikipedia.org/wiki/List_of_Chinese_classifiers.
  2. Highlight Chinese character.
  3. Run service.

Run JavaScript

function run(input, parameters) {

    var trimmedInput = input[0].replace(/\s/g, '');
    return trimmedInput
    
}

Run AppleScript

on run {input, parameters}
        
    return "http://www.cantonese.sheik.co.uk/dictionary/search/?searchtype=2&text=" & input
    
end run

Run AppleScript

on run {input, parameters}
    tell application "Google Chrome"
        set curTabIndex to active tab index of front window
                
        open location input
        
        set active tab index of first window to curTabIndex
    end tell
end run
4
  • Why a Run JavaScript action and two Run AppleScript actions when it can be done in a single Run AppleScript action!? Also, open location is a part of Standard Additions, not Google Chrome. So, if you want to open a URL in Google Chrome then set the URL of the active tab to the target URL. BTW This works for me coded using a single Run AppleScript action if I select the first Chinese character in the URL you linked, it goes to http://www.cantonese.sheik.co.uk/dictionary/search/?searchtype=2&text=把 and changes to .../dictionary/characters/821/ Sep 26, 2020 at 23:26
  • @user3439894 thanks for the help. new to apple script and automator, so not sure why it was split into three actions. you're saying it fails if split into three actions but works if consolidated into one action?
    – Crashalot
    Sep 27, 2020 at 3:53
  • @user3439894 also how do you turn the javascript into apple script? thanks for your help!
    – Crashalot
    Sep 27, 2020 at 3:54
  • RE: "you're saying it fails if split into three actions but works if consolidated into one action?" -- No, Im not saying it fails with the three actions, you are. Isn't that why you posted the question? I didn't test with the three actions, just wrote code of one Run AppleScript action and it worked. I posted it as an answer. Sep 27, 2020 at 11:53

1 Answer 1

1

The following example AppleScript code combines the three Automator actions shown in your OP into one Run AppleScript action.

Selecting the first Chinese character, from the link in your OP, and running the Automator Service/Quick Action it worked for me by opening a new tab to the target URL.

  • Note that as coded it assumes Google Chrome is already running with at least one window already opened and it further assumes you are selecting the text in the active tab of the front window, otherwise the coding of the tell block for Google Chrome needs to be modified.
  • The script uses AppleScript's text item delimiters to do the same thing as: .replace(/\s/g, '')
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 ¬
                "http://www.cantonese.sheik.co.uk/dictionary/search/?searchtype=2&text=" & ¬
                searchString
            set active tab index to curTabIndex
        end tell
    end tell
    
end run 

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.

2

You must log in to answer this question.

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