Skip to content

Instantly share code, notes, and snippets.

@JurajNyiri
Last active November 6, 2023 09:26
Show Gist options
  • Save JurajNyiri/df4bacd1ccfc00105843e67f09b2fc80 to your computer and use it in GitHub Desktop.
Save JurajNyiri/df4bacd1ccfc00105843e67f09b2fc80 to your computer and use it in GitHub Desktop.
UTM Pass USB Device via apple script
#---- MODIFY BELOW ----#
set application_name to "Home Assistant" # Set to your UTM VM Name
set button_name to "USB" #Description of the button, can be found by using Accessibility Inspector from Xcode, you probably want this set to "USB"
set usb_select to "Conbee" #Name of the device you wish to pass to VM
#---- MODIFY ABOVE ----#
#---- APPLICATION CODE, DO NOT TOUCH BELOW----#
set foundVM to false
repeat while foundVM = false
log "Looking for UTM..."
tell application "System Events"
repeat with theProcess in processes
if not background only of theProcess then
tell theProcess
set processName to name
set theWindows to windows
end tell
set windowsCount to count of theWindows
if processName is "UTM" then
if windowsCount is greater than 0 then
repeat with theWindow in theWindows
log "Looking for VM..."
set window_name to name of theWindow
if window_name is application_name then
set foundVM to true
log "VM found, trying to click the button..."
tell theProcess
set frontmost to true
repeat with theButton in button of toolbar 1 of theWindow
set button_description to description of theButton
if button_description is button_name then
log button_description
click theButton
keystroke usb_select
key code 36
end if
end repeat
end tell
end if
end repeat
end if
end if
end if
end repeat
end tell
end repeat
@Nystro0m
Copy link

Thank you so much for your script! It works perfectly. I added a:

tell application "Finder"
    set visible of process "UTM" to false
end tell

once the USB has been changed to hide the UTM application.

Also, for some reason, the command

open "utm://start?name=HAOS"

only opens UTM (not the VM) if it's not running OR start the VM if it's running. So I had to add UTM as a login item, and add a timer in the AppleScript to wait for it to start, then open and change the USB. Have you encountered the same issues before?

I had the same problem. And solved it in the same way.

@deadrubberboy
Copy link

Thank you for this! I used it with a twist. I had already setup the shortcut to call UTM URL to launch my Home Assistant VM. I edited that shortcut and added a "wait 30 secs" so the VM was up and then added "run apple script" and pasted your code in w/appropriate changes to start sharing the USB for my SkyConnect device. Works! Thank you @Nystro0m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment