Talon & Applescript Error, Cant Send Key Strokes - Intermittently
Issue
Getting the following issue when attempting to run apple script from Talon.
ApplescriptErr: System Events got an error: Talon is not allowed to send keystrokes.
I have already given Talon system permissions for:
- Input monitoring
- Accessibility
- Automation
IF I re-toggle Accessibility setting (turn accessability for Talon off/on) And then restart talon it starts working again for some time.
Context:
Full Error from talon log
2025-03-07 09:16:59.392 ERROR cb error topic="phrase" cb=SpeechSystem.engine_event
16: lib/python3.11/threading.py:995 * # cron thread
15: lib/python3.11/threading.py:1038*
14: lib/python3.11/threading.py:975 *
13: talon/cron.py:156 |
12: talon/vad.py:23 |
11: talon/vad.py:129 |
10: talon/scripting/speech_system.py:369 |
9: talon/engines/w2l.py:742 |
8: talon/scripting/dispatch.py:134 | # 'phrase' main:EngineProxy._redispatch()
7: talon/scripting/speech_system.py:66 |
6: -------------------------------------# [stack splice]
5: talon/scripting/dispatch.py:134 | # 'phrase' main:SpeechSystem.engine_event()
4: talon/scripting/speech_system.py:442 |
3: talon/scripting/executor.py:111 |
2: talon/scripting/talon_script.py:707 |
1: talon/scripting/talon_script.py:610 |
talon.scripting.talon_script.TalonScriptError:
in script at /Users/nickolaykondratyev/.talon/user/community/core/nk/nk_app/nk_app_launcher.talon:40:
> user.run_bash_function_in_terminal_cleanly('hi')
ApplescriptErr: System Events got an error: Talon is not allowed to send keystrokes.
Talon call
hi$: user.run_bash_function_in_terminal_cleanly("hi")
Python code that backs talon call
from talon import Context, Module, actions
from talon.mac import applescript
mod = Module()
ctx = Context()
def apple_script_activate(app_name: str):
script = """
tell application "%s"
activate
end tell
""" % app_name
applescript.run(script)
def _run_bash_function_in_terminal_cleanly(function_name: str, arguments: str = ""):
"""Run a bash function in Terminal.app after canceling any running command (Ctrl+C)"""
command = function_name if not arguments else f"{function_name} {arguments}"
script = f"""
tell application "Terminal"
activate
tell application "System Events"
keystroke "c" using control down
delay 0.1
end tell
do script "{command}" in front window
end tell
"""
print("Script: {}".format(script))
applescript.run(script)
def run_bash_function_in_iterm_with_cancel(function_name: str, arguments: str = ""):
"""Run a bash function in iTerm after canceling any running command (Ctrl+C)"""
command = function_name if not arguments else f"{function_name} {arguments}"
script = f"""
tell application "iTerm"
activate
tell application "System Events"
keystroke "c" using control down
delay 0.1
end tell
tell current window
tell current session
write text "{command}"
end tell
end tell
end tell
"""
print("Script: {}".format(script))
applescript.run(script)
# noinspection PyMethodParameters
@mod.action_class
class Actions:
def run_bash_function_in_terminal_cleanly(function_name: str, arguments: str = ""):
"""Run a bash function in Terminal.app"""
_run_bash_function_in_terminal_cleanly(function_name, arguments)
def run_bash_function_in_iterm_cleanly(function_name: str, arguments: str = ""):
"""Run a bash function in iTerm after canceling any running process"""
run_bash_function_in_iterm_with_cancel(function_name, arguments)
def launch_app(app_name: str):
"""Launch app"""
print("""launch_app: {}""".format(app_name))
apple_script_activate(app_name)