Get connection state by connection name (AppleScript)

Got a problem with Viscosity or need help? Ask here!

aktariel

Posts: 1
Joined: Tue Nov 17, 2020 2:01 pm

Post by aktariel » Tue Nov 17, 2020 3:16 pm
this works:
Code: Select all
tell application "Viscosity"
	set connectionName to "PIA-USWest"
	set connectionState to state of first connection
	if connectionState is "Connected" then
		disconnect connectionName
	else
		connect connectionName
	end if
end tell
this does not ("Script Error: Can’t get state of "PIA-USWest"."):
Code: Select all
	set connectionName to "PIA-USWest"
	set connectionState to state of connectionName
nor does this ("Script Error: Viscosity got an error: Invalid key form."):
Code: Select all
	set connectionName to "PIA-USWest"
	set connectionState to state of connection connectionName
More frustratingly, "the first connection" is actually "PIA-USChicago"; I have no idea why, since it's nowhere near the top of the list. Are there any plans or any way to get the status of a connection by name? This is driving me a little nuts. adding + removing VPN connections and reordering the list results in "the first connection" not being what I would expect at all, and since the status of a connection can't be checked by name, I end up having to write all kinds of crazy nested if/else compare loops.

Am I missing something, or can the status of a connection really not be checked by name, even in 1.9?

James

User avatar
Posts: 2313
Joined: Thu Sep 04, 2008 9:27 pm

Post by James » Tue Nov 17, 2020 6:07 pm
Hi aktariel,

As a rule we don't offer support for user-developed AppleScripts (as the time requirements can quickly get out of hand), but as this is a straightforward one:
Code: Select all
tell application "Viscosity"
	get state of first connection whose name is "My Connection"
end tell

Replace My Connection with your connection's name. Happy scripting! :)

Cheers,
James
Web: https://www.sparklabs.com
Support: https://www.sparklabs.com/support
Twitter: https://twitter.com/sparklabs
2 posts Page 1 of 1