Khanna, Videh Rakesh Rakesh
feat: JARVIS security, cross-device, intelligence & polish overhaul
e2a2dda | """JARVIS System Control β full laptop/phone control like Siri.""" | |
| import subprocess | |
| import os | |
| import platform | |
| from tools import tool | |
| # βββ Volume ββββββββββββββββββββββββββββββββββββββ | |
| def set_volume(level: int) -> str: | |
| if level == -1: | |
| subprocess.run(["osascript", "-e", "set volume with output muted"]) | |
| return "Muted" | |
| elif level == -2: | |
| subprocess.run(["osascript", "-e", "set volume without output muted"]) | |
| return "Unmuted" | |
| else: | |
| level = max(0, min(100, level)) | |
| subprocess.run(["osascript", "-e", f"set volume output volume {level}"]) | |
| return f"Volume set to {level}%" | |
| def get_volume() -> str: | |
| result = subprocess.run( | |
| ["osascript", "-e", "output volume of (get volume settings)"], | |
| capture_output=True, text=True, | |
| ) | |
| return f"Volume: {result.stdout.strip()}%" | |
| # βββ Brightness ββββββββββββββββββββββββββββββββββ | |
| def set_brightness(level: float) -> str: | |
| level = max(0.0, min(1.0, level)) | |
| # Use brightness command if available | |
| try: | |
| subprocess.run(["brightness", str(level)], capture_output=True, timeout=5) | |
| return f"Brightness set to {int(level*100)}%" | |
| except Exception: | |
| subprocess.run([ | |
| "osascript", "-e", | |
| f''' | |
| tell application "System Preferences" | |
| reveal anchor "displaysDisplayTab" of pane id "com.apple.preference.displays" | |
| end tell | |
| ''' | |
| ], stderr=subprocess.DEVNULL) | |
| return f"Brightness control requires 'brightness' CLI. Install: brew install brightness" | |
| # βββ Dark Mode βββββββββββββββββββββββββββββββββββ | |
| def toggle_dark_mode(enable: bool) -> str: | |
| mode = "true" if enable else "false" | |
| subprocess.run([ | |
| "osascript", "-e", | |
| f'tell application "System Events" to tell appearance preferences to set dark mode to {mode}' | |
| ]) | |
| return f"Dark mode {'enabled' if enable else 'disabled'}" | |
| # βββ WiFi ββββββββββββββββββββββββββββββββββββββββ | |
| def wifi_control(action: str) -> str: | |
| if action == "on": | |
| subprocess.run(["networksetup", "-setairportpower", "en0", "on"]) | |
| return "WiFi turned on" | |
| elif action == "off": | |
| subprocess.run(["networksetup", "-setairportpower", "en0", "off"]) | |
| return "WiFi turned off" | |
| else: | |
| result = subprocess.run( | |
| ["networksetup", "-getairportnetwork", "en0"], | |
| capture_output=True, text=True, | |
| ) | |
| return result.stdout.strip() | |
| # βββ Bluetooth βββββββββββββββββββββββββββββββββββ | |
| def bluetooth_control(action: str) -> str: | |
| try: | |
| if action == "on": | |
| subprocess.run(["blueutil", "--power", "1"], capture_output=True, timeout=5) | |
| return "Bluetooth on" | |
| else: | |
| subprocess.run(["blueutil", "--power", "0"], capture_output=True, timeout=5) | |
| return "Bluetooth off" | |
| except Exception: | |
| return "Install blueutil: brew install blueutil" | |
| # βββ Do Not Disturb ββββββββββββββββββββββββββββββ | |
| def do_not_disturb(enable: bool) -> str: | |
| if enable: | |
| subprocess.run([ | |
| "osascript", "-e", | |
| ''' | |
| tell application "System Events" | |
| tell process "ControlCenter" | |
| -- Toggle Focus via menu bar | |
| end tell | |
| end tell | |
| ''' | |
| ], stderr=subprocess.DEVNULL) | |
| # Alternative: use shortcuts | |
| subprocess.run(["shortcuts", "run", "Focus On"], capture_output=True, timeout=5) | |
| return "Do Not Disturb enabled" | |
| else: | |
| subprocess.run(["shortcuts", "run", "Focus Off"], capture_output=True, timeout=5) | |
| return "Do Not Disturb disabled" | |
| # βββ Screenshots βββββββββββββββββββββββββββββββββ | |
| def screenshot(area: str = "full") -> str: | |
| path = os.path.expanduser(f"~/Desktop/jarvis_screenshot_{int(__import__('time').time())}.png") | |
| if area == "select": | |
| subprocess.run(["screencapture", "-i", path]) | |
| else: | |
| subprocess.run(["screencapture", path]) | |
| return f"Screenshot saved: {path}" | |
| # βββ Music / Media Control βββββββββββββββββββββββ | |
| def media_control(action: str) -> str: | |
| if action == "now_playing": | |
| result = subprocess.run([ | |
| "osascript", "-e", | |
| ''' | |
| try | |
| tell application "Music" | |
| set trackName to name of current track | |
| set artistName to artist of current track | |
| return trackName & " by " & artistName | |
| end tell | |
| on error | |
| try | |
| tell application "Spotify" | |
| return name of current track & " by " & artist of current track | |
| end tell | |
| on error | |
| return "Nothing playing" | |
| end try | |
| end try | |
| ''' | |
| ], capture_output=True, text=True, timeout=5) | |
| return result.stdout.strip() | |
| key_map = { | |
| "play": "play", | |
| "pause": "pause", | |
| "next": "next track", | |
| "previous": "previous track", | |
| } | |
| cmd = key_map.get(action, "play") | |
| # Try Music app first, then Spotify | |
| subprocess.run([ | |
| "osascript", "-e", | |
| f''' | |
| try | |
| tell application "Music" to {cmd} | |
| on error | |
| try | |
| tell application "Spotify" to {cmd} | |
| end try | |
| end try | |
| ''' | |
| ], capture_output=True, timeout=5) | |
| return f"Media: {action}" | |
| # βββ Clipboard βββββββββββββββββββββββββββββββββββ | |
| def clipboard(action: str, text: str = "") -> str: | |
| if action == "read": | |
| result = subprocess.run(["pbpaste"], capture_output=True, text=True) | |
| return f"Clipboard: {result.stdout[:500]}" | |
| elif action == "write": | |
| proc = subprocess.Popen(["pbcopy"], stdin=subprocess.PIPE) | |
| proc.communicate(text.encode()) | |
| return f"Copied to clipboard" | |
| return "Unknown action" | |
| # βββ Notifications / Alerts ββββββββββββββββββββββ | |
| def send_notification(title: str, message: str) -> str: | |
| safe_title = title.replace('\\', '').replace('"', '').replace("'", '')[:80] | |
| safe_msg = message.replace('\\', '').replace('"', '').replace("'", '')[:120] | |
| subprocess.run([ | |
| "osascript", "-e", | |
| f'display notification "{safe_msg}" with title "{safe_title}" sound name "default"' | |
| ]) | |
| return f"Notification sent: {title}" | |
| # βββ App Management ββββββββββββββββββββββββββββββ | |
| def list_running_apps() -> str: | |
| result = subprocess.run([ | |
| "osascript", "-e", | |
| 'tell application "System Events" to get name of every process whose background only is false' | |
| ], capture_output=True, text=True, timeout=5) | |
| return f"Running apps: {result.stdout.strip()}" | |
| def quit_app(app_name: str) -> str: | |
| safe_name = app_name.replace('\\', '').replace('"', '').replace("'", '').replace('`', '') | |
| subprocess.run([ | |
| "osascript", "-e", | |
| f'tell application "{safe_name}" to quit' | |
| ], capture_output=True, timeout=5) | |
| return f"Quit {app_name}" | |
| # βββ Timer / Alarm βββββββββββββββββββββββββββββββ | |
| def set_timer(seconds: int, label: str = "Timer") -> str: | |
| import threading | |
| def timer_alert(): | |
| import time | |
| time.sleep(seconds) | |
| subprocess.run([ | |
| "osascript", "-e", | |
| f'display dialog "Timer: {label}" with title "JARVIS Timer" buttons {{"OK"}} default button "OK" giving up after 30' | |
| ]) | |
| subprocess.run(["say", "-v", "Daniel", f"Sir, your {label} timer is done."]) | |
| threading.Thread(target=timer_alert, daemon=True).start() | |
| mins = seconds // 60 | |
| secs = seconds % 60 | |
| if mins: | |
| return f"Timer set: {label} β {mins}m {secs}s" | |
| return f"Timer set: {label} β {secs}s" | |
| # βββ Lock Screen / Sleep βββββββββββββββββββββββββ | |
| def lock_screen() -> str: | |
| subprocess.run([ | |
| "osascript", "-e", | |
| 'tell application "System Events" to keystroke "q" using {control down, command down}' | |
| ]) | |
| return "Screen locked" | |
| def sleep_display() -> str: | |
| subprocess.run(["pmset", "displaysleepnow"]) | |
| return "Display sleeping" | |
| # βββ Trash βββββββββββββββββββββββββββββββββββββββ | |
| def empty_trash() -> str: | |
| subprocess.run([ | |
| "osascript", "-e", | |
| 'tell application "Finder" to empty trash' | |
| ], capture_output=True, timeout=10) | |
| return "Trash emptied" | |
| # βββ Text to Speech (announce) βββββββββββββββββββ | |
| def announce(message: str) -> str: | |
| subprocess.run(["say", "-v", "Daniel", "-r", "180", message]) | |
| return f"Announced: {message}" | |
| # βββ Open URL ββββββββββββββββββββββββββββββββββββ | |
| def open_url(url: str) -> str: | |
| subprocess.run(["open", url]) | |
| return f"Opened: {url}" | |
| # βββ Spotlight Search ββββββββββββββββββββββββββββ | |
| def search_files(query: str) -> str: | |
| result = subprocess.run( | |
| ["mdfind", query, "-limit", "10"], | |
| capture_output=True, text=True, timeout=10, | |
| ) | |
| files = result.stdout.strip().split("\n")[:10] | |
| return "\n".join(files) if files[0] else "No files found" | |
| # βββ Create Reminder βββββββββββββββββββββββββββββ | |
| def create_reminder(title: str) -> str: | |
| subprocess.run([ | |
| "osascript", "-e", | |
| f''' | |
| tell application "Reminders" | |
| make new reminder with properties {{name:"{title}"}} | |
| end tell | |
| ''' | |
| ], capture_output=True, timeout=5) | |
| return f"Reminder created: {title}" | |
| # βββ Calendar ββββββββββββββββββββββββββββββββββββ | |
| def get_events() -> str: | |
| result = subprocess.run([ | |
| "osascript", "-e", | |
| ''' | |
| set today to current date | |
| set time of today to 0 | |
| set tomorrow to today + 86400 | |
| tell application "Calendar" | |
| set output to "" | |
| repeat with cal in calendars | |
| set evts to (every event of cal whose start date >= today and start date < tomorrow) | |
| repeat with evt in evts | |
| set output to output & summary of evt & " at " & time string of start date of evt & "\\n" | |
| end repeat | |
| end repeat | |
| if output is "" then | |
| return "No events today" | |
| end if | |
| return output | |
| end tell | |
| ''' | |
| ], capture_output=True, text=True, timeout=10) | |
| return result.stdout.strip() | |