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
@JurajNyiri
Copy link
Author

JurajNyiri commented Dec 13, 2022

How to run:

  1. Open Automator
  2. Create new application
  3. Choose "Run AppleScript"
  4. Copy & paste the code above
  5. Change application_name to your VM name
  6. Change usb_select to the prefix (or exact match) of your USB device, for example if you are using ConBee II, you can put in conbee or con as long as you do not have other device like connected usb device, it basically just writes the text and hits enter after context menu is shown.
  7. Save
  8. Add your new application into System Settings > Privacy & Security > Accessibility
  9. Launch it, allow control of System Events.app

@Nystro0m
Copy link

Nystro0m commented Jan 15, 2023

Hi!
First of all, thanks for the script!  I have an problem loading a USB bluetooth device on boot in my UTM instance of HASS.
I get the script to work all fine if i start UTM first, wait untill start, but when i add it to my login items and run it on boot i get this error message:
"The Action to run applescript ecountered an error can't get item 32 of every process"
Any idea?

I also have an Automator app for running my UTM Hass VM on boot. 

So i guess maybe the script run before UTM har started fully.

What do you think?

Best regards,
Måns

This was solved by adding a 30 second delay in the beginning of the script "delay 30"

@timothe
Copy link

timothe commented Jan 24, 2023

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?

@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