AppleScript to quickly toggle Mac settings for use with iProxy/NetShare/Flashlight

Discussion in 'iPhone' started by BeyondtheTech, Dec 1, 2010.

  1. BeyondtheTech, Dec 1, 2010
    Last edited: Dec 2, 2010

    BeyondtheTech macrumors 68020

    BeyondtheTech

    Joined:
    Jun 20, 2007
    #1
    Provided in BBcode for your benefit and corrected code for non-Growl users.


    For those who are unable or unwilling to jailbreak their device to be able to tether their iPhone's internet connection for your Mac using apps such as iProxy (requires an adhoc build or dev license) or the pulled NetShare and Handy Flashlight apps on the App Store, I have further simplified the process, since the biggest pain is manually creating an ad-hoc connection and static IP setup every time just to get it started.

    If you've jailbroken and got yourself MyWi or PDAnet or simply enabled Internet Tethering, congratulations and good for you, and there's no need to bash this setup or the people who want to use it.

    This AppleScript I put together from various sources will simply toggle the Network Locations and create an ad-hoc network instantly for you.

    What you need:
    • an iPhone with one of the mentioned or similar tethering apps
    • a Mac
    • Growl (optional software for pop-up notifications)
    • Proxifier (optional software for tunneling all SOCKS requests)

    You should have some basic knowledge of how this "tethering" feature works and have been able to set it up from all the various written and video instructions already on the internet, with static IPs on your Mac and iPhone and all.

    But, if you haven't used Network Locations, it's a great way of separating your normal method of connecting to wireless networks, versus the slightly different setup that your proxy tethering is done. Having a separate Network Location can retain the static IP address needed for the proxy. Follow the clearly detailed instructions on iProxy's wiki here, as it applies to all the proxy tethering apps:

    https://github.com/tcurdt/iProxy/wiki/Configuring-iProxy

    Proxifier, as I mentioned above, is a nice shareware utility from Initex Software that allows you to tunnel all your applications through the proxy. Without the software, only your browser is able to natively go through the proxy. But with Proxifier or similar software, you can use Safari, Firefox, Mail, Twitterific, Skype, iChat, and a bunch of other apps, without having to manually configure each of them to go through the proxy.

    MacProxy is a similar product, but I am still testing it with unsatisfactory results (slow). ProxyCap and Authoxy (freeware?) are also similar products that I have not yet tested.

    Anyhow, fire up the AppleScript Editor, and copy and paste this code:

    Code:
    -- Toggle an ad-hoc network for iProxy/NetShare/Flashlight use
    -- compiled by Raphael Salgado aka BeyondtheTech, 2010
     
    -- modify the following lines as needed, NetworkPassword must be 10 digits.
    property NetworkName : "Johnny's MacBook"
    property NetworkPassword : "1234567890"
    property NormalNetworkLocation : "Normal"
    property ProxyNetworkLocation : "Proxy"
    property GrowlNotification : ""
    property NewNetworkLocation : ""
    property DisconnectNetworkName : "Disconnect from " & NetworkName
    property EnablingProxyMode : "Enabling ad-hoc and proxy for iPhone tethering"
    property DisablingProxyMode : "Disabling ad-hoc and proxy for normal network mode"
     
    -- get the current Network Location from System Preferences
    set CurrentNetworkLocation to do shell script "/usr/sbin/scselect 2>&1 | grep '^ ' 2>&1 | grep '*' | cut -f 2 -d '(' | cut -f 1 -d ')'"
     
    -- determine which mode, then switch accordingly
    if CurrentNetworkLocation = NormalNetworkLocation then
    	set GrowlNotification to EnablingProxyMode
    	set NewNetworkLocation to ProxyNetworkLocation
    else
    	set GrowlNotification to DisablingProxyMode
    	set NewNetworkLocation to NormalNetworkLocation
    end if
     
    -- is Growl installed?
    tell application "System Events"
    	set isGrowlRunning to (count of (every process whose name is "GrowlHelperApp")) > 0
    end tell
     
    -- only display notifications if Growl is running
    if isGrowlRunning = true then
    	tell application "GrowlHelperApp"
    		set the AllNotificationsList to {"Notice"}
    		set the enabledNotificationsList to {"Notice"}
    		register as application "Proxy Toggler" all notifications AllNotificationsList default notifications enabledNotificationsList icon of application "Script Editor"
    	               
    		-- send notification
    		notify with name "Notice" title "Proxy Mode" description GrowlNotification application name "Proxy Toggler"
    	end tell
    end if
     
    -- change the location
    do shell script "/usr/sbin/scselect " & NewNetworkLocation
     
    -- change AirPort settings accordingly
    if NewNetworkLocation is ProxyNetworkLocation then
    	-- create ad-hoc network
    	tell application "System Events"
    		tell process "SystemUIServer"
    			tell menu bar 1
    				-- find Airport Menu
    				set menu_extras to value of attribute "AXDescription" of menu bar items
    				repeat with the_menu from 1 to the count of menu_extras
    					if item the_menu of menu_extras is "Airport Menu Extra" then exit repeat
    				end repeat
    			               
    				-- turn on Airport then create ad-hoc network
    				tell menu bar item the_menu
    					perform action "AXPress"
    					-- if Airport is off, turn it on
    					if title of menu item 2 of menu 1 is "Turn Airport On" then
    						perform action "AXPress" of menu item "Turn Airport On" of menu 1
    						perform action "AXPress"
    					end if
     
    					-- the following string contains an ellipsis, not three dots!
    					perform action "AXPress" of menu item "Create Network…" of menu 1
    				end tell
    			end tell
    		               
    			-- Enter information into Create Network dialog box
    			tell window 1
    				-- require password
    				click checkbox 1
    			               
    				-- set to 40-bit WEP
    				click pop up button 2
    				click menu item 1 of menu 1 of pop up button 2
    			               
    				-- fill in information
    				set value of text field 2 to NetworkPassword
    				set value of text field 3 to NetworkPassword
    			               
    				-- set name last so OK button will be enabled (password field loses focus)
    				set value of text field 1 to NetworkName
    			               
    				-- click OK
    				click button 1
    			end tell
    		end tell
    	end tell
                   
    	-- launch proxy tunneling software
    	tell application "Proxifier" to activate
                   
    else
                   
    	-- turn off ad-hoc network
    	tell application "System Events"
    		tell process "SystemUIServer"
    			tell menu bar 1
    				-- find Airport Menu
    				set menu_extras to value of attribute "AXDescription" of menu bar items
    				repeat with the_menu from 1 to the count of menu_extras
    					if item the_menu of menu_extras is "Airport Menu Extra" then exit repeat
    				end repeat
    			               
    				-- turn on Airport then create ad-hoc network
    				tell menu bar item the_menu
    					perform action "AXPress"
    					-- if Airport is off, turn it on
    					if title of menu item 2 of menu 1 is "Turn Airport On" then
    						perform action "AXPress" of menu item "Turn Airport On" of menu 1
    						perform action "AXPress"
    					end if
    					try
    						perform action "AXPress" of menu item DisconnectNetworkName of menu 1
    					end try
    				end tell
    			end tell
    		               
    		end tell
    	end tell
                   
    	-- quit proxy tunneling software
    	tell application "Proxifier" to quit
                   
    end if
    This script will assume you have already created at least two Network Locations, one called "Normal" and the other called "Proxy." The "Proxy" Location should have the static IP address needed for the ad-hoc network, and there's no need to set up a SOCKS route the Proxies tab if you're going to use Proxifier.

    Make any adjustments in the code, save it as an application called "Toggle Proxy Mode," copy it to your Applications folder, then throw an alias onto your Dock alongside Proxifier.

    When you launch this application, it will:
    1. Determine what Network Location you're in and change accordingly
    2. Pop up a notification using Growl (if available)
    3. Turn on AirPort if it's off
    4. If you're in normal mode, change Network Location to "Proxy" and create a WEP40-protected ad-hoc network, then launch Proxifier
    5. If you're in proxy mode, disconnect the ad-hoc network, switch the Network Location back to "Normal," then quit Proxifier

    All you need to do on your iPhone is make sure that it's authenticating to the ad-hoc connection on your Mac and has a static IP address with it, then fire up your favorite SOCKS proxy app, and you're up and running in no time.

    Hope this helps, thanks for reading.


    UPDATE: Corrected logic for users who don't have Growl installed, added comments.
     
  2. kelpi macrumors regular

    kelpi

    Joined:
    Sep 14, 2008
    #2
    This is great. Have handy flashlight. Will try it out and report back.
     

Share This Page