Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve mac support #37

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@
desc="The key that is used as the NVDA key",
)

mod.setting(
"voiceover_key",
type=str,
default="capslock",
desc="The key that is used as the voiceover key",
)


mod.setting(
"speak_errors",
type=bool,
Expand Down
2 changes: 1 addition & 1 deletion voiceover/readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# VoiceOver

TODO
This integration is a work in progress. VoiceOver is difficult to customize or script since it is all closed source and most of the best APIs are only able to be queried via objc or swift, not Python unless ffi is brought into play.

## Setup

Expand Down
37 changes: 30 additions & 7 deletions voiceover/voiceover.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import os
import sys

from talon import Context, Module, actions, cron, settings
from talon import Context, Module, actions, cron, settings, ui

mod = Module()
ctx = Context()
ctx.matches = r"""
os: mac
"""

from talon.mac import applescript


@ctx.action_class("user")
class MacActions:
Expand All @@ -19,7 +21,7 @@ def with_voiceover_mod_press(key: str):
"""Presses the given key with the voiceover modifier key"""
voiceover_key = settings.get("user.voiceover_key")
actions.key(f"{voiceover_key}:down")
actions.sleep("50ms")
actions.sleep("100ms")
actions.key(key)
actions.sleep("10ms")
actions.key(f"{voiceover_key}:up")
Expand All @@ -32,6 +34,7 @@ def with_voiceover_mod_press(key: str):
def set_voiceover_running_tag():
"""Update tags based on if voiceover is running"""
# Edge case on startup this might not be set yet we we catch all exceptions

try:
ctx.tags = (
["user.voiceover_running"] if actions.user.is_voiceover_running() else []
Expand All @@ -48,13 +51,23 @@ def set_voiceover_running_tag():
class Actions:
def is_voiceover_running() -> bool:
"""Returns true if voiceover is running"""
return True if "user.voiceover_running" in ctx.tags else False
if os.name != "darwin":
return False

# TODO : figure out how a way to check this without spamming subprocesses
return False

def voiceover_tts(text: str):
"""text to speech with voiceover"""

def with_voiceover_mod_press(key: str):
"""Presses the given key with the voiceover modifier key"""
voiceover_key = settings.get("user.voiceover_key")
actions.key(f"{voiceover_key}:down")
actions.sleep("50ms")
actions.key(key)
actions.sleep("10ms")
actions.key(f"{voiceover_key}:up")


ctxVoiceoverRunning = Context()
Expand All @@ -65,8 +78,18 @@ def with_voiceover_mod_press(key: str):

@ctxVoiceoverRunning.action_class("user")
class VoiceoverActions:
def voiceover_tts(text: str):
def tts(text: str, interrupt: bool = True):
"""text to speech with voiceover"""
script_path = os.path.join(
os.path.dirname(__file__), "voiceover_tts.applescript"
)

def wrapper(text):
res = applescript.run(
f"""

tell application "VoiceOver"
output "{text}"
end tell
"""
)

# spawn it on a different thread so if it stalls we don't hang
cron.after("0s", lambda: wrapper(text))
12 changes: 12 additions & 0 deletions voiceover/voiceover.talon
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
os: mac
-

read below: key(ctrl-alt-a)
next heading: key(cmd-ctrl-alt-h)
previous heading: key(cmd-ctrl-alt-shift-h)
next landmark: key(ctrl-alt-cmd-n)

# TODO voiceover mod not working
reader help: user.with_voiceover_mod_press("h")

screen curtain: user.with_voiceover_mod_press("shift-fn-_")
36 changes: 0 additions & 36 deletions voiceover/voiceover_tts.applescript

This file was deleted.

Loading