Switching FoxyProxy Profiles using Script

FoxyProxy is a useful extension to Firefox, but I’m always having to switch profiles back and forth as I take my laptop from work to home, and vice versa. I experimented with proxy PAC files without any success (mainly because laptops have multiple network adapters and the PAC scripts only detect your first IP address). Plus, as far as I can tell, PAC files are checked for every single HTTP request which must be a hit on browser performance.

Anyway, what I decided to do was directly modify the FoxyProxy config file before Firefox was run, and this would then select which FoxyProxy profile was used. The script below detects the host of something on my local network (which doesn’t exist at work) and switches accordingly. You could get it to switch on anything you like, but this works for me.

runfirefox.vbs

Set WshShell = WScript.CreateObject("WScript.Shell")

' Code to determine where you are and switch profiles accordingly
if (Ping("somehost") = "201.69.34.132") then
	ModifyFoxyProxy("3402440320")
else 
	ModifyFoxyProxy("patterns")
end if

' Run Firefox - this uses a specific profile.. your command line can simply exclude it
ReturnCode  = WshShell.Run("""C:\Program Files\Mozilla Firefox\firefox.exe"" -profile ""z:/firefox/profiles/qtxjee58.default""", 6, False)



sub ModifyFoxyProxy(sProfile) 
	' Path to your foxyproxy.xml file
	xmlfile="Z:\Firefox\Profiles\qtxjee58.default\foxyproxy.xml"
	outfile="Z:\Firefox\Profiles\qtxjee58.default\foxyproxy.xml"
	set oparser=createobject("msxml2.domdocument")
	with oparser
	    .async=false
	    .validateOnParse=false
	    .resolveExternals=false
	    .load xmlfile
	end with
	    
	if oparser.parseerror.errorcode<>0 then
	    wscript.echo "xml file " & xmlfile & " is not well-formed." & vbcrlf & "Operation aborted."
	    wscript.quit 999
	end if
	
	set oroot=oparser.documentElement
	oroot.setAttribute "mode", sProfile
	oparser.save outfile
	    
	set oparser=nothing 

end sub


function Ping(strHost)

    dim objPing, objRetStatus

    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery ("select * from Win32_PingStatus where address = '" & strHost & "'")

    for each objRetStatus in objPing
        if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
    		Ping = ""
            'WScript.Echo "Status code is " & objRetStatus.StatusCode
        else
            'Ping = True
            'Wscript.Echo "Bytes = " & vbTab & objRetStatus.BufferSize
            'Wscript.Echo "Time (ms) = " & vbTab & objRetStatus.ResponseTime
            'Wscript.Echo "TTL (s) = " & vbTab & objRetStatus.ResponseTimeToLive
            Ping = objRetStatus.ProtocolAddress 
        end if
    next
    
end function 
Advertisements

USB Detect & Launch Version 1.5 Released

USB D&L USB Detect & Launch allows you to automatically execute a script or application each time a USB storage device is plugged into your PC. Different scripts can be executed for different devices and it’s the volume label that determines what action to perform.

Version 1.5 contains new functionality to monitor for any device being plugged in; useful when you have no control over what memory stick is being inserted, but need to fire off a script to perform a particular action.

e.g.

label:(any)
exec:”c:batchbatch-any.cmd” %1 %2

You can find the download on the USB Detect & Launch page.

File Mover 1.9 Released

File mover is useful little utility that will move files from one folder to another. This is useful when a program (or remote server) dumps files into a folder & you want to move them somewhere else. An added bonus with this utility is that it’ll (optionally) run a program each time a file is moved; this could be useful for firing off email notification.

Version 1.9 has been released and includes some small fixes and enhancements. E.g. copy from log now works, and the app doesn’t freeze when executing a script in ‘Wait for executed process’ mode.

You can find the download on the File Mover page.

File Mover Version 1.6 Released

This is a minor release and gives the application more resilience around folder availability, i.e. it can handle when a UNC drive isn’t available for copying from/to.

Click over to the software page for the download.

USB Detect & Launch Version 1.4 Released

USB D&L This new version includes the ability to detect when devices with no label are inserted. This is handy when you have a whole stack of new devices that you need to put data onto.

Here’s an example of the syntax you’d use:

label:(no label)
exec:”c:\batch\batch-no-label.cmd” %1 %2

Click over to the software page for the download.