I'm using AppleScript the first time, and what I want to do to control Tunnelblick with it. Try to connect to a specified VPN, but if it doesn't connect in 2 seconds, throw an error. To make the context complete, I'll run the script with nodeJS, wrap the AppleScript into a for loop, if it throws an error, just continue the nodeJS loop, rerunning the AppleScript with a different VPN, until it connects. Here's my code (JS):
const switchVpn = async () => {
for (;;) {
const config = getVpn();
try {
await runAS(`
tell application "Tunnelblick"
set i to 0
disconnect all
connect "${config}"
get state of first configuration where name = "${config}"
repeat until result = "CONNECTED"
if i > 2 then
error 'timeout'
exit repeat
end if
delay 1
get state of first configuration where name = "${config}"
set i to i + 1
end repeat
end tell
`);
break;
} catch (e) {
console.log(e);
}
}
};
But it throws error: '292:293: syntax error: Expected “given”, “with”, “without”, other parameter name, etc. but found unknown token. (-2741)'
What am I doing wrong? Or, how can it be done more efficiently?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…