Button with dynamic logo

Hey,

Currently I have an applescript running in my touchbar which tells me whether I am connect to my VPN or not. It is green when I am connected and red with a warning sign when I am not connected. It is as simple as telling the button to run a terminal command to verify this. However, pressing this 'button' does not do anything. By adding an action to this in bettertouchtool I want to be able to use the button as a toggle for my vpn. Hence, if the button is green (vpn is connected) I want to be able to press it such that it will disconnect (and become red) and vice versa. However, the current configuration does not work as the code is constantly running. I need some kind of line which tells the program 'wait for button press' or something. Can anyone help me out?

This is my current code:

set activeInterfaces to do shell script "ifconfig"
if activeInterfaces contains "netmask 0xffffffff" then
scutil --nc stop "NordVPN IKE"
else
scutil --nc start "NordVPN IKE"
end if

Anyone who can help me out?

Are you still working on this? I'm gonna try tackling this in the next few days.

Yes I am still working on this. Or, at least I tried some things but I did not succeed yet. But I am not very experienced in Apple Script. So that's why I am wondering whether someone could help me out with this.

Any input is welcome.

Add an action to the button to run a non-asynchronous script. In the script check the current state of the connection and perform the correct action. Add an additional action to refresh the button.

Refer to this thread which got it working for me. I'm not a coder and can manage playing around with applescript and such but mostly through trial and error. It seems because nordvpn is ike2 (no idea, different from most standard vpn types?) scutil does not work to enable/disable or even check the vpn's status.

I messed around and shortened the code a little...works fine so far. As Northeastpaw mentioned, you're checking the state of vpn connection and enabling it if it's off and disabling if it's on. And then an additional action to refresh the button so you won't have to wait the 5 seconds or whatever number of seconds you set to rerun the script over and over. I set it to wait 0.35 seconds as it takes about 0.25 to 0.3 seconds for nordvpn to switch states.

Screens attached on what I did. Let me know if you need another noob to figure it out with you lol

And...you will need to install vpnutil to get this to work. Link from comment on other thread. Install instructions in text file in zip.

Hey,

Thanks for explaining. I will try to adapt it this weekend. Just one question: why would I install vpnutil? I mean, you can also use scutil for starting or stopping vpn connections right?

Scutil doesn't work for ikev2 which nord is. That's the only reason... I'm still refining my widget and could share if interested when I'm done. Not a coder so it's taking time.

Problem was that when you trigger it to turn vpn off, it just reconnects right after. Found that you can turn off on demand vpn to prevent this, but now vpn is off whenever you close the lid or restart the computer or disconnect from wifi. I fixed most of this so far but results aren't 100% consistent. Still messing with it every day though.

Install vpnutil and try this.

It will check if you have internet. If yes, it will check if connected to VPN. If yes, it will try 12 times to connect until successful. Once VPN established, it will not try to connect anymore...so you can tap the icon to turn on and off without it trying to do its own thing. But whenever you restart BTT or close the lid, it'll attempt to connect to NordVPN 12 times.

Icons reflect status. Red = no vpn, blue = connected. NordVPN downloaded from app store...the version downloaded from nord's own site: the "IKE" has to be removed from the "NordVPN IKE" in the code.

NordVPN.bttpreset (44.8 KB)