服务的 AppleScript 快捷方式不起作用,尽管单击脚本编辑器生成的应用程序工作正常

AppleScript shortcut for service not working, although clicking on application generated by Script Editor works fine

我为一系列复制粘贴击键和延迟创建了一个 AppleScript:

示例 AppleScript 代码:

tell application "System Events"
    keystroke "k" using command down
    delay 0.1
    keystroke "a" using command down
    delay 0.1
    keystroke "c" using command down
    delay 0.1
    
    tell application "Google Chrome"
        if it is running then
            quit
        else
            activate
            open location "http://translate.google.com"
            delay 1
            activate
            delay 0.7
        end if
    end tell
    
    tell application "System Events"
        keystroke "v" using command down
        delay 0.7
        keystroke "c" using control down
    end tell
end tell

我已经将它作为应用程序从脚本编辑器中导出,当我点击它时它工作正常。

当我尝试通过在“系统偏好设置”>“键盘”>“快捷方式”>“服务”中设置的快捷方式执行它时,没有任何反应;我只看到齿轮短暂地出现在顶部栏上。我已经在“系统偏好设置”>“安全和隐私”>“辅助功能”中授予了脚本应用程序的权限,并且已经检查了终端输入是否存在热键冲突:defaults find NSServicesStatusdefaults find '@~$]'(我尝试过的快捷方式使用的是 Command+Alt+Shift+].

关于我在哪里可以检查我做错了什么,您是否有任何建议?

您应该减少 GUI 脚本以获得更稳定的代码。此处显示了解决方法:

my copyTextToClipboard()
set sourceText to (the clipboard) as string
my performGoogleTranslate(sourceText)
my getTranslatedTextToClipboard()

on copyTextToClipboard()
    tell application "System Events"
        keystroke "k" using command down
        delay 0.1
        keystroke "a" using command down
        delay 0.1
        keystroke "c" using command down
        delay 0.1
    end tell
end copyTextToClipboard

on performGoogleTranslate(sourceText)
    -- following translates from English (en) to Russian (ru)
    -- you can put other languadges settings
    set myURL to "https://translate.google.gr/?
    hl=el#view=home&op=translate&sl=en&tl=ru&text=" & sourceText
    tell application "Google Chrome"
        activate
        set URL of active tab of window 1 to myURL
    end tell
end performGoogleTranslate

on getTranslatedTextToClipboard()
    -- HERE you need some mouse tool to move the mouse pointer over
    -- the "Copy" button of Google Translate and to click it
end getTranslatedTextToClipboard

这是一个完全不同的选项,无需使用 UI 脚本,也无需打开任何浏览器。

为了能够使用我的解决方案,您必须使用此命令安装 translate-shell shell command. In Terminal.app, I installed it using Homebrewbrew install translate-shell

安装成功后translate-shell shell command, it can then be used in AppleScripts and Automator Workflows

以下 AppleScript 代码将获取当前在剪贴板上的文本,并将其翻译成您选择的语言。然后它将剪贴板的内容设置为翻译后的文本。

我冒昧地添加了一些 Language Codes 以帮助您入门。

property convertLanguage : {"Convert To Belarusian (be)", "Convert To Bulgarian (bg)", ¬
    "Convert To Dutch (nl)", "Convert To English (en)", "Convert To Estonian (et)", ¬
    "Convert To French (fr)", "Convert To German (de)", "Convert To Greek (el)", ¬
    "Convert To Hebrew (he)", "Convert To Hungarian (hu)", "Convert To Italian (it)", ¬
    "Convert To Polish (pl)", "Convert To Romanian (ro)", "Convert To Russian (ru)", ¬
    "Convert To Spanish (es)", "Convert To Swedish (sv)", "Convert To Ukrainian (uk)"}

activate
set chosenLanguage to word 4 of ((choose from list convertLanguage ¬
    with title "Language Translator" with prompt ¬
    "Choose A Language To Convert To" OK button name ¬
    "Translate" cancel button name "Cancel") as text)

convertToLanguage(chosenLanguage)

on convertToLanguage(twoLetterLanguageCode)
    set textToConvert to the clipboard
    delay 0.1
    set the clipboard to ¬
        (do shell script "export PATH=\"/usr/local/bin:$PATH\";/usr/local/bin/trans -b :" & ¬
            quoted form of twoLetterLanguageCode & " " & ¬
            quoted form of (textToConvert as text))
end convertToLanguage

以下动画演示了将英文文本(已在我的剪贴板上)转换为法文。然后我将转换后的文本粘贴到文档中。