Automator service to look up words works with Latin characters but not Chinese characters

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

Solution 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.