Repository navigation
Expand file tree
/
Copy pathtools.py
More file actions
220 lines (169 loc) · 6.28 KB
/
Copy pathtools.py
File metadata and controls
220 lines (169 loc) · 6.28 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""
tools.py — the actual "hands" of Jarvis.
Every function here is decorated with @needle.tool. Needle reads the
function signature + docstring to decide WHEN to call it and HOW to
fill in the arguments from what you said. Keep docstrings short and
concrete — that's the only "prompt engineering" Needle needs.
All of these run real commands on macOS via AppleScript (osascript)
or shell. Nothing here touches the internet except web_search /
get_weather, which just open a browser tab (no API keys needed).
"""
import subprocess
import webbrowser
import datetime
import platform
import json
from urllib.parse import quote_plus
import needle
IS_MAC = platform.system() == "Darwin"
def _run_applescript(script: str) -> str:
"""Run an AppleScript snippet and return its stdout (stripped)."""
if not IS_MAC:
return "(skipped — not running on macOS)"
result = subprocess.run(
["osascript", "-e", script],
capture_output=True,
text=True,
)
if result.returncode != 0:
return f"error: {result.stderr.strip()}"
return result.stdout.strip()
def _run_shell(cmd: list[str]) -> str:
if not IS_MAC:
return "(skipped — not running on macOS)"
result = subprocess.run(cmd, capture_output=True, text=True)
return result.stdout.strip() or result.stderr.strip()
def _open_safari_tab(url: str) -> str:
"""Open a URL in a new Safari tab."""
script_url = json.dumps(url)
return _run_applescript(
f'''tell application "Safari"
activate
if (count of windows) is 0 then make new document
tell front window to make new tab with properties {{URL:{script_url}}}
end tell'''
)
# ---------------------------------------------------------------------
# App control
# ---------------------------------------------------------------------
@needle.tool
def open_app(app_name: str):
"Open or switch to a Mac application, e.g. Safari, Notes, Spotify, Terminal."
out = _run_applescript(f'tell application "{app_name}" to activate')
return {"app": app_name, "status": "opened" if "error" not in out else out}
@needle.tool
def quit_app(app_name: str):
"Quit / close a running Mac application."
out = _run_applescript(f'tell application "{app_name}" to quit')
return {"app": app_name, "status": "quit" if "error" not in out else out}
# ---------------------------------------------------------------------
# System controls
# ---------------------------------------------------------------------
@needle.tool
def set_volume(level: int):
"Set the system output volume to a percentage from 0 to 100."
level = max(0, min(100, level))
_run_applescript(f"set volume output volume {level}")
return {"volume": level}
@needle.tool
def mute():
"Mute the system volume."
_run_applescript("set volume output muted true")
return {"status": "muted"}
@needle.tool
def unmute():
"Unmute the system volume."
_run_applescript("set volume output muted false")
return {"status": "unmuted"}
@needle.tool
def set_brightness(level: int):
"Set the screen brightness to a percentage from 0 to 100 (requires 'brightness' CLI, see README)."
level = max(0, min(100, level)) / 100
out = _run_shell(["brightness", str(level)])
return {"brightness_percent": int(level * 100), "note": out}
@needle.tool
def sleep_display():
"Turn off / sleep the Mac's display."
_run_shell(["pmset", "displaysleepnow"])
return {"status": "display sleeping"}
@needle.tool
def lock_screen():
"Lock the Mac's screen immediately."
_run_applescript(
'tell application "System Events" to keystroke "q" using {control down, command down}'
)
return {"status": "locked"}
@needle.tool
def empty_trash():
"Empty the macOS Trash."
out = _run_applescript('tell application "Finder" to empty trash')
return {"status": "trash emptied" if "error" not in out else out}
# ---------------------------------------------------------------------
# Info / utility
# ---------------------------------------------------------------------
@needle.tool
def get_time():
"Get the current time."
return {"time": datetime.datetime.now().strftime("%I:%M %p")}
@needle.tool
def get_date():
"Get today's date."
return {"date": datetime.datetime.now().strftime("%A, %d %B %Y")}
@needle.tool
def web_search(query: str):
"Search the web in a new Safari tab."
url = f"https://www.google.com/search?q={quote_plus(query)}"
_open_safari_tab(url)
return {"searched": query}
@needle.tool
def get_weather(city: str):
"Check the weather for a city in a new Safari tab."
url = f"https://www.google.com/search?q={quote_plus('weather in ' + city)}"
_open_safari_tab(url)
return {"city": city, "status": "opened weather page"}
@needle.tool
def open_url(url: str):
"Open a specific website URL in a new Safari tab."
if not url.startswith("http"):
url = "https://" + url
_open_safari_tab(url)
return {"opened": url}
# ---------------------------------------------------------------------
# Reminders / Notes (via Apple's own apps — no extra setup)
# ---------------------------------------------------------------------
@needle.tool
def create_reminder(text: str):
"Create a reminder in the Reminders app."
script = f'''
tell application "Reminders"
tell list "Reminders"
make new reminder with properties {{name:"{text}"}}
end tell
end tell
'''
_run_applescript(script)
return {"reminder": text}
@needle.tool
def create_note(text: str):
"Create a new note in the Notes app with the given text."
script = f'''
tell application "Notes"
tell account "iCloud"
make new note with properties {{body:"{text}"}}
end tell
end tell
'''
_run_applescript(script)
return {"note": text}
@needle.tool
def speak(text: str):
"Say something out loud using the Mac's built-in text-to-speech voice."
_run_shell(["say", text])
return {"said": text}
# Collect every tool defined above so jarvis.py can just import this list.
ALL_TOOLS = [
open_app, quit_app,
set_volume, mute, unmute, set_brightness, sleep_display, lock_screen, empty_trash,
get_time, get_date, web_search, get_weather, open_url,
create_reminder, create_note, speak,
]