-
Notifications
You must be signed in to change notification settings - Fork 12
/
macos_defaults.py
94 lines (77 loc) · 3.08 KB
/
macos_defaults.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
from urllib.parse import unquote, urlparse
from talon import Context, Module, actions, app, settings, ui
from talon.mac import applescript
mod = Module()
ctx = Context()
ctx.matches = r"""
os: mac
"""
# TODO(pcohen): support application names
mod.setting(
"preferred_terminal",
type=str,
default="com.apple.Terminal",
desc="The application bundle to use in the 'open in terminal' commands (e.g., 'com.apple.Terminal', 'com.googlecode.iterm2')",
)
@ctx.action_class("user")
class user_actions:
"""Default macOS implementations for user actions"""
def file_manager_current_path():
"""A generic fallback for that will use `window.doc` to provide the current path
in applications that don't implement a more specific function."""
# NOTE(pcohen): using AXDocument because active_window().doc
# returns URL-encoded path sometimes: https://github.com/talonvoice/talon/issues/473
window = ui.active_window()
try:
url = urlparse(window.element.AXDocument)
if url.scheme == "file": # will there ever be other schemes?
return unquote(url.path)
except AttributeError:
pass
# Fallback to doc
return window.doc
def file_manager_terminal_here():
"""Default implementation that opens the current directory in the terminal"""
path = actions.user.file_manager_current_path()
if not path:
app.notify(
"Can't open terminal",
"The current application does not report a path to open",
)
return
if os.path.isfile(path):
# If the application gave a file, open the terminal in its parent directory.
path = os.path.abspath(os.path.join(path, os.pardir))
if not os.path.exists(path):
app.notify(
"Can't open terminal",
f"The current application reported a path that doesn't exist: {path}",
)
return
escaped_path = path.replace(r'"', r"\"")
applescript.run(
rf"""
tell application id "{settings.get('user.preferred_terminal')}"
activate
open "{escaped_path}"
end tell
"""
)
@ctx.action_class("edit")
class Actions:
"""Default macOS implementations for edit actions"""
def selected_text() -> str:
try:
selected_text = ui.focused_element().AXSelectedText
if not selected_text:
# Some partially-accessible applications incorrectly report empty selections sometimes, and this can be
# quite bad depending on the use case. For maximum safety we have to fall back to the clipboard
# implementation in this case. This could be customized if we knew the application was fully
# trustworthy.
#
# See https://github.com/phillco/talon-axkit/issues/59
return actions.next()
return selected_text
except Exception:
return actions.next()