Skip to content

Instantly share code, notes, and snippets.

@iamkirkbater
Last active December 8, 2023 22:07
Show Gist options
  • Save iamkirkbater/ba6278d0ac6d695cb8c6c5fc309ba210 to your computer and use it in GitHub Desktop.
Save iamkirkbater/ba6278d0ac6d695cb8c6c5fc309ba210 to your computer and use it in GitHub Desktop.
Launches OBS, starts the virtual camera, and then minimizes the window.
on run {input, parameters} -- remove this line if running in Script Editor and not with Automator
tell application "OBS" to activate
tell application "System Events"
tell process "OBS"
set frontmost to true
if name of every menu item of menu "Tools" of menu bar 1 contains "Start Virtual Camera" then
click menu item "Start Virtual Camera" of menu "Tools" of menu bar 1
end if
click (first button of window 1 whose role description is "minimize button")
end tell
end tell
end run -- remove this line if running in Script Editor and not with Automator
@absolutecad
Copy link

Brilliant, now can use OBS for my clients to use with Zoom to present to clients with different scenes, thank you

@SagesOfRPG
Copy link

Hello, I copied the script into script editor on my Mac.

I get this error when I run it.

Any help or ideas?

error "" number -1721

@iamkirkbater
Copy link
Author

@SagesOfRPG did you try to copy from this screen or did you try to copy/paste from the "Raw" version of the file, or use the Copy button?

The only thing I can think of is that maybe there was some weird formatting character that got copied from that?

What happens if you click on "Raw" and then save the file and open it in Script Editor?

@SagesOfRPG
Copy link

SagesOfRPG commented Nov 30, 2020

@iamkirkbater Figured it out. In the script editor, I had to remove the following lines

on run {input, parameters}
and
end run

Those are needed for Automator to run the AppleScript within Automator, but not when running via script editor.

Thanks for the quick response.

@iamkirkbater
Copy link
Author

Ahh, nice. Glad you were able to figure it out! I'll make a comment in the original gist for others!

@random-developer
Copy link

random-developer commented Dec 18, 2020

Now that OBS has the virtual camera pre-installed, no plugin is necessary for this and nothing appears in the Tools menu to start it. Rather, the Controls buttons includes a Start Virtual Camera button that must be clicked.

I really don't know anything about AppleScript, but I tried adding:
click (first checkbox of window 1 whose title is "Start Virtual Camera")

My result was that the button became highlighted as if the mouse was hovered over it, however the virtual camera was not activated (and the text on the button was not changed to "Stop Virtual Camera".

@ChoPraTs
Copy link

I've the same problem!

I would like to continue using this script but adapted to the new version of OBS. As random-developer said, the button to start the virtual camera now is in another site, not in the Tools menu.

How can we edit the script?

Thank you!

@iamkirkbater
Copy link
Author

Honestly I'm not sure with the new layout if this will be possible to do via Applescript. OBS has pretty lacking support for Applescript, the only reason I was able to do this end-around way was because it was in the menu which Apple itself has support for. I don't have OBS installed anymore, but I can see what I can do to see if I can reproduce and find something to fix this.

@ChoPraTs
Copy link

ChoPraTs commented Jan 15, 2021

Oh! I see... I've tried something but doesn't work for now.

I thought maybe it should be possible to do something similar setting a keyboard shortcut to start the virtual camera and then, change the script to recreate that keyboard shortcut instead of the menu click... But once it's saved as an App and I try to launch it, macOS advertises me that the app doesn't have access to the keyboard... And I've been looking in preferences / security if I should grant some privileges to the Script to access the keyboard, but I don't know where to do this or if it is possible. I have already given access to the script to control the system, but it seems that it is not enough.

Edit: It works! I just needed to delete the script/app from Preferences/Security/Privacy/Accessibility and add it there again. Done!

@RayNichol
Copy link

The 'Start Virtual Camera' command does not appear on my Mac, OBS version 26.1.2

I gave to start Virtual Camera from the 'Controls' tab.

So this does not work, any ideas?

Thanks

@OBSer1
Copy link

OBSer1 commented Mar 29, 2021

I got it to work in the script! Im new to AppleScript and this is my first time on github so sorry if my lingo isn't worded right.

you have to set a hotkey to start virtual camera in OBS to F12.
then add the following to your code:
key down 63 -- fn
key code 111 -- f12

Also, I have made mine to do the following:
Open OBS
Start Virtual Camera
Open Multiview Windowed
Minimize main window
Open Zoom

Heres my full script for Script Editor:

tell application "OBS" to activate
tell application "System Events"
tell process "OBS"
set frontmost to true
key down 63 -- fn
key code 111 -- f12
if name of every menu item of menu "View" of menu bar 1 contains "Multiview (Windowed)" then
click menu item "Multiview (Windowed)" of menu "View" of menu bar 1
end if
click (first button of window 2 whose role description is "minimize button")
end tell
tell application "zoom.us" to activate
end tell

@RayNichol
Copy link

RayNichol commented Mar 29, 2021 via email

@iamkirkbater
Copy link
Author

@RayNichol - Since the script is now adding new permissions (keystrokes instead of menu clicks) you'll probably need to add the appropriate permissions in your Security Preferences. I'm not sure off the top of my head what that would be but it's probably something like keyboard control or something along those lines.

@RayNichol
Copy link

RayNichol commented Mar 29, 2021 via email

@OBSer1
Copy link

OBSer1 commented Mar 29, 2021

Hi Kirk, It was strange as it did work the first time I run it with 'keystroke' but then didn't. Have just started doing a re-install of system anyway as I was having some other glitches.

I had the same issue. It’s the one under accessibility.
note ChoPraTs comment:
Edit: It works! I just needed to delete the script/app from Preferences/Security/Privacy/Accessibility and add it there again. Done!

@RayNichol
Copy link

RayNichol commented Mar 30, 2021 via email

@lucky13820
Copy link

Great, thanks, the hotkey solution works.

@slln
Copy link

slln commented Sep 19, 2021

I got it to work in the script! Im new to AppleScript and this is my first time on github so sorry if my lingo isn't worded right.

you have to set a hotkey to start virtual camera in OBS to F12.
then add the following to your code:
key down 63 -- fn
key code 111 -- f12

Also, I have made mine to do the following:
Open OBS
Start Virtual Camera
Open Multiview Windowed
Minimize main window
Open Zoom

Heres my full script for Script Editor:

tell application "OBS" to activate
tell application "System Events"
tell process "OBS"
set frontmost to true
key down 63 -- fn
key code 111 -- f12
if name of every menu item of menu "View" of menu bar 1 contains "Multiview (Windowed)" then
click menu item "Multiview (Windowed)" of menu "View" of menu bar 1
end if
click (first button of window 2 whose role description is "minimize button")
end tell
tell application "zoom.us" to activate
end tell

Thank you!
For those that want the very basic open, start vitural cam, then minimize and have, like me, little knowledge I managed to adapt the script above to the bare minimum need:

(remember 1st to activate F12 in settings>shortcuts of OBS)

tell application "OBS" to activate
tell application "System Events"
tell process "OBS"
set frontmost to true
key down 63 -- fn
key code 111 -- f12
click (first button of window 1 whose role description is "minimize button")
end tell
end tell

@RayNichol
Copy link

RayNichol commented Sep 23, 2021 via email

@charlespwd
Copy link

charlespwd commented Mar 30, 2022

Here's a properly formatted option that is sligthly more minimalistic. Like mentioned above, for this to work in the latest version of OBS, you can make it work by setting a keyboard shortcut for "Start Virtual Camera".

Mine is F5.

I use Google meet, so at the end I switch focus to "Google Chrome" instead of "zoom.us". Same same.

Start virtual cam:

tell application "OBS" to activate
tell application "System Events"
	tell process "OBS"
		set frontmost to true
		key down 63 -- fn
		key code 96 -- f5
		set visible to false
	end tell
end tell
tell application "Google Chrome" to activate

Stop virtual cam & quit:

tell application "System Events"
	tell process "OBS"
		set visible to true
		set frontmost to true
		key down 63 -- fn
		key code 96 -- f5
	end tell
end tell
tell application "OBS" to quit
tell application "Google Chrome" to activate

@bramses
Copy link

bramses commented Aug 27, 2022

^ Using this method locked the Option key on my Mac -- required the virtual keyboard to get unstuck

Screenshot 2022-08-27 17-15-43

Try this method instead: https://stackoverflow.com/questions/3690167/how-can-one-invoke-a-keyboard-shortcut-from-within-an-applescript

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