Last active
May 28, 2025 20:39
-
-
Save svrc/199b5005d9495ea2716758638b4636b4 to your computer and use it in GitHub Desktop.
Hammerspoon config for keyboard shortcut to connect Apple Vision Pro, and Lock Screen on disconnect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Position settings, modify if you find the mouse is at incorrect position when you press the hotkey. | |
| screenMirroringIconX = 215 -- position in px of icon from right of screen | |
| visionProOptionY = 125 -- position in px of icon from top of screen | |
| -- Hotkey settings, default is cmd+opt+ctrl+m | |
| connectHotkey = "m" | |
| connectHotkeyModifiers = { 'command', 'option', 'control' } | |
| hs.hotkey.bind(connectHotkeyModifiers, connectHotkey, function() | |
| -- only execute if the primary screen is the Built-in Retina Display | |
| local screen = hs.screen.primaryScreen() | |
| if screen:name() == "Built-in Retina Display" then | |
| hs.alert.show("Trying to connect to Vision Pro...") | |
| local mirroringIconPosition = hs.geometry.point( | |
| screen:frame().x + screen:frame().w - screenMirroringIconX, 0) | |
| setMousePosition(mirroringIconPosition, screen) | |
| local visionProOptionPosition = hs.geometry.point( | |
| screen:frame().x + screen:frame().w - screenMirroringIconX, visionProOptionY) | |
| hs.timer.doAfter(0.2, function() | |
| hs.eventtap.leftClick(hs.mouse.absolutePosition()) | |
| setMousePosition(visionProOptionPosition, screen) | |
| hs.eventtap.leftClick(hs.mouse.absolutePosition()) | |
| end) | |
| else | |
| hs.alert.show("Not using built-in display, ignored") | |
| end | |
| end) | |
| setMousePosition = function(pos, screen) | |
| hs.mouse.setRelativePosition(pos, screen) | |
| hs.eventtap.event.newMouseEvent(hs.eventtap.event.types.mouseMoved, pos):post() | |
| end | |
| -- Function is now defined above | |
| primaryScreenName = hs.screen.primaryScreen():name() | |
| function screenChangedCallback() | |
| local primaryScreen = hs.screen.primaryScreen() | |
| if primaryScreen:name() == primaryScreenName then | |
| return | |
| end | |
| primaryScreenName = primaryScreen:name() | |
| print("Primary screen name: " .. primaryScreenName) | |
| if primaryScreenName == "Sidecar Display (AirPlay)" then | |
| print("AVP connected.") | |
| hs.caffeinate.set("displayIdle", true, true) | |
| hs.caffeinate.set("systemIdle", true, true) | |
| hs.caffeinate.set("system", true, true) | |
| else | |
| print("AVP disconnected.") | |
| hs.caffeinate.set("displayIdle", false, true) | |
| hs.caffeinate.set("systemIdle", false, true) | |
| hs.caffeinate.set("system", false, true) | |
| hs.audiodevice.defaultOutputDevice():setMuted(true) | |
| hs.caffeinate.lockScreen() | |
| end | |
| end | |
| screenWatcher = hs.screen.watcher.new(screenChangedCallback):start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment