Script to quit automatically if VPN fails

Discussion of Transmission that doesn't fit in the other categories
Post Reply
apapitrat
Posts: 2
Joined: Tue Mar 10, 2009 2:38 pm

Script to quit automatically if VPN fails

Post by apapitrat »

Hello,

I use Transmission with Relakks as my VPN... I was wondering if anyone here had written a script / app that quits Transmission when the VPN fails ?

Thanks in advance,

Adrien.
emah
Posts: 1
Joined: Thu Sep 24, 2009 8:19 pm

Re: Script to quit automatically if VPN fails

Post by emah »

Hi,

I don't know if your are still looking for an answer because you posted this a few month ago…

I was looking for the same thing yesterday and i found a script to do that.

Go to Application->Utilities->AppleScript Editor and copy/paste this script :

Code: Select all

on idle
	tell application "System Events"
		tell current location of network preferences
			set myConnection to the service "YourVPNname"
			if current configuration of myConnection is not connected then
				tell application "Transmission"
					quit
				end tell
			end if
		end tell
		return 30
	end tell
end idle
Replace "YourVPNname" by your VPN service name.

Go to File->Save as and choose "Application" for the File Format and check "Stay Open" option.

Run your vpn, Transmission and the script.
And that's it, Transmission will quit when your VPN connection is lost.

It's working perfectly for me.

I am looking for a solution to stop or pause all my Transmission transfers instead of quitting Transmission... if someone have a clue !
Mogwai82
Posts: 4
Joined: Fri Jan 22, 2010 2:28 am

Re: Script to quit automatically if VPN fails

Post by Mogwai82 »

Thanks Emah. Looks like it's working here. Any news on stopping or pausing T if the VPN drops?
Longinus00
Posts: 137
Joined: Fri Aug 21, 2009 5:46 am

Re: Script to quit automatically if VPN fails

Post by Longinus00 »

If you have transmission-remote installed you could just fire this off:

Code: Select all

transmission-remote -tall --stop
You'll have to include the host:port and user:password if you have them non default of course.
plasticexist3nce
Posts: 5
Joined: Sun Apr 20, 2008 5:50 pm

Re: Script to quit automatically if VPN fails

Post by plasticexist3nce »

this is what i think ppl are looking for, but I can't seem to get mine to work (ie. no results)

Code: Select all

on idle
	tell application "System Events"
		tell current location of network preferences
			set myConnection to the service "VPN (PPTP)"
			if current configuration of myConnection is connected then
				activate application "Transmission"
				delay 15
				tell process "Transmission"
					click menu item "Resume All" of menu 1 of menu bar item "Transfers" of menu bar 1
				end tell
			else if current configuration of myConnection is not connected then
				connect myConnection
				if exists (activate application "Transmission") then
					tell process "Transmission"
						click menu item "Pause All" of menu 1 of menu bar item "Transfers" of menu bar 1
					end tell
				end if
			end if
		end tell
	end tell
	delay 33
	
end idle
daff
Posts: 2
Joined: Mon Oct 18, 2010 12:18 pm

Re: Script to quit automatically if VPN fails

Post by daff »

Hi
I made a lot of research on this and here is what works for me.
since we can't know if Transmission is paused or not with applescript, we have to quit and restart the app according to vpn status

Code: Select all

on idle
	tell application "System Events"
		tell current location of network preferences
			
			set vpnConnection to the service "YourVPNname"
			-- Check if Transmission is running
			tell application "System Events"
				set transmissionIsRunning to (count of (every process whose name is "Transmission")) > 0
			end tell
			
			-- Check if VPN is down
			if current configuration of vpnConnection is not connected then
				if (transmissionIsRunning = true) then
					tell application "Transmission" to quit
				end if
				connect vpnConnection
			-- Check if VPN is up
			else if current configuration of vpnConnection is connected then
				if (transmissionIsRunning = false) then
					tell application "Transmission" to activate
				end if
			end if
			
		end tell
		return 10
	end tell
end idle
Replace "YourVPNname" by your VPN service name.
Go to File->Save as and choose "Application" for the File Format and check "Stay Open" option.

You just have to launch this script, to start automatically the VPN and when it is connected, then Transmission starts 10 seconds later.
If VPN fails, Transmission quits and the script will keep trying to reconnect to VPN every 10 seconds, and then will restart Transmission when VPN is up.

To hide the script from the Dock, add an entry in info.plist :
[KEY]NSUIElement[/KEY]
[STRING]1[/STRING]

To quit the script easily, create another one :

Code: Select all

tell application "System Events"
	if ((count of (every process whose name is "YourScriptName")) > 0) then
		tell application "YourScriptName" to quit
	end if
end tell
Replace "YourScriptName" by your script name created before.
Go to File->Save as and choose "Application" for the File Format with no options.

This will only quit the script.
If you also want to quit Transmission and disconnect vpn :

Code: Select all

tell application "System Events"
	if ((count of (every process whose name is "YourScriptName")) > 0) then
		tell application "YourScriptName" to quit
	end if
	if ((count of (every process whose name is "Transmission")) > 0) then
		tell application "Transmission" to quit
	end if
	tell current location of network preferences
		set vpnConnection to the service "YourVPNname"
		if current configuration of vpnConnection is connected then
			disconnect vpnConnection
		end if
	end tell
end tell
Replace "YourScriptName" by your script name created before and "YourVPNname" by your VPN service name.
Go to File->Save as and choose "Application" for the File Format with no options.

You can also put the scripts in /Users/YourUserFolder/Library/Scripts, like this they are available from status bar within the applescript menu

i hope this helps
I would prefer to just pause transfers, but by quiting it also avoid exchanges with trackers when vpn is down
tomi.o
Posts: 1
Joined: Sat Oct 30, 2010 3:22 pm

Re: Script to quit automatically if VPN fails

Post by tomi.o »

Hi Guys,

used the script and it works on startup, but somehow when VPN disconnects, Transmit doesn't shut down as planned. Issue is that there is a window ("Do you really want to quit..."). After clicking OK, it restarts VPN and restarts Transmit as it should. How can I disable the message or make the script to "click OK" for me?

Thanks for a hint,
Tomi
daff
Posts: 2
Joined: Mon Oct 18, 2010 12:18 pm

Re: Script to quit automatically if VPN fails

Post by daff »

I know it's possible to make it by script, but i can'T remember how. So the easier way is to disable the prompt in Transmission preferences, "general" tab, uncheck "Prompt user for : Quit with active transfers"
smackjack
Posts: 2
Joined: Wed Nov 18, 2009 9:18 pm

Re: Script to quit automatically if VPN fails

Post by smackjack »

Is it possible to automatically connect to vpn when you launch transmission to?
scooterbaga
Posts: 8
Joined: Wed Aug 20, 2008 1:27 am

Re: Script to quit automatically if VPN fails

Post by scooterbaga »

daff wrote:To hide the script from the Dock, add an entry in info.plist :
[KEY]NSUIElement[/KEY]
[STRING]1[/STRING]

Sorry to bump an old thread... However, the solution here is excellent, although a little vague. Can someone tell me where exactly the above addition should go in the script app's info.plist? I've tried just sticking it at the end and just above the </plist> tag. One breaks the app, one does nothing.

It also appears as if the code should be formatted as followed:

<KEY>NSUIElement</KEY>
<STRING>1</STRING>

It doesn't seem to work regardless of formatting or placement. I'd really like to hide the icon.

Edit: I just used Dock Dodger. (It also looks like the tags need to go in the first <dict> segment.)

http://foggynoggin.com/dockdodger
yogicabump
Posts: 2
Joined: Tue Jun 03, 2014 10:56 am

Re: Script to quit automatically if VPN fails

Post by yogicabump »

Hey,

This is what I'm looking for, but I don't want to deal with editing scripts myself, I think this should be part of the program.
Does anyone have a finished script that is working well as of 2014 and wants to share?
Thanks
RailRanger
Posts: 1
Joined: Mon Aug 04, 2014 7:12 pm

Re: Script to quit automatically if VPN fails

Post by RailRanger »

If anyone else has this issue, I sort of made a problem to work around it. It's only made for Windows though if you are using transmission-qt. It could easily be modified for mac/linux though. The reason it isn't is because I used taskill and it would be slightly different on linux/mac.

http://demthruz.wordpress.com/2014/08/0 ... p-address/
Post Reply