Jump to content
Sign in to follow this  
KoolAutoIT

Define your Custom Browser

Recommended Posts

KoolAutoIT

What I am trying to do with this is to be able to define a default browser, which could be portable. Here is the script :

;========Version=========
;.003
;2009-10-13
;========License=========
;SelectBrowser: Set the default browser on a Windows computer
;Copyright (C) 2009 Jeff Schroeder
;
;This program is free software: you can redistribute it and/or modify
;it under the terms of the GNU General Public License as published by
;the Free Software Foundation, either version 3 of the License, or
;(at your option) any later version.
;
;This program is distributed in the hope that it will be useful,
;but WITHOUT ANY WARRANTY; without even the implied warranty of
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;GNU General Public License for more details.
;
;You should have received a copy of the GNU General Public License
;along with this program.  If not, see <http://www.gnu.org/licenses/>.
;=========================

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#NoTrayIcon
Opt("GUIOnEventMode", 1)

buildMain()

While 1
  Sleep(1000)
WEnd

Func buildMain()
    global $browserarray = IniReadSectionNames(@ScriptDir & '\SelectBrowser.ini')
    if $browserarray == 1 then
        ;msgbox (0,"","No Browsers")
        $numbrowsers = 0
    else
        $numbrowsers = $browserarray[0]
    endif
    global $mainwindow = GuiCreate("SelectBrowser",215,$numbrowsers*20+60)
    GUISetOnEvent($GUI_EVENT_CLOSE, "closeWindow")
    GuiSetIcon(@ScriptDir & "\Internet-web-browser.ico")
    global $classes[3] = ["http","https","ftp"]
    global $count = 0
    if $numbrowsers > 0 then
        for $browser in $browserarray
            if $count > 0 then
                global $command = IniRead(@ScriptDir & '\SelectBrowser.ini',$browser,'BrowserCommand','')
                global $iconpath = IniRead(@ScriptDir & '\SelectBrowser.ini',$browser,'IconPath','')
                global $iconnumber = IniRead(@ScriptDir & '\SelectBrowser.ini',$browser,'IconNumber','')
                global $relativepath = IniRead(@ScriptDir & '\SelectBrowser.ini',$browser,'RelativePath','')
                $radioLabel = $browser
                Assign("browser" & $count,GuiCtrlCreateRadio($radioLabel, 30, ($count-1)*20+5, 220),2)
                if $command == RegRead('HKCU\Software\Classes\http\shell\open\command','') then
                    GuiCtrlSetState(-1, $GUI_CHECKED)
                endif
                GuiCtrlCreateIcon(relativeToAbsolute($iconpath), Number($iconnumber), 10, ($count-1)*20+8, 16, 16)
            endif
            $count = $count + 1
        next
    endif
    ;=====New=====
    global $newbutton = GuiCtrlCreateButton('New',5,$numbrowsers*20+10,65,20)
    GUICtrlSetOnEvent($newbutton,"newBrowser")
    ;=====Edit=====
    global $editbutton = GuiCtrlCreateButton('Edit',75,$numbrowsers*20+10,65,20)
    GUICtrlSetOnEvent($editbutton,"editBrowser")
    ;=====Delete=====
    global $deletebutton = GuiCtrlCreateButton('Delete',145,$numbrowsers*20+10,65,20)
    GUICtrlSetOnEvent($deletebutton,"deleteBrowser")
    ;=====Set as Default=====
    global $gobutton = GuiCtrlCreateButton('Set as Default',5,$numbrowsers*20+35,100,20)
    GUICtrlSetOnEvent($gobutton,"setDefault")
    ;=====Restore Backup=====
    global $restorebutton = GuiCtrlCreateButton('Restore Backup',110,$numbrowsers*20+35,100,20)
    GUICtrlSetOnEvent($restorebutton,"restoreBackup")
    if NOT FileExists(@ScriptDir & '\' & @ComputerName & '-StartMenuInternet.reg') then
        GUICtrlSetState($restorebutton,$GUI_DISABLE)
    endif
    if $numbrowsers == 0 then
        GUICtrlSetState($editbutton,$GUI_DISABLE)
        GUICtrlSetState($deletebutton,$GUI_DISABLE)
        GUICtrlSetState($gobutton,$GUI_DISABLE)
    endif
    GUISetState(@SW_SHOW)
EndFunc

Func getBrowserData($friendlyname)
    $command = IniRead(@ScriptDir & '\SelectBrowser.ini',$friendlyname,'BrowserCommand','')
    $icon = IniRead(@ScriptDir & '\SelectBrowser.ini',$friendlyname,'IconPath','')
    $iconnumber = IniRead(@ScriptDir & '\SelectBrowser.ini',$friendlyname,'IconNumber','')
    $relativepath = IniRead(@ScriptDir & '\SelectBrowser.ini',$friendlyname,'RelativePath','')
    $exelocation = StringInStr($command,'.exe') + 4
    $slashlocation = StringInStr($command,"\","",-1,$exelocation)
    $drivelocation = StringInStr($command,":") - 1
    $exename = StringMid($command,$slashlocation+1,$exelocation-$slashlocation-1)
    $exepath = StringMid($command,$drivelocation,$exelocation-$drivelocation)
    Dim $return[6] = [$command,$icon,$exename,$exepath,$iconnumber,$relativepath]
    return $return
EndFunc

Func closeWindow()
    Exit
EndFunc

Func buildEditWindow($friendlynametoedit,$commandtoedit,$icontoedit,$iconNumbertoedit,$relativepathtoedit)
    global $friendlynamebeingedited = $friendlynametoedit
    global $editwindow
    global $main = WinGetPos($mainwindow) ;find where main window is
    global $editwindow = GuiCreate("Edit Browser",315,150,$main[0]+20,$main[1]+20,$WS_POPUP+$WS_CAPTION+$WS_SYSMENU,$WS_EX_TOOLWINDOW)
    GUISetOnEvent($GUI_EVENT_CLOSE,"closeEdit")
    ;====name====
    GUICtrlCreateLabel("Name",30,5)
    global $editname = GUICtrlCreateInput($friendlynametoedit,30,20,165,20)
    ;====relative path====
    global $editrelative = GUICtrlCreateCheckbox("Relative Paths",200,20,110,20)
    if $relativepathtoedit == "1" then
        GUICtrlSetState($editrelative,$GUI_CHECKED)
    endif
    GUICtrlSetOnEvent($editrelative,"setRelativeOrAbsolute")
    ;====command====
    GUICtrlCreateLabel("Command",30,45)
    global $editcommand = GUICtrlCreateInput($commandtoedit,30,60,210,20)
    global $editcommandbutton = GUICtrlCreateButton("Browse",245,60,65,20)
    GUICtrlSetOnEvent($editcommandbutton,"browseCommand")
    ;====icon====
    global $iconpreview = GUICtrlCreateIcon($icontoedit, Number($iconNumbertoedit), 10, 103, 16, 16)
    global $iconnumber = GUICtrlCreateInput($iconNumbertoedit,200,100,40,20)
    global $iconupdown = GUICtrlCreateUpdown($iconnumber)
    GUICtrlSetOnEvent($iconupdown,"changeIconNumber")
    GUICtrlSetLimit($iconupdown,999,0)
    GUICtrlCreateLabel("Icon",30,85)
    global $editicon = GUICtrlCreateInput($icontoedit,30,100,165,20)
    global $editiconbutton = GUICtrlCreateButton("Browse",245,100,65,20)
    GUICtrlSetOnEvent($editiconbutton,"browseIcon")
    ;====save====
    global $editsave = GUICtrlCreateButton("Save",30,125,50,20)
    GUICtrlSetOnEvent($editsave,"saveBrowser")
    ;====cancel====
    global $editcancel = GUICtrlCreateButton("Cancel",85,125,50,20)
    GUICtrlSetOnEvent($editcancel, "closeEdit")
    ;==============
    GUISetState(@SW_SHOW)
    GUISetState(@SW_DISABLE,$mainwindow)
EndFunc

Func newBrowser() ;build new item window
    buildEditWindow('','','',0,0)
EndFunc

Func editBrowser()
    global $editwindow
    for $i = 1 to $count
        if GUICtrlRead(eval("browser" & $i)) == 1 then
            $friendlyname = GUICtrlRead(eval("browser" & $i),1)
            $browserData = getBrowserData($friendlyname)
            buildEditWindow($friendlyname,$browserData[0],$browserData[1],$browserData[4],$browserData[5])
        endif
    next
EndFunc

Func deleteBrowser()
    For $i = 1 to $count
        if GUICtrlRead(eval("browser" & $i)) == 1 then
            $friendlyname = GUICtrlRead(eval("browser" & $i),1)
            IniDelete(@ScriptDir & '\SelectBrowser.ini',$friendlyname)
            GUISwitch($mainwindow)
            GUIDelete() ; Refresh main window... this currently causes taskbar button to rebuild.
            buildMain() ; Maybe there's a better way.
        EndIf
    Next
EndFunc

Func saveBrowser()
    if NOT ($friendlynamebeingedited == GUICtrlRead($editname)) and NOT ($friendlynamebeingedited == "") then
        IniDelete(@ScriptDir & '\SelectBrowser.ini',$friendlynamebeingedited)
    endif
    $savedcommand = IniWrite(@ScriptDir & '\SelectBrowser.ini',GUICtrlRead($editname),'BrowserCommand',"""" & GUICtrlRead($editcommand) & """")
    $savedicon = IniWrite(@ScriptDir & '\SelectBrowser.ini',GUICtrlRead($editname),'IconPath',"""" & GUICtrlRead($editicon) & """")
    $savediconnumber = IniWrite(@ScriptDir & '\SelectBrowser.ini',GUICtrlRead($editname),'IconNumber',"""" & GUICtrlRead($iconnumber) & """")
    $savedrelativepath = IniWrite(@ScriptDir & '\SelectBrowser.ini',GUICtrlRead($editname),'RelativePath',"""" & GUICtrlRead($editrelative) & """")
    GUISwitch($editwindow)
    GUIDelete()
    GUISwitch($mainwindow)
    GUIDelete()
    buildMain()
EndFunc

Func browseCommand()
    $var = FileOpenDialog("Select Browser", @ScriptDir & "\", "Executables (*.exe;*.bat)|All (*.*)", 1 )
    if $var <> "" then
        if GUICtrlRead($editrelative) == 1 then
            $var = absoluteToRelative($var)
        endif
        GUICtrlSetData($editcommand,"""" & $var & """ ""%1""")
        if GUICtrlRead($editname) == '' then
            GUICtrlSetData($editname,folderNameFromExePath($var))
        endif
        if GUICtrlRead($editicon) == '' then
            GUICtrlSetData($editicon,$var)
            GUICtrlSetImage($iconpreview,$var)
        endif
    endif
EndFunc

Func folderNameFromExePath($exepath) ;gives name of folder containing exe file
    $exelocation = StringInStr($exepath,'.exe') + 4
    $slashlocation2 = StringInStr($exepath,"\","",-1,$exelocation)
    $slashlocation1 = StringInStr($exepath,"\","",-1,$slashlocation2-1)
    return StringMid($exepath,$slashlocation1+1,$slashlocation2-$slashlocation1-1)
EndFunc

Func exePathFromCommand($command)
    $exelocation = StringInStr($command,'.exe') + 4
    $slashlocation = StringInStr($command,"\","",-1,$exelocation)
    if StringInStr($command,":") then
        $drivelocation = StringInStr($command,":") - 1
        return StringMid($command,$drivelocation,$exelocation-$drivelocation)
    else ;must be relative
        $lastQuoteBeforeExe = StringInStr(StringMid($command,1,$exelocation-1),"""")
        if $lastQuoteBeforeExe then
            $startOfPath = $lastQuoteBeforeExe + 1
        else
            $startOfPath = 1
        endif
        return StringMid($command,$startOfPath,$exelocation-$startOfPath)
    endif
EndFunc

Func absoluteToRelative($command) ;returns relative path PROBLEM - THIS WILL BREAK IF FED A RELATIVE PATH
    $exePath = exePathFromCommand($command)
    $i = 1
    While StringMid($exePath,$i,1) == StringMid(@ScriptFullPath,$i,1)
        $i = $i + 1
    WEnd
    $dotdots = ""
    if $i > 1 then
        StringReplace(StringMid(@ScriptFullPath,$i), "\", "\")
        $numSlashes = @Extended
        for $j = 1 to $numSlashes
            $dotdots = $dotdots & "..\"
        next
    endif
    $relativeExePath = $dotdots & StringMid($exePath,$i)
    return StringReplace($command,$exePath,$relativeExePath)
EndFunc

Func relativeToAbsolute($command) ;returns absolute path PROBLEM - ONLY WORKS IF FILE EXISTS
    $exePath = exePathFromCommand($command)
    $relativeExePath = FileGetLongName($exePath,1)
    return StringReplace($command,$exePath,$relativeExePath)
EndFunc

Func setRelativeOrAbsolute()
    if GUICtrlRead($editrelative) == 1 then
        GUICtrlSetData($editcommand,absoluteToRelative(GUICtrlRead($editcommand)))
        GUICtrlSetData($editicon,absoluteToRelative(GUICtrlRead($editicon)))
    else
        GUICtrlSetData($editcommand,relativeToAbsolute(GUICtrlRead($editcommand)))
        GUICtrlSetData($editicon,relativeToAbsolute(GUICtrlRead($editicon)))
    endif
EndFunc

Func removeSpaces($stringWithSpaces)
    return StringReplace($stringWithSpaces," ","")
EndFunc

Func browseIcon()
    $var = FileOpenDialog("Select Icon", @ScriptDir & "\", "Icons (*.ico;*.exe;*.dll)|All (*.*)", 1 )
    if $var <> "" then
        GUICtrlSetData($editicon,$var)
        GUICtrlSetImage($iconpreview,$var)
    endif
EndFunc

Func changeIconNumber()
    $newiconnumber = Number(-(GUICtrlRead($iconnumber)+1))
    GUICtrlSetImage($iconpreview,GUICtrlRead($editicon),$newiconnumber)
EndFunc

Func closeEdit()
    GUISwitch($editwindow)
    GUIDelete()
    GUISetState(@SW_ENABLE,$mainwindow)
    WinActivate($mainwindow)
EndFunc

Func setDefault()
    for $i = 1 to $count
        if GUICtrlRead(eval("browser" & $i)) == 1 then
            $friendlyname = GUICtrlRead(eval("browser" & $i),1)
            $browserData = getBrowserData($friendlyname)
            ;===Win7====
            RegWrite('HKCU\Software\Classes\' & removeSpaces($friendlyname) & 'URL','','REG_SZ',$friendlyname & ' URL')
            RegWrite('HKCU\Software\Classes\' & removeSpaces($friendlyname) & 'URL','FriendlyTypeName','REG_SZ',$friendlyname & ' URL')
            RegWrite('HKCU\Software\Classes\' & removeSpaces($friendlyname) & 'URL','URL Protocol','REG_SZ','')
            RegWrite('HKCU\Software\Classes\' & removeSpaces($friendlyname) & 'URL','EditFlags','REG_DWORD','2')
            RegWrite('HKCU\Software\Classes\' & removeSpaces($friendlyname) & 'URL\Shell\Open\Command','','REG_SZ',relativeToAbsolute($browserData[0]))
            ;===/Win7===
            for $class in $classes
                if NOT FileExists(@ScriptDir & '\' & @ComputerName & "-" & $class & '.reg') then ;only back up current registry entries if there is not already a backup for this computer name
                    ShellExecuteWait('reg.exe','export "HKCU\Software\Classes\' & $class & '" "' & @ScriptDir & '\' & @ComputerName & "-" & $class & '.reg"',@ScriptDir,'',@SW_HIDE) ;export reg settings for each class
                    ;===Win7====
                    ShellExecuteWait('reg.exe','export "HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\' & $class & '" "' & @ScriptDir & '\' & @ComputerName & "-" & $class & '-Win7.reg"',@ScriptDir,'',@SW_HIDE) ;export reg settings for each class
                    ;===/Win7===
                endif
                RegDelete('HKCU\Software\Classes\' & $class)
                RegWrite('HKCU\Software\Classes\' & $class & '\DefaultIcon','','REG_SZ',relativeToAbsolute($browserData[1]))
                RegWrite('HKCU\Software\Classes\' & $class & '\shell','','REG_SZ','open')
                RegWrite('HKCU\Software\Classes\' & $class & '\shell\open\command','','REG_SZ',relativeToAbsolute($browserData[0]))
                RegWrite('HKCU\Software\Classes\' & $class & '\shell\open\ddeexec','','REG_SZ','')
                ;===Win7====
                RegWrite('HKCU\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\' & $class & '\UserChoice','Progid','REG_SZ',removeSpaces($friendlyname) & 'URL')
                ;===/Win7===
            next
            if NOT FileExists(@ScriptDir & '\' & @ComputerName & '-StartMenuInternet.reg') then ;only back up current registry entries if there is not already a backup for this computer name
                ShellExecuteWait('reg.exe','export "HKLM\Software\Clients\StartMenuInternet" "' & @ScriptDir & '\' & @ComputerName & '-StartMenuInternet.reg"',@ScriptDir,'',@SW_HIDE) ;export reg settings for start menu internet
            endif
            RegWrite('HKLM\Software\Clients\StartMenuInternet\' & $browserData[2],'','REG_SZ',$friendlyname)
            RegWrite('HKLM\Software\Clients\StartMenuInternet\' & $browserData[2],'LocalizedString','REG_SZ',$friendlyname)
            RegWrite('HKLM\Software\Clients\StartMenuInternet\' & $browserData[2] & '\shell\open\command','','REG_SZ',relativeToAbsolute(exePathFromCommand($browserData[0])))
            RegWrite('HKLM\Software\Clients\StartMenuInternet\' & $browserData[2] & '\DefaultIcon','','REG_SZ',relativeToAbsolute($browserData[1]) & ',' & $browserData[4])
            RegWrite('HKLM\Software\Clients\StartMenuInternet','','REG_SZ',StringUpper($browserData[2]))
            ;RegWrite('HKCU\Software\Clients\StartMenuInternet','','REG_SZ',StringUpper($browserData[2]))
            DllCall("user32.dll", "int", "SendMessageW", "hwnd", 0xFFFF, "uint", 0x001A, "wparam", 0, "wstr", "Software\Clients\StartMenuInternet")
        endif
    next
EndFunc

Func restoreBackup()
    for $class in $classes
        RegDelete('HKCU\Software\Classes\' & $class)
        ShellExecuteWait('reg.exe','import "' & @ScriptDir & '\' & @ComputerName & "-" & $class & '.reg"',@ScriptDir,"",@SW_HIDE)
        ;===Win7====
        ShellExecuteWait('reg.exe','import "' & @ScriptDir & '\' & @ComputerName & "-" & $class & '-Win7.reg"',@ScriptDir,"",@SW_HIDE)
        ;===/Win7====
    next
    RegDelete('HKLM\Software\Clients\StartMenuInternet')
    ShellExecuteWait('reg.exe','import "' & @ScriptDir & '\' & @ComputerName & '-StartMenuInternet.reg"',@ScriptDir,"",@SW_HIDE)
    DllCall("user32.dll", "int", "SendMessage", "hwnd", 0xFFFF, "int", 0x001A, "int", 0, "int", "Environment") ;refresh explorer to show correct start menu browser
EndFunc

Can anyone improve this ?

It doesn't work in all situations.

OK 1-Internet Shortcuts from Windows Explorer

OK 2-Links from about boxes in programs

NOT OK (defaultbrowser gets this one) 3-links in windows live messenger

NOT OK 4-TEXT links from the bat or any other mail client

OK 5-HREF links from the bat or any other mail client

OK 6-Links from RSS Feed readers SNARFER PORTABLE

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×