AppleScript应用程序不允许发送按键信息。

34

我用AppleScript制作了一个名为FRIDAY的应用程序。当我告诉它“打开chrome”时,它会打开Google Chrome,在Script Editor和脚本编辑器外部都可以正常工作。我还可以告诉它“打开一个新标签”,然后使用按键打开一个新标签:

-- this boolean is the reason this script keeps runing
set condition to false
say "Welcome sir"
set commands to {"what is todays date", "what time is it", "what is the time", "what day is it", "what month is it", "which month is it", "friday you up", "friday", "friday you tere", "you there", "empty the trash", "take out the trash", "clean up", "new tab", "next tab", "back", "take a nap", "go to sleep", "take a rest", "wake up", "copy the link", "paste the link", "give me list of running apps", "open a folder", "open chrome", "open youtube", "open atom", "quit atom", "goodbye FRIDAY_AI", "okay thanks", "okay thank you", "thanks", "thank you", "good night", "good morning", "morning", "night", "hey friday"}
repeat until condition is true
    try
        tell application "SpeechRecognitionServer"
            set FRIDAY_AI to listen continuously for commands with identifier "mine" with section title "FRIDAY's Commands"
        end tell

    -- full date
    set todays_date to date string of (current date)
    -- day
    set todays_day to weekday of (current date) as string
    -- time
    set t to time string of (current date)
    -- month
    set m to month of (current date) as string


    -- time and day
    if FRIDAY_AI = "what is todays date" then
        say todays_date
    end if
    if FRIDAY_AI = "what time is it" then
        say t
    end if
    if FRIDAY_AI = "what is the time" then
        say t
    end if
    if FRIDAY_AI = "what day is it" then
        say todays_day
    end if
    if FRIDAY_AI = "what month is it" then
        say m
    end if
    if FRIDAY_AI = "which month is it" then
        say m
    end if
    -- end of time and day


    -- Clear trash --
    if FRIDAY_AI = "empty the trash" then
        tell application "Finder"
            empty the trash
        end tell
    end if

    -- FRIDAY --
    if FRIDAY_AI = "friday" then
        say "yes"
    end if

    if FRIDAY_AI = "friday you up" then
        say "for you sir, alllways"
    end if

    if FRIDAY_AI = "friday you there" then
        say "yes"
    end if

    if FRIDAY_AI = "you there" then
        say "yes"
    end if

    if FRIDAY_AI = "hey friday" then
        say "hey sir, waht can I do for you"
    end if
    -- END OF FRIDAY --


    if FRIDAY_AI = "take out the trash" then
        tell application "Finder"
            empty the trash
        end tell
    end if
    -- end of clear trash --

    -- QUITS EVERY APPLICATION --
    if FRIDAY_AI = "clean up" then
        say "cleaning up"
        delay 2
        say "goodbye sir"
        tell application "System Events" to set the visible of every process to true

        set white_list to {"Finder"}

        try
            tell application "Finder"
                set process_list to the name of every process whose visible is true
            end tell
            repeat with i from 1 to (number of items in process_list)
                set this_process to item i of the process_list
                if this_process is not in white_list then
                    tell application this_process
                        quit
                    end tell
                end if
            end repeat
        on error
            tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
        end try
    end if
    -------------------------------------


    -- SWITCH BETWEEN TABS TABS --
    if FRIDAY_AI = "new tab" then
        tell application "System Events"
            key code 17 using {command down}
        end tell
    end if
    --next tab --
    if FRIDAY_AI = "next tab" then
        tell application "System Events"
            key code 48 using {control down}
        end tell
    end if
    -- priveus tab --
    if FRIDAY_AI = "back" then
        tell application "System Events"
            key code 48 using {shift down, control down}
        end tell
    end if
    -- END OF SWITCH BETWEEN TABS --




    -- launches screen saver  
    if FRIDAY_AI = "take a nap" then
        try
            tell application id "com.apple.ScreenSaver.Engine" to launch
        end try
    end if
    if FRIDAY_AI = "go to sleep" then
        try
            tell application id "com.apple.ScreenSaver.Engine" to launch
        end try
    end if
    if FRIDAY_AI = "take a rest" then
        try
            tell application id "com.apple.ScreenSaver.Engine" to launch
        end try
    end if

    if FRIDAY_AI = "wake up" then
        tell application "System Events"
            key code 49
        end tell
    end if
    -- END OF SCREEN SAVER --





    if FRIDAY_AI = "give me list of running apps" then
        tell application "System Events"
            key code 53 using {command down, option down}
        end tell
    end if



    -- copy url --
    if FRIDAY_AI = "copy the link" then
        tell application "System Events"
            key code 37 using {command down}
            delay 0.5
            key code 8 using {command down}
        end tell
    end if

    -- paste url --
    if FRIDAY_AI = "paste the link" then
        tell application "System Events"
            key code 37 using {command down}
            delay 0.5
            key code 9 using {command down}
            keystroke return
        end tell
    end if




    if FRIDAY_AI = "open a folder" then
        log FRIDAY_AI
        tell application "Finder"
            activate
            make new Finder window
            set target of front window to home
        end tell
    end if

    if FRIDAY_AI = "open chrome" then
        tell application "Google Chrome"
            log FRIDAY_AI
            say "Opening Google Chrome sir"
            activate
        end tell
    end if

    (* if FRIDAY_AI = "open atom" then
    tell application "Atom"
        log FRIDAY_AI
        say "opening Atom"
        activate
    end tell
end if 

if FRIDAY_AI = "quit atom" then
    log FRIDAY_AI
    tell application "Atom" to quit
end if *)

    if FRIDAY_AI = "goodbye FRIDAY_AI" then
        log FRIDAY_AI
        say "goodbye sir"
        -- quit = exist the program completely--
        -- Stop the script- -
        exit repeat
    end if

    if FRIDAY_AI = "open youtube" then
        log FRIDAY_AI
        tell application "Google Chrome"
            activate
            open location "https://www.youtube.com/"
        end tell
    end if

    -- JUST PERSONAL COMMENTARY --
    if FRIDAY_AI = "thanks" then
        log FRIDAY_AI
        say "I am glad, I could be of help sir"
    end if

    if FRIDAY_AI = "okay thanks" then
        log FRIDAY_AI
        say "I am glad, I could be of help sir"
    end if

    if FRIDAY_AI = "thank you" then
        log FRIDAY_AI
        say "You're welcome sir"
    end if

    if FRIDAY_AI = "okay thank you" then
        log FRIDAY_AI
        say "You're welcome sir"
    end if

    if FRIDAY_AI = "good night" then
        log FRIDAY_AI
        say "Good night sir"
    end if

    if FRIDAY_AI = "night" then
        log FRIDAY_AI
        say "Good night sir"
    end if

    if FRIDAY_AI = "good morning" then
        log FRIDAY_AI
        say "Good morning sir"
    end if

    if FRIDAY_AI = "morning" then
        log FRIDAY_AI
        say "morning sir"
    end if

    -- END OF PERSONAL COMMENTARY --
    -- if an error occurs, it goes back and keep repeating
    on error
        set condition to false
    end try


end repeat

现在,以上代码只能在Script Editor上运行时正常工作,但当我将FRIDAY保存为应用程序并运行它时,它就无法工作。

Script Editor和我的应用程序(FRIDAY)都拥有辅助功能,事实上FRIDAY拥有完全磁盘辅助功能。但仍然无法发送按键。

我不断收到这个错误:

System Events出错:FRIDAY不允许发送按键。

辅助功能: enter image description here 完全磁盘访问: enter image description here 自动化: enter image description here

如果您能帮助我解决这个问题,我会非常感激,谢谢!


10
系统偏好设置 > 安全性与隐私 > 隐私 > 辅助功能。移除您的应用程序,然后重新添加它。 - matt
请注意,如果您正在使用第三方工具,则应向该工具的作者咨询此事。 - matt
如果您发布整个代码而不仅仅是两三行片段,那么这将非常有帮助,您将获得更好、更准确的解决方案。例如,问题实际上可能在代码开头设置的某个变量的值中,但错误本身可能会在代码末尾触发。此外,其他人可能能够识别您代码中的其他陷阱并提供解决方案。 - wch1zpink
3
好的,我正在添加我的所有代码。请不要嘲笑我的代码 :( - WeeeHaaa
整个事情都是一个(重复的)噩梦,但我发现使用Javascript而不是AppleScript似乎更加一致。为什么我也不知道。 - taranaki
显示剩余2条评论
5个回答

69

我在升级到macOS 10.15 Catalina后遇到了类似的问题。我前往“系统偏好设置”->“安全性与隐私”->“辅助功能”,在那里删除了我的应用程序,然后再次添加它。这样之后,一切都运行得很完美。


就我而言,FWIW 意味着实际的 AppleScript 文件。我没有完全清楚,但基本上已经添加了每个可想象的应用程序以及我的脚本,目前至少它是可以工作的。 - jerclarke
2
我只需要切换一次开关,不需要移除和重新添加。 - haridsv
我认为问题特定于Chrome的安装/更新方式,因为它是唯一经常失去权限(即位置)的应用程序。 - Carl Walsh
这对我在Ventura中来说是一个严重的挫败感。我的AppleScript应用程序在升级到Ventura后仍然可以工作,但第一次进行更改后,它停止工作,并显示此错误。我很生气。这不是脚本的问题,当我能够得到完美的沙盒巫术组合时,它就可以正常工作。但那需要很长时间。 - Trellis

11

我发现,如果你的脚本名字中有空格,即使被要求允许你的脚本访问系统事件,它也不会被添加到“安全与隐私”中。我曾经因为这个问题苦恼,直到把空格替换成下划线才行。然后它就被添加到这里了,而且一切正常。 我的脚本用下划线代替空格:


3
除了空格之外,连字符也不允许使用。将其替换为下划线对我有用。 - adrum
天啊,经过所有在辅助功能白名单中搞来搞去的操作,竟然是名称中的空格?!?苹果?!? - jerclarke
1
好的,撤回,不幸的是我认为这不是我的问题。看起来重命名文件只是触发了相同的暂时效果,在稍后又会出现故障。 - jerclarke

2

以下是我为您翻译的内容:

这是我的解决方法:

前往“系统偏好设置” -> “安全性与隐私” -> “辅助功能”,并授予iterm2权限。

enter image description here


1

我有些晚了,但以下是对我有效的方法:

进入“系统偏好设置”→“安全性与隐私”→“隐私”→“辅助功能”→“允许下列应用程序控制您的计算机”→勾选“Automator”应用程序。

https://istack.dev59.com/WkWEy.webp


0

您可以在辅助功能设置中启用鼠标键,并使用快捷键87。这就是我的自动点击器的工作方式。


1
请遵循以下链接了解如何撰写优秀的回答:如何撰写好的回答? - Willie Cheng

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接