Controlling Little Snitch via AppleScript

This is perhaps a rather niche problem, but when I faced it I could not find any good solution out there, so I wrote one. Hopefully others who may have a similar need may find this useful.

Little Snitch (LS) is a macOS firewall application. It is not inherently scriptable. Version 5+ does have a CLI which supports some remote commands, but it requires macOS 11 (Big Sur), and I’m stuck on Catalina until the M1 iMacs come out. Consequently, this solution is targeted at LS v4.6 (although it should still work for v5).

The problem is this: I use LS to block all inbound connection attempts to httpd, sshd, and screen sharing on my home computer. But when I’m on the road I want to temporarily disable LS while I connect to one or more of those services from my laptop. The following script does that. It controls a single checkbox in LS, so run it once to disable, run it again to enable. There are a number of ways to trigger it (think carefully, or else you’re opening a rather large security hole), including mail rules, or ssh from a known secure host (that you’ve configured LS to allow).

tell application "Little Snitch Configuration" to activate

tell application "System Events"
	-- UI item names/references deduced with help from Apple's UIElementInspector app
	repeat until (exists process "Little Snitch Configuration")
		delay 1
	end repeat
	tell process "Little Snitch Configuration"
		set frontmost to true
		-- menu 2 is "Little Snitch Configuration" and I guess menu bar 1 is its home
		repeat until (exists menu item "Preferences…" of menu 2 of menu bar 1)
			delay 1
		end repeat
		click menu item "Preferences…" of menu 2 of menu bar 1
		repeat until (exists window "Preferences")
			delay 1
		end repeat
		tell window "Preferences"
			-- The Preferences window has multiple panes, which are known as "toolbars".
			-- Toolbar 1 is the "General" pane. You'd think "General" would be the name
			-- of the toolbar, but no. It has no name, just an index.
			click button "General" of toolbar 1
			-- Again, no way to deduce this reference without the UIElementInspector. 
			-- Neither item is named, only typed and indexed. No idea what a 'group' is.
			click checkbox 1 of group 1
		end tell
	end tell
end tell