Script to change proxy settings

Discussion in 'Mac Programming' started by tatchley, Oct 18, 2012.

  1. macrumors newbie

    Joined:
    Oct 17, 2012
    #1
    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.
     
  2. macrumors 6502

    blaster_boy

    Joined:
    Jan 31, 2004
    Location:
    Belgium
    #2
    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 !
     
  3. kryten2, Oct 19, 2012
    Last edited: Oct 19, 2012

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #3
    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
     
  4. thread starter macrumors newbie

    Joined:
    Oct 17, 2012
    #4
    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? :)
     
  5. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #5
    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"
     
  6. thread starter macrumors newbie

    Joined:
    Oct 17, 2012
    #6
    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.
     

Share This Page