AppleScript Application is not allowed to send keystrokes
Asked Answered
H

7

40

I made an app with AppleScript called FRIDAY. When I tell it to 'open chrome', it opens google chrome, this is working on Script Editor and out side of script editor. I can also tell it to 'open a new tab' and it opens a new tab using keystrokes:

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

Now, the code above only works when I run FRIDAY on Script Editor but it DOES NOT WORK when I save FRIDAY as an application and run it.

Script Editor and my application(FRIDAY) have accessibility, in fact FRIDAY has full disk accessibility. But still it cannot send keystrokes.

I am keep getting this ERROR:

System Events got an error: FRIDAY is not allowed to send keystrokes.

Accessibility: enter image description here Full Disk Access: enter image description here Automation: enter image description here

If you could help me solve this problem, I would really appreciate it, thank you!

Heptamerous answered 3/3, 2019 at 20:12 Comment(7)
System Preferences > Security & Privacy > Privacy > Accessibility. Remove your application. Add it again.Eldredge
Also please note, if you are using a third-party tool, the people to ask about this are the authors of that tool.Eldredge
Also, unless your script application is code-signed or run-only, you will need to add it to the Privacy pane after each use (a script saves properties and global variables in its file).Sharronsharyl
It would really help and you'll get better and more accurate solutions if you post all of your code rather than only two or three line snippets. For example, the problem may actually be in the value of a variable which is set at the very beginning of your code but the error itself may get triggered towards the end of your code. Also, others may be able to identify other pitfalls within your code and offer solutions.Janycejanyte
okay, I am adding all of my code. PLEASE DON'T MAKE FUN OF MY CODE :(Heptamerous
@Heptamerous not here to make fun of code, but there might be an else if construct in AppleScript which seems to apply to your situation. See computers.tutsplus.com/tutorials/… It would allow you to delete some lines and do the same thing. Sorry to digress, carry on!Quinacrine
The whole thing is a (relived) nightmare, but one thing I discovered is that using Javascript rather than AppleScript seems to work more consistently. NO idea why.Accountancy
H
77

I got a similar problem when updated to macOS 10.15 Catalina. I went to System Preferences -> Security & Privacy -> Accessibility and here I deleted my application and then added it again. After this, all work perfectly.

Helwig answered 15/10, 2019 at 19:7 Comment(4)
FWIW, this means the actual applescript file, at least in my case. I wasn't totally clear, but have basically added every conceivable app as well as my script, and at the moment at least, it's working.Rubeola
I just had to toggle off and on once, didn't have to remove and readd.Evanish
I think the problem is specific to how Chrome is installed/updated, because it's the only app that regularly loses permissions (i.e. location).Phelloderm
This is proving to be a serious frustration for me in Ventura. My AppleScript app worked through an upgrade to Ventura, then the first time I made a change, it stopped working, displaying this error. I'm angry. It's not the script, when I can get the perfect combination of sandbox voodoo, it's fine. But that takes a long time.Histolysis
C
12

I also found though it is getting asked to allow your script with access to System Events it is not added to Security & Privacy if your script has spaces in it's name. I was struggling with this issue until I replaced space with an underscore. Then it was added here and worked fine since. My script with underscore instead of space:

Comment answered 25/11, 2019 at 17:7 Comment(3)
Hyphens are also not allowed, in addition to spaces. Replacing it with an underscore fixed it for me.Vallejo
Holy god, after all that messing around in the Accessibility whitelist, it was the spaces in the name!?!? APPLE?!?!?Rubeola
Ok, backing down, I don't think this was my issue sadly. It seems like renaming the file just triggered the same temporary effect where it starts working, but breaks again later.Rubeola
T
3

**

This what worked for me:

**

Go to System Preferences -> Security & Privacy -> Accessibility and give permissions to iterm2

enter image description here

Tuff answered 11/4, 2022 at 10:23 Comment(0)
F
1

I'm a bit late but this is what worked for me:

Go to System Preferences → Security & Privacy → Privacy → Accessibility → Allow the apps below to control your computer. → Checkmark the "Automator" app.

https://i.sstatic.net/WkWEy.png

Forbade answered 30/4, 2022 at 9:53 Comment(0)
G
0

You can enable mouse keys in accessibilty settings and use keystroke 87. That's how my autoclickers work.

Gametocyte answered 4/2, 2021 at 15:23 Comment(1)
please followHow do I write a good answer?Beryllium
G
0

UPDATE 2024

I've tried everything above and nothing worked for me.

What WORKED for me was NOT removingthe Automator app from from accessibility tab BUT ENABLING AND DISABLIBG with toggle button.

And then just in case I closed settings with cmd + q(just in case) and run workflow from Finder app as was suggested above.

enter image description here

Glyoxaline answered 16/2 at 12:30 Comment(0)
P
0

None of the above worked for me, but here's what did:

  1. System Settings -> Privacy & Security -> Accessibility -> UNselect automator and shortcuts
  2. Click System Settings on top left of screen -> Quit System Settings
  3. Open Shortcuts app
  4. Run the action manually. It will trigger the error - check carefully as it may appear behind another window
  5. In any case, go back into System Settings -> Privacy & Security -> Accessibility and select Automator and Shortcuts.
  6. Run it again in Shortcuts and it should work!

All credit to this very helpful answer.

Plainsong answered 16/4 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.