Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在AppleScript中判断输入属于哪种语言?_Applescript_Automator - Fatal编程技术网

如何在AppleScript中判断输入属于哪种语言?

如何在AppleScript中判断输入属于哪种语言?,applescript,automator,Applescript,Automator,我想在macOS上的服务中添加一项,例如: 我在网页上选择一个词,然后在维基百科上搜索它的解释 如果单词是英文,我想搜索“”,如果单词是中文,我想搜索“”。所以首先我需要判断输入的语言。我想知道如何在AppleScript中实现它。谢谢。据我所知,确定一段文本的语言不能用香草AppleScript完成,但可以用AppleScriptObjC完成 尽管你对这个启示不太感兴趣,但我还是加入了一个片段,展示了做你想做的事情其实很容易: to guessLanguage for input

我想在macOS上的服务中添加一项,例如:

我在网页上选择一个词,然后在维基百科上搜索它的解释


如果单词是英文,我想搜索“”,如果单词是中文,我想搜索“”。所以首先我需要判断输入的语言。我想知道如何在AppleScript中实现它。谢谢。

据我所知,确定一段文本的语言不能用香草AppleScript完成,但可以用AppleScriptObjC完成

尽管你对这个启示不太感兴趣,但我还是加入了一个片段,展示了做你想做的事情其实很容易:

    to guessLanguage for input
        script
            use framework "Foundation"
            property this : a reference to current application
            property NSLTag : a reference to NSLinguisticTagger of this

            to guessLanguage()
                (NSLTag's dominantLanguageForString:input) as text
            end guessLanguage
        end script

        result's guessLanguage()
    end guessLanguage
您应该将此处理程序放在脚本的底部,在
结束运行之后。可以从脚本中的任何位置调用它,如下所示:

    on run {input, parameters}
        set [input] to the input
        guessLanguage for the input
        set lang to text 1 thru 2 of result

        open location "https://" & lang & ".wikipedia.org/wiki/" & input
    end run

    to guessLanguage for input
        .
        .
        .
    end guessLanguage
以下是对不同语言样本的两次测试运行,显示了每个样本旁边的返回结果:

    guessLanguage for "Hello, how are you?" --> "en"
    guessLanguage for "Guten Tag, wie gehts?" --> "de"
    guessLanguage for "Salut, ça va?" --> "fr" 
    guessLanguage for "你好!你好嗎?" --> "zh-Hant"
    guessLanguage for "こんにちは、元気ですか?" --> "ja"

展示你的方法。谢谢。
运行时{input,parameters}打开位置“https://zh.wikipedia.org/wiki/“&input return input end run
您不能。您需要使用AppleScriptObjC.Sad,但是谢谢您