Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

tatchley

macrumors newbie
Original poster
Oct 17, 2012
4
0
Hello all,

At this point, I am frustrated by my lack of programing knowledge. With one click, I would like to be able to start/close a specific application and automatically enable/disable the system's SOCKS Proxy. I know it can be done, and I have googled with various results, but nothing that would suit my needs. So that is why I am here: to ask if there are any of your who would be willing to, out of the goodness of your hearts, write me a script that could do this. I have nothing to offer you in return. I'm sure for someone with even notice experience, this would be a simple undertaking, but, alas, I do not even have that. Like I said, I would want the script to launch a certain application (I can write it in if I know the syntax) and to switch the SOCKS proxy to on. Also, I would like the script to close the application and turn off the proxy when I click it again. Would any of you help me? Thanks for reading this far.
 

blaster_boy

macrumors 6502
Jan 31, 2004
282
4
Belgium
You need to supply a few more details :
- script for mac ?
- for windows ?
- what application exactly do you need to open and close ?

More details helps !
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
Something to get you started :

Code:
(*
The TextEdit application is used as a general example
 Replace "com.apple.TextEdit" with the bundle identifier from the actual application you would like to start/stop
 The example is the bundle identifier from the TextEdit application
*)
tell application "System Events"
	set isRunning to (count of (every process whose bundle identifier is "com.apple.TextEdit")) > 0
end tell

-- Check if application is running
if isRunning then
	-- Close the application
	tell application "TextEdit" to quit
	(*
	Disable the system's SOCKS Proxy
	Take a look at the networksetup command.
	Open Terminal application an type networksetup for more information
	Don't know if this is the setting you want :
	networksetup -setsocksfirewallproxy Ethernet 127.0.0.1 1080 off
	-setsocksfirewallproxy – tells networksetup to turn the proxy on, with the following settings
	 Ethernet – the identifier of the network service to change the settings for (e.g. AirPort, Ethernet). Use networksetup -listallnetworkservices to see all valid values.
	127.0.0.1 – the address of the SOCKS proxy.
	1080 – the port of the SOCKS proxy.
	 off – this is for authentication
	 You can just as simply disable the proxy.
	 networksetup -setsocksfirewallproxystate Ethernet off
	 Again, substitute Ethernet for your network service name if necessary (probably either AirPort or Ethernet).
	 It is also possible, once the settings are remembered by Mac OS X, to just use:
	 networksetup -setsocksfirewallproxystate Ethernet on
*)
	do shell script "networksetup -setsocksfirewallproxystate Ethernet off" --with administrator privileges user name password
else
	-- Enable the system's SOCKS Proxy
	do shell script "networksetup -setsocksfirewallproxystate Ethernet on" --with administrator privileges user name password
	-- Activate the application
	tell application "TextEdit" to activate
end if

Or with a simple dialog :

Code:
display dialog "Start/stop application and enable/disable the system's SOCKS Proxy." buttons {"Cancel", "Stop", "Start"} default button 3
if the button returned of the result is "Start" then
	do shell script "networksetup -setsocksfirewallproxystate Ethernet on" --with administrator privileges user name password
	tell application "TextEdit" to activate
else
	tell application "TextEdit" to quit
	do shell script "networksetup -setsocksfirewallproxystate Ethernet off" --with administrator privileges user name password
end if


To get the bundle identifier for your application :

Code:
-- Replace TextEdit with the right application
get bundle identifier of (info for (path to application "TextEdit"))

Info : https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man8/networksetup.8.html
 
Last edited:

tatchley

macrumors newbie
Original poster
Oct 17, 2012
4
0
Wow...

That was an awesome reply kryton2. Thanks a bundle. The shorter script works great. I have two more questions though: 1. Could you tweak the script to automatically detect whether the application is open or not and act accordingly? I would like minimize on the number of clicks. Less clicks = more :). 2. Could you tweak the script to temporarily assume administrative privileges when running? I have a rather long password, and it would be painful to have to type it out each time. As I said before, less clicks = more :).

If you cannot comply, do not worry about it. How can I complain about a free product? :)
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
Regarding your questions.

1) Try the first script
2) Remove the dashes from the do shell script lines eg :

Code:
do shell script "networksetup -setsocksfirewallproxystate Ethernet off" with administrator privileges user name "yourusername" password "yourpassword"
 

tatchley

macrumors newbie
Original poster
Oct 17, 2012
4
0
Splendid. Thank you for your kindness. One last thing: How can I set the permissions of the app so that it requires administrative credentials to view? I would not like just anyone to be able to view the script, and more importantly my login information.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.