Snow_Flake

Installer.ahk

Oct 3rd, 2014
1,028
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #NoEnv
  2. #NoTrayIcon
  3. ; #Warn
  4. #SingleInstance Off
  5.  
  6. if !A_IsAdmin && !%False%
  7. {
  8.     if A_OSVersion not in WIN_2003,WIN_XP,WIN_2000
  9.     {
  10.         Run *RunAs "%A_AhkPath%" "%A_ScriptFullPath%",, UseErrorLevel
  11.         if !ErrorLevel
  12.             ExitApp
  13.     }
  14.     MsgBox 0x31, AutoHotkey Setup,
  15.     (LTrim Join`s
  16.     Setup is running as a limited user.  If you continue, some problems
  17.     are likely to occur.  It is strongly recommended that you run Setup
  18.     as an administrator.`n
  19.     `n
  20.     To continue anyway, click OK.  Otherwise click Cancel.
  21.     )
  22.     IfMsgBox Cancel
  23.         ExitApp
  24. }
  25.  
  26. SourceDir := A_ScriptDir
  27. SilentMode := false
  28. SilentErrors := 0
  29.  
  30. if 0 > 0
  31. if 1 = /kill ; For internal use.
  32. {
  33.     DetectHiddenWindows On
  34.     WinKill % "ahk_id " %0%
  35.     ExitApp
  36. }
  37. else if 1 = /fin ; For internal use.
  38. {
  39.     DetectHiddenWindows On
  40.     WinKill ahk_id %3%
  41.     WinWaitClose ahk_id %3%,, 1
  42.    
  43.     exefile = %2%
  44.     InstallFile(exefile, "AutoHotkey.exe")
  45.     if 4 = 0 ; SilentMode
  46.         MsgBox 64, AutoHotkey Setup, The settings have been updated.
  47.     ExitApp
  48. }
  49. else if 1 = /runahk ; For internal use.
  50. {
  51.     RunAutoHotkey_()
  52.     ExitApp
  53. }
  54.  
  55. ProductName := "AutoHotkey"
  56. ProductVersion := A_AhkVersion
  57. ProductPublisher := "Lexikos"
  58. ProductWebsite := "http://ahkscript.org/"
  59.  
  60. EnvGet ProgramW6432, ProgramW6432
  61. DefaultPath := (ProgramW6432 ? ProgramW6432 : A_ProgramFiles) "\AutoHotkey"
  62. DefaultType := A_Is64bitOS ? "x64" : "Unicode"
  63. DefaultStartMenu := "AutoHotkey"
  64. DefaultCompiler := true
  65. DefaultDragDrop := true
  66. DefaultToUTF8 := false
  67. DefaultIsHostApp := false
  68. AutoHotkeyKey := "SOFTWARE\AutoHotkey"
  69. UninstallKey := "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AutoHotkey"
  70. FileTypeKey := "AutoHotkeyScript"
  71.  
  72. DetermineVersion()
  73.  
  74. Loop %0%
  75.     if %A_Index% = /S
  76.         SilentMode := true
  77.     else if %A_Index% = /U32
  78.         DefaultType = Unicode
  79.     else if %A_Index% in /U64,/x64
  80.         DefaultType = x64
  81.     else if %A_Index% in /A32,/ANSI
  82.         DefaultType = ANSI
  83.     else if InStr(%A_Index%, "/D=") = 1 {
  84.         if !RegExMatch(DllCall("GetCommandLine", "str"), "(?<!"")/D=\K[^""]*?(?=$|[ `t]+/)", DefaultPath)
  85.             DefaultPath := SubStr(%A_Index%, 4)
  86.         Loop %DefaultPath%, 2  ; Resolve relative path.
  87.             DefaultPath := A_LoopFileLongPath
  88.         SlashD := true
  89.     }
  90.     else if (%A_Index% = "/?") {
  91.         ViewHelp("/docs/Scripts.htm#install")
  92.         ExitApp
  93.     }
  94.     else if (%A_Index% = "/Uninstall") {
  95.         SilentMode := true
  96.         Uninstall()
  97.         ExitApp
  98.     }
  99.     else if (%A_Index% = "/E") {
  100.         Extract(SlashD ? DefaultPath : "")
  101.         ExitApp
  102.     }
  103.     else if (SubStr(%A_Index%,1,5) = "/Test") {
  104.         TestMode := SubStr(%A_Index%,6)
  105.     }
  106.  
  107. if SilentMode {
  108.     QuickInstall()
  109.     ExitApp % SilentErrors
  110. }
  111.  
  112. if WinExist("AutoHotkey Setup ahk_class AutoHotkeyGUI") {
  113.     MsgBox 0x30, AutoHotkey Setup, AutoHotkey Setup is already running!
  114.     WinActivate
  115.     ExitApp
  116. }
  117.  
  118. OnExit GuiClose
  119.  
  120. Gui Margin, 0, 0
  121. Gui Add, ActiveX, vwb w600 h400 hwndhwb, Shell.Explorer
  122. ComObjConnect(wb, "wb_")
  123. OnMessage(0x100, "gui_KeyDown", 2)
  124. try {
  125.     if !wb
  126.         throw Exception("Failed to create IE control")
  127.     if (TestMode = "FailUI")
  128.         throw Exception("Testing UI")
  129.     InitUI()
  130. }
  131. catch excpt {
  132.     excpt := excpt.Message
  133.     if (A_ScriptDir = DefaultPath) {
  134.         MsgBox 0x10, AutoHotkey Setup, Setup failed to initialize its user interface and will now exit.
  135.         ExitApp
  136.     }
  137.     type := DefaultType="ANSI" ? "ANSI 32-bit" : "Unicode " (DefaultType="x64"?"64":"32") "-bit"
  138.     MsgBox 0x13, AutoHotkey Setup,
  139. (
  140. Setup failed to initialize its user interface.
  141.   Error: %excpt%
  142.  
  143. Do you want to install with default options?
  144.   %ProductName% v%ProductVersion% (%type%)
  145.   %DefaultPath%
  146.  
  147. Click Yes to install.
  148. Click No to copy setup files to a directory of your choosing.
  149. Click Cancel to exit.
  150. )
  151.     IfMsgBox Yes
  152.         QuickInstall()
  153.     else IfMsgBox No
  154.         Extract()
  155.     ExitApp
  156. }
  157. Gui Show,, AutoHotkey Setup
  158. return
  159.  
  160. GuiEscape:
  161. MsgBox 0x34, AutoHotkey Setup, Are you sure you want to exit setup?
  162. IfMsgBox No
  163.     return
  164. GuiClose:
  165. Gui Destroy
  166. OnExit
  167. ExitApp
  168.  
  169. DetermineVersion() {
  170.     global
  171.     local url, v
  172.     ; This first section has two purposes:
  173.     ;  1) Determine the location of any current installation.
  174.     ;  2) Determine which view of the registry it was installed into
  175.     ;     (only applicable if the OS is 64-bit).
  176.     CurrentRegView := ""
  177.     Loop % (A_Is64bitOS ? 2 : 1) {
  178.         SetRegView % 32*A_Index
  179.         RegRead CurrentPath, HKLM, %AutoHotkeyKey%, InstallDir
  180.         if !ErrorLevel {
  181.             CurrentRegView := A_RegView
  182.             break
  183.         }
  184.     }
  185.     if ErrorLevel {
  186.         CurrentName := ""
  187.         CurrentVersion := ""
  188.         CurrentType := ""
  189.         CurrentPath := ""
  190.         CurrentStartMenu := ""
  191.         return
  192.     }
  193.     RegRead CurrentVersion, HKLM, %AutoHotkeyKey%, Version
  194.     RegRead CurrentStartMenu, HKLM, %AutoHotkeyKey%, StartMenuFolder
  195.     RegRead url, HKLM, %UninstallKey%, URLInfoAbout
  196.     ; Identify by URL since uninstaller display name is the same:
  197.     if (url = "http://www.autohotkey.net/~Lexikos/AutoHotkey_L/"
  198.         || url = "http://l.autohotkey.net/")
  199.         CurrentName := "AutoHotkey_L"
  200.     else
  201.         CurrentName := "AutoHotkey"
  202.     ; Identify which build is installed/set as default:
  203.     FileAppend ExitApp `% (A_IsUnicode=1) << 8 | (A_PtrSize=8) << 9, %A_Temp%\VersionTest.ahk
  204.     RunWait %CurrentPath%\AutoHotkey.exe "%A_Temp%\VersionTest.ahk",, UseErrorLevel
  205.     if ErrorLevel = 0x300
  206.         CurrentType := "x64"
  207.     else if ErrorLevel = 0x100
  208.         CurrentType := "Unicode"
  209.     else if ErrorLevel = 0
  210.         CurrentType := "ANSI"
  211.     else
  212.         CurrentType := ""
  213.     FileDelete %A_Temp%\VersionTest.ahk
  214.     ; Set some default parameter based on current installation:
  215.     if CurrentType
  216.         DefaultType := CurrentType
  217.     DefaultPath := CurrentPath
  218.     DefaultStartMenu := CurrentStartMenu
  219.     DefaultCompiler := FileExist(CurrentPath "\Compiler\Ahk2Exe.exe") != ""
  220.     RegRead v, HKCR, %FileTypeKey%\ShellEx\DropHandler
  221.     DefaultDragDrop := ErrorLevel = 0
  222.     RegRead v, HKCR, Applications\AutoHotkey.exe, IsHostApp
  223.     DefaultIsHostApp := !ErrorLevel
  224.     RegRead v, HKCR, %FileTypeKey%\Shell\Open\Command
  225.     DefaultToUTF8 := InStr(v, " /CP65001 ") != 0
  226. }
  227.  
  228. InitUI() {
  229.     local w
  230.     SetWBClientSite()
  231.     gosub DefineUI
  232.     wb.Silent := true
  233.     wb.Navigate("about:blank")
  234.     while wb.ReadyState != 4
  235.         Sleep 10
  236.     wb.Document.open()
  237.     wb.Document.write(html)
  238.     wb.Document.Close()
  239.     w := wb.Document.parentWindow
  240.     if (!CurrentType && A_ScriptDir != DefaultPath)
  241.         CurrentName := ""  ; Avoid showing the Reinstall option since we don't know which version it was.
  242.     w.initOptions(CurrentName, CurrentVersion, CurrentType
  243.                 , ProductVersion, DefaultPath, DefaultStartMenu
  244.                 , DefaultType, A_Is64bitOS = 1)
  245.     if (A_ScriptDir = DefaultPath) {
  246.         w.installdir.disabled := true
  247.         w.installdir_browse.disabled := true
  248.         w.installcompiler.disabled := !DefaultCompiler
  249.         w.installcompilernote.style.display := "block"
  250.         w.ci_nav_install.innerText := "apply"
  251.         w.install_button.innerText := "Apply"
  252.         w.extract.style.display := "None"
  253.         w.opt1.disabled := true
  254.         w.opt1.firstChild.innerText := "Checking for updates..."
  255.         SetTimer CheckForUpdates, -500
  256.     }
  257.     w.installcompiler.checked := DefaultCompiler
  258.     w.enabledragdrop.checked := DefaultDragDrop
  259.     w.separatebuttons.checked := DefaultIsHostApp
  260.     ; w.defaulttoutf8.checked := DefaultToUTF8
  261.     if !A_Is64bitOS
  262.         w.it_x64.style.display := "None"
  263.     if A_OSVersion in WIN_2000,WIN_2003,WIN_XP ; i.e. not WIN_7, WIN_8 or a future OS.
  264.         w.separatebuttons.parentNode.style.display := "none"
  265.     w.switchPage("start")
  266.     w.document.body.focus()
  267.     ; Scale UI by screen DPI.  My testing showed that Vista with IE7 or IE9
  268.     ; did not scale by default, but Win8.1 with IE10 did.  The scaling being
  269.     ; done by the control itself = deviceDPI / logicalDPI.
  270.     logicalDPI := w.screen.logicalXDPI, deviceDPI := w.screen.deviceXDPI
  271.     if (A_ScreenDPI != 96)
  272.         w.document.body.style.zoom := A_ScreenDPI/96 * (logicalDPI/deviceDPI)
  273. }
  274.  
  275. CheckForUpdates:
  276. CheckForUpdates()
  277. return
  278. CheckForUpdates() {
  279.     local w := getWindow(), latestVersion := ""
  280.     URLDownloadToFile http://ahkscript.org/download/1.1/version.txt, %A_Temp%\ahk_version.txt
  281.     if !ErrorLevel {
  282.         FileRead latestVersion, %A_Temp%\ahk_version.txt
  283.         FileDelete %A_Temp%\ahk_version.txt
  284.     }
  285.     if RegExMatch(latestVersion, "^(\d+\.){3}\d+") {
  286.         if (latestVersion = ProductVersion)
  287.             w.opt1.firstChild.innerText := "Reinstall (download required)"
  288.         else
  289.             w.opt1.firstChild.innerText := "Download v" latestVersion
  290.         w.opt1.href := "ahk://Download/"
  291.         w.opt1.disabled := false
  292.     } else
  293.         w.opt1.innerText := "An error occurred while checking for updates."
  294. }
  295.  
  296. /*  Fix keyboard shortcuts in WebBrowser control.
  297.  *  References:
  298.  *    http://www.autohotkey.com/community/viewtopic.php?p=186254#p186254
  299.  *    http://msdn.microsoft.com/en-us/library/ms693360
  300.  */
  301.  
  302. gui_KeyDown(wParam, lParam, nMsg, hWnd) {
  303.     global wb
  304.     pipa := ComObjQuery(wb, "{00000117-0000-0000-C000-000000000046}")
  305.     VarSetCapacity(kMsg, 48), NumPut(A_GuiY, NumPut(A_GuiX
  306.     , NumPut(A_EventInfo, NumPut(lParam, NumPut(wParam
  307.     , NumPut(nMsg, NumPut(hWnd, kMsg)))), "uint"), "int"), "int")
  308.     Loop 2
  309.     r := DllCall(NumGet(NumGet(1*pipa)+5*A_PtrSize), "ptr", pipa, "ptr", &kMsg)
  310.     ; Loop to work around an odd tabbing issue (it's as if there
  311.     ; is a non-existent element at the end of the tab order).
  312.     until wParam != 9 || wb.Document.activeElement != ""
  313.     ObjRelease(pipa)
  314.     if r = 0 ; S_OK: the message was translated to an accelerator.
  315.         return 0
  316. }
  317.  
  318.  
  319. /*  ahk://Func/Param  -->  Func("Param")
  320.  */
  321.  
  322. wb_BeforeNavigate2(wb, url, flags, frame, postdata, headers, cancel) {
  323.     if !RegExMatch(url, "^ahk://(.*?)/(.*)", m)
  324.         return
  325.     static func, prms
  326.     func := m1
  327.     prms := []
  328.     StringReplace m2, m2, `%20, %A_Space%, All
  329.     Loop Parse, m2, `,
  330.         prms.Insert(A_LoopField)
  331.     ; Cancel: don't load the error page (or execute ahk://whatever
  332.     ; if it happens to somehow be a registered protocol).
  333.     NumPut(-1, ComObjValue(cancel), "short")
  334.     ; Call after a delay to allow navigation (this might only be
  335.     ; necessary if called from NavigateError; i.e. on Windows 8).
  336.     SetTimer wb_bn2_call, -15
  337.     return
  338. wb_bn2_call:
  339.    %func%(prms*)
  340.     func := prms := ""
  341.     return
  342. }
  343.  
  344. wb_NavigateError(wb, url, frame, status, cancel) {
  345.     ; This might only be called on Windows 8, which skips the
  346.     ; BeforeNavigate2 call (because the protocol is invalid?).
  347.     wb_BeforeNavigate2(wb, url, 0, frame, "", "", cancel)
  348. }
  349.  
  350.  
  351. /*  Complex workaround to override "Active scripting" setting
  352.  *  and ensure scripts can run within the WebBrowser control.
  353.  */
  354.  
  355. global WBClientSite
  356.  
  357. SetWBClientSite()
  358. {
  359.     interfaces := {
  360.     (Join,
  361.         IOleClientSite: [0,3,1,0,1,0]
  362.         IServiceProvider: [3]
  363.         IInternetSecurityManager: [1,1,3,4,8,7,3,3]
  364.     )}
  365.     unkQI      := RegisterCallback("WBClientSite_QI", "Fast")
  366.     unkAddRef  := RegisterCallback("WBClientSite_AddRef", "Fast")
  367.     unkRelease := RegisterCallback("WBClientSite_Release", "Fast")
  368.     WBClientSite := {_buffers: bufs := {}}, bufn := 0,
  369.     for name, prms in interfaces
  370.     {
  371.         bufn += 1
  372.         bufs.SetCapacity(bufn, (4 + prms.MaxIndex()) * A_PtrSize)
  373.         buf := bufs.GetAddress(bufn)
  374.         NumPut(unkQI,       buf + 1*A_PtrSize)
  375.         NumPut(unkAddRef,   buf + 2*A_PtrSize)
  376.         NumPut(unkRelease,  buf + 3*A_PtrSize)
  377.         for i, prmc in prms
  378.             NumPut(RegisterCallback("WBClientSite_" name, "Fast", prmc+1, i), buf + (3+i)*A_PtrSize)
  379.         NumPut(buf + A_PtrSize, buf + 0)
  380.         WBClientSite[name] := buf
  381.     }
  382.     global wb
  383.     if pOleObject := ComObjQuery(wb, "{00000112-0000-0000-C000-000000000046}")
  384.     {   ; IOleObject::SetClientSite
  385.         DllCall(NumGet(NumGet(pOleObject+0)+3*A_PtrSize), "ptr"
  386.             , pOleObject, "ptr", WBClientSite.IOleClientSite, "uint")
  387.         ObjRelease(pOleObject)
  388.     }
  389. }
  390.  
  391. WBClientSite_QI(p, piid, ppvObject)
  392. {
  393.     static IID_IUnknown := "{00000000-0000-0000-C000-000000000046}"
  394.     static IID_IOleClientSite := "{00000118-0000-0000-C000-000000000046}"
  395.     static IID_IServiceProvider := "{6d5140c1-7436-11ce-8034-00aa006009fa}"
  396.     iid := _String4GUID(piid)
  397.     if (iid = IID_IOleClientSite || iid = IID_IUnknown)
  398.     {
  399.         NumPut(WBClientSite.IOleClientSite, ppvObject+0)
  400.         return 0 ; S_OK
  401.     }
  402.     if (iid = IID_IServiceProvider)
  403.     {
  404.         NumPut(WBClientSite.IServiceProvider, ppvObject+0)
  405.         return 0 ; S_OK
  406.     }
  407.     NumPut(0, ppvObject+0)
  408.     return 0x80004002 ; E_NOINTERFACE
  409. }
  410.  
  411. WBClientSite_AddRef(p)
  412. {
  413.     return 1
  414. }
  415.  
  416. WBClientSite_Release(p)
  417. {
  418.     return 1
  419. }
  420.  
  421. WBClientSite_IOleClientSite(p, p1="", p2="", p3="")
  422. {
  423.     if (A_EventInfo = 3) ; GetContainer
  424.     {
  425.         NumPut(0, p1+0) ; *ppContainer := NULL
  426.         return 0x80004002 ; E_NOINTERFACE
  427.     }
  428.     return 0x80004001 ; E_NOTIMPL
  429. }
  430.  
  431. WBClientSite_IServiceProvider(p, pguidService, piid, ppvObject)
  432. {
  433.     static IID_IUnknown := "{00000000-0000-0000-C000-000000000046}"
  434.     static IID_IInternetSecurityManager := "{79eac9ee-baf9-11ce-8c82-00aa004ba90b}"
  435.     if (_String4GUID(pguidService) = IID_IInternetSecurityManager)
  436.     {
  437.         iid := _String4GUID(piid)
  438.         if (iid = IID_IInternetSecurityManager || iid = IID_IUnknown)
  439.         {
  440.             NumPut(WBClientSite.IInternetSecurityManager, ppvObject+0)
  441.             return 0 ; S_OK
  442.         }
  443.         NumPut(0, ppvObject+0)
  444.         return 0x80004002 ; E_NOINTERFACE
  445.     }
  446.     NumPut(0, ppvObject+0)
  447.     return 0x80004001 ; E_NOTIMPL
  448. }
  449.  
  450. WBClientSite_IInternetSecurityManager(p, p1="", p2="", p3="", p4="", p5="", p6="", p7="", p8="")
  451. {
  452.     if (A_EventInfo = 5) ; ProcessUrlAction
  453.     {
  454.         if (p2 = 0x1400) ; dwAction = URLACTION_SCRIPT_RUN
  455.         {
  456.             NumPut(0, p3+0)  ; *pPolicy := URLPOLICY_ALLOW
  457.             return 0 ; S_OK
  458.         }
  459.     }
  460.     return 0x800C0011 ; INET_E_DEFAULT_ACTION
  461. }
  462.  
  463. _String4GUID(pGUID)
  464. {
  465.     VarSetCapacity(String,38*2)
  466.     DllCall("ole32\StringFromGUID2", "ptr", pGUID, "str", String, "int", 39)
  467.     Return  String
  468. }
  469.  
  470.  
  471. /*  Utility Functions
  472.  */
  473.  
  474. getWindow() {
  475.     global wb
  476.     return wb.document.parentWindow
  477. }
  478.  
  479. ErrorExit(errMsg) {
  480.     global
  481.     if SilentMode
  482.         ExitApp 1
  483.     MsgBox 16, AutoHotkey Setup, %errMsg%
  484.     Exit
  485. }
  486.  
  487. CloseScriptsEtc(installdir, actionToContinue) {
  488.     titles := ""
  489.     DetectHiddenWindows On
  490.     close := []
  491.     WinGet w, List, ahk_class AutoHotkey
  492.     Loop % w {
  493.         ; Exclude the install script.
  494.         if (w%A_Index% = A_ScriptHwnd)
  495.             continue
  496.         ; Determine if the script actually needs to be terminated.
  497.         WinGet exe, ProcessPath, % "ahk_id " w%A_Index%
  498.         if (exe != "") {
  499.             ; Exclude external executables.
  500.             if InStr(exe, installdir "\") != 1
  501.                 continue
  502.             ; The main purpose of this next check is to avoid closing
  503.             ; SciTE4AutoHotkey's toolbar, but also may be helpful for
  504.             ; other situations.
  505.             exe := SubStr(exe, StrLen(installdir) + 2)
  506.             if !RegExMatch(exe, "i)^(AutoHotkey(A32|U32|U64)?\.exe|Compiler\\Ahk2Exe.exe)$")
  507.                 continue
  508.         }        
  509.         ; Append script path to the list.
  510.         WinGetTitle title, % "ahk_id " w%A_Index%
  511.         title := RegExReplace(title, " - AutoHotkey v.*")
  512.         titles .= "  -  " title "`n"
  513.         close.Insert(w%A_Index%)
  514.     }
  515.     if (titles != "") {
  516.         global SilentMode
  517.         if !SilentMode {
  518.             MsgBox 49, AutoHotkey Setup,
  519.             (LTrim
  520.             Setup needs to close the following script(s):
  521.             `n%titles%
  522.             Click OK to close these scripts and continue the %actionToContinue%.
  523.             )
  524.             IfMsgBox Cancel
  525.                 Exit
  526.         }
  527.         ; Close script windows (typically causing them to exit).
  528.         Loop % close.MaxIndex()
  529.         {
  530.             WinClose % "ahk_id " close[A_Index]
  531.             WinWaitClose % "ahk_id " close[A_Index],, 1
  532.         }
  533.     }
  534.     ; Close all help file and Window Spy windows automatically:
  535.     GroupAdd autoclosegroup, AutoHotkey_L Help ahk_class HH Parent
  536.     GroupAdd autoclosegroup, AutoHotkey Help ahk_class HH Parent
  537.     GroupAdd autoclosegroup, Active Window Info ahk_exe %installdir%\AU3_Spy.exe
  538.     ; Also close the old Ahk2Exe (but the new one is a script, so it
  539.     ; was already handled by the section above):
  540.     GroupAdd autoclosegroup, Ahk2Exe v ahk_exe %installdir%\Compiler\Ahk2Exe.exe
  541.     WinClose ahk_group autoclosegroup
  542. }
  543.  
  544. GetErrorMessage(error_code="") {
  545.     VarSetCapacity(buf, 1024) ; Probably won't exceed 1024 chars.
  546.     if DllCall("FormatMessage", "uint", 0x1200, "ptr", 0, "int", error_code!=""
  547.                 ? error_code : A_LastError, "uint", 1024, "str", buf, "uint", 1024, "ptr", 0)
  548.         return buf
  549. }
  550.  
  551. switchPage(page) {
  552.     global
  553.     if !SilentMode
  554.         getWindow().switchPage(page)
  555. }
  556.  
  557. UpdateStatus(status) {
  558.     ; if !SilentMode
  559.         ; getWindow().install_status.innerText := status
  560. }
  561.  
  562. ShellRun(prms*)
  563. {
  564.     shellWindows := ComObjCreate("{9BA05972-F6A8-11CF-A442-00A0C90A8F39}")
  565.    
  566.     desktop := shellWindows.Item(ComObj(19, 8)) ; VT_UI4, SCW_DESKTOP                
  567.    
  568.     ; Retrieve top-level browser object.
  569.     if ptlb := ComObjQuery(desktop
  570.         , "{4C96BE40-915C-11CF-99D3-00AA004AE837}"  ; SID_STopLevelBrowser
  571.         , "{000214E2-0000-0000-C000-000000000046}") ; IID_IShellBrowser
  572.     {
  573.         ; IShellBrowser.QueryActiveShellView -> IShellView
  574.         if DllCall(NumGet(NumGet(ptlb+0)+15*A_PtrSize), "ptr", ptlb, "ptr*", psv:=0) = 0
  575.         {
  576.             ; Define IID_IDispatch.
  577.             VarSetCapacity(IID_IDispatch, 16)
  578.             NumPut(0x46000000000000C0, NumPut(0x20400, IID_IDispatch, "int64"), "int64")
  579.            
  580.             ; IShellView.GetItemObject -> IDispatch (object which implements IShellFolderViewDual)
  581.             DllCall(NumGet(NumGet(psv+0)+15*A_PtrSize), "ptr", psv
  582.                 , "uint", 0, "ptr", &IID_IDispatch, "ptr*", pdisp:=0)
  583.            
  584.             ; Get Shell object.
  585.             shell := ComObj(9,pdisp,1).Application
  586.            
  587.             ; IShellDispatch2.ShellExecute
  588.             shell.ShellExecute(prms*)
  589.            
  590.             ObjRelease(psv)
  591.         }
  592.         ObjRelease(ptlb)
  593.     }
  594. }
  595.  
  596. Run_(target, args="") {
  597.     try
  598.         ShellRun(target, args)
  599.     catch e
  600.         Run % args="" ? target : target " " args
  601. }
  602.  
  603.  
  604. /*  Utility Functions invoked by the UI
  605.  */
  606.  
  607. Customize() {
  608.     getWindow().switchPage("custom-install")
  609. }
  610.  
  611. SelectFolder(id, prompt="", root="::{20d04fe0-3aea-1069-a2d8-08002b30309d}") {
  612.     global wb
  613.     if !(field := wb.document.getElementById(id))
  614.         return
  615.     Gui +OwnDialogs
  616.     FileSelectFolder path
  617.         , % root " *" field.value
  618.         ,, % prompt
  619.     if !ErrorLevel
  620.         field.value := path
  621. }
  622.  
  623. ReadLicense() {
  624.     Run_(A_ScriptDir "\license.txt")
  625. }
  626.  
  627. ViewHelp(topic) {
  628.     local path
  629.     if FileExist("AutoHotkey.chm")
  630.         path := A_WorkingDir "\AutoHotkey.chm"
  631.     else
  632.         path := CurrentPath "\AutoHotkey.chm"
  633.     if FileExist(path)
  634.         Run_("hh.exe", "mk:@MSITStore:" path "::" topic)
  635.     else
  636.         Run_("http://ahkscript.org" topic)
  637. }
  638.  
  639. RunAutoHotkey() {
  640.     ; Setup may be running as a user other than the one that's logged
  641.     ; in (i.e. an admin user), so in addition to running AutoHotkey.exe
  642.     ; in user mode, have it call the function below to ensure the script
  643.     ; file is correctly located.
  644.     Run_("AutoHotkey.exe", """" A_WorkingDir "\Installer.ahk"" /runahk")
  645. }
  646. RunAutoHotkey_() {
  647.     ; This could detect %ExeDir%\AutoHotkey.ahk (which takes precedence
  648.     ; over %A_MyDocuments%\AutoHotkey.ahk), but that file is unlikely to
  649.     ; exist in this situation.
  650.     script_path := A_MyDocuments "\AutoHotkey.ahk"
  651.     ; Start the script.
  652.     Run AutoHotkey.exe,,, pid
  653.     ; Check for common failures.
  654.     SetTitleMatchMode 2
  655.     DetectHiddenWindows On
  656.     message := ""
  657.     message_flags := 0x34
  658.     Loop {
  659.         Sleep 50
  660.         Process Exist, %pid%
  661.         if !ErrorLevel {
  662.             message =
  663.             (LTrim Join`s
  664.             AutoHotkey has exited.  You may need to edit your startup
  665.             script.  For instance, if it exited because it had nothing
  666.             to do, you can add a hotkey.
  667.             )
  668.             message_flags := 0x44 ; Less severe, since it might be intentional.
  669.             break
  670.         }
  671.         if WinExist("ahk_class #32770 ahk_pid " pid) {
  672.             WinGetText message
  673.             if !InStr(message, "Error")
  674.                 return
  675.             WinWaitClose
  676.             Process Exist, %pid%
  677.             message := "Your script encountered an error" (ErrorLevel ? "." : " and exited.")
  678.                    . "  You will need to edit it to resolve this error."
  679.             break
  680.         }
  681.         if WinExist("ahk_class AutoHotkey ahk_pid " pid) {
  682.             WinWaitClose,,, .2 ; Wait a moment in case the script is empty/about to exit.
  683.             if !ErrorLevel
  684.                 continue ; Back to the top of the loop.
  685.             DetectHiddenWindows Off
  686.             if !WinExist("ahk_pid " pid)
  687.                 MsgBox 0x40, AutoHotkey Setup, Your script is running in the background.
  688.             return
  689.         }
  690.     }
  691.     MsgBox % message_flags, AutoHotkey Setup, %message%`n`nYour script is located here:`n   %script_path%`n`nDo you want to edit this file?
  692.     IfMsgBox Yes
  693.         Run edit "%script_path%"
  694. }
  695.  
  696. Quit() {
  697.     ExitApp
  698. }
  699.  
  700. ViewWebsite() {
  701.     global
  702.     Run_(ProductWebsite)
  703. }
  704.  
  705. Extract(dstDir="") {
  706.     if (dstDir = "") {
  707.         FileSelectFolder dstDir,,, Select a folder to copy program files to.
  708.         if ErrorLevel
  709.             return
  710.     }
  711.     try {
  712.         global TestMode, SourceDir
  713.         if (TestMode = "FailExtract")
  714.             throw
  715.         shell := ComObjCreate("Shell.Application")
  716.         try FileCreateDir %dstDir%
  717.         dst := shell.NameSpace(dstDir)
  718.         src := shell.NameSpace(SourceDir)
  719.         if !(dst && src)
  720.             throw
  721.         try dst.CopyHere(src.Items, 256)
  722.     }
  723.     catch {
  724.         FileCopyDir %SourceDir%, %dstDir%, 1
  725.         if ErrorLevel {
  726.             MsgBox 48, AutoHotkey Setup, An unspecified error occurred.
  727.             return
  728.         }
  729.     }
  730.     Run %dstDir%
  731. }
  732.  
  733. Download() {
  734.     Run http://ahkscript.org/download/ahk-install.exe
  735.     ExitApp
  736. }
  737.  
  738.  
  739. /*  Setup Actions
  740.  */
  741.  
  742. ; Upgrade to newer version or from AutoHotkey to AutoHotkey_L.
  743. ;   Type: "ANSI" or "Unicode"
  744. Upgrade(Type="") {
  745.     global
  746.     _Install({
  747.     (Join C
  748.         type: Type,
  749.         path: DefaultPath,
  750.         menu: DefaultStartMenu,
  751.         ahk2exe: DefaultCompiler,
  752.         dragdrop: DefaultDragDrop,
  753.         utf8: DefaultToUTF8,
  754.         isHostApp: DefaultIsHostApp
  755.     )})
  756. }
  757.  
  758. ; Quick install with default options.
  759. QuickInstall() {
  760.     global
  761.     _Install({
  762.     (Join
  763.         type: DefaultType,
  764.         path: DefaultPath,
  765.         menu: DefaultStartMenu,
  766.         ahk2exe: DefaultCompiler,
  767.         dragdrop: DefaultDragDrop,
  768.         utf8: DefaultToUTF8,
  769.         isHostApp: DefaultIsHostApp
  770.     )})
  771. }
  772.  
  773. ; Begin installation after reviewing options.
  774. CustomInstall() {
  775.     local w := getWindow()
  776.     _Install({
  777.     (C Join
  778.         type: w.installtype.value,
  779.         path: w.installdir.value,
  780.         menu: w.startmenu.value,
  781.         ahk2exe: w.installcompiler.checked,
  782.         dragdrop: w.enabledragdrop.checked,
  783.         utf8: DefaultToUTF8, ;w.defaulttoutf8.checked
  784.         isHostApp: w.separatebuttons.checked
  785.     )})
  786. }
  787.  
  788. ; Uninstall.
  789. Uninstall() {
  790.     global
  791.    
  792.     try
  793.         SetWorkingDir % CurrentPath
  794.     catch
  795.         ErrorExit("Error uninstalling; installation directory '" CurrentPath "' may be invalid.")
  796.    
  797.     CloseScriptsEtc(CurrentPath, "uninstallation")
  798.    
  799.     switchPage("wait")
  800.    
  801.     /*  Registry
  802.      */
  803.    
  804.     SetRegView % CurrentRegView
  805.    
  806.     RegDelete HKLM, %UninstallKey%
  807.     RegDelete HKLM, %AutoHotkeyKey%
  808.     RegDelete HKCU, %AutoHotkeyKey%  ; Created by Ahk2Exe.
  809.    
  810.     RegDelete HKCR, .ahk
  811.     RegDelete HKCR, %FileTypeKey%
  812.     RegDelete HKCR, Applications\AutoHotkey.exe
  813.    
  814.     RegDelete HKLM, SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AutoHotkey.exe
  815.    
  816.     /*  Files
  817.      */
  818.    
  819.     FileDelete AutoHotkeyU32.exe
  820.     FileDelete AutoHotkeyA32.exe
  821.     FileDelete AutoHotkeyU64.exe
  822.    
  823.     FileDelete AU3_Spy.exe
  824.     FileDelete AutoHotkey.chm
  825.     FileDelete license.txt
  826.    
  827.     ; This file would only exist if an older version of AutoHotkey_L
  828.     ; installed it:
  829.     FileDelete Update.ahk
  830.    
  831.     ; Although the old installer was designed not to overwrite this in
  832.     ; case the user made customizations, the old uninstaller deletes it:
  833.     FileDelete %A_WinDir%\ShellNew\Template.ahk
  834.    
  835.     RemoveCompiler()
  836.    
  837.     FileDelete %ProductName% Website.url
  838.     if (CurrentStartMenu != "")  ; Must not remove A_ProgramsCommon itself!
  839.         FileRemoveDir %A_ProgramsCommon%\%CurrentStartMenu%, 1
  840.    
  841.     if !SilentMode
  842.         MsgBox 64, AutoHotkey Setup
  843.             , Setup will now close to complete the uninstallation.
  844.    
  845.     ; Try deleting it normally first, in case this script is running
  846.     ; on an external exe (such as via a downloaded installer).
  847.     FileDelete AutoHotkey.exe
  848.     if !ErrorLevel {
  849.         FileDelete Installer.ahk
  850.         SetWorkingDir %A_Temp%  ; Otherwise FileRemoveDir will fail.
  851.         FileRemoveDir %CurrentPath%  ; Only if empty.
  852.         ExitApp
  853.     }
  854.    
  855.     Gui Cancel
  856.    
  857.     ; Use cmd.exe to work around the fact that AutoHotkey.exe is locked
  858.     ; while it is still running.  Having a second instance of the script
  859.     ; terminate this instance should be more reliable than performing
  860.     ; an arbitrary wait (e.g. by calling "ping").
  861.     Run %ComSpec% /c "
  862.    (Join`s&`s
  863.    AutoHotkey.exe "%A_ScriptFullPath%" /kill %A_ScriptHwnd%
  864.    del Installer.ahk
  865.    del AutoHotkey.exe
  866.    cd %A_Temp%
  867.    rmdir "%CurrentPath%"
  868.    )",, Hide
  869. }
  870.  
  871.  
  872. /*  Installation
  873.  */
  874.  
  875. _Install(opt) {
  876.     global
  877.    
  878.     /*  Validation
  879.      */
  880.    
  881.     local exefile, binfile
  882.     if opt.type = "Unicode" {
  883.         exefile := "AutoHotkeyU32.exe"
  884.         binfile := "Unicode 32-bit.bin"
  885.     } else if opt.type = "x64" && A_Is64bitOS {
  886.         exefile := "AutoHotkeyU64.exe"
  887.         binfile := "Unicode 64-bit.bin"
  888.     } else if opt.type = "ANSI" {
  889.         exefile := "AutoHotkeyA32.exe"
  890.         binfile := "ANSI 32-bit.bin"
  891.     } else
  892.         ErrorExit("Invalid installation type '" opt.type "'")
  893.    
  894.     if !InStr(FileExist(opt.path), "D")
  895.         try
  896.             FileCreateDir % opt.path
  897.         catch
  898.             ErrorExit("Unable to create installation directory ('" opt.path "')")
  899.    
  900.     CloseScriptsEtc(CurrentPath, "installation")
  901.    
  902.     /*  Preparation
  903.      */
  904.    
  905.     SetWorkingDir % opt.path
  906.    
  907.     switchPage("wait")
  908.    
  909.     ; Remove old files which are no longer relevant.
  910.     if (CurrentVersion <= "1.0.48.05") {
  911.         FileDelete Compiler\README.txt
  912.         FileDelete Compiler\upx.exe
  913.     }
  914.     FileDelete uninst.exe
  915.    
  916.     if A_Is64bitOS {
  917.         ; For xx-bit installs, write to the xx-bit view of the registry.
  918.         local regView := (opt.type = "x64") ? 64 : 32
  919.         if (CurrentRegView && CurrentRegView != regView) {
  920.             ; Clean up old keys in the other registry view.
  921.             SetRegView % CurrentRegView
  922.             RegDelete HKLM, %UninstallKey%
  923.             RegDelete HKLM, %AutoHotkeyKey%
  924.             RegDelete HKLM, SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AutoHotkey.exe
  925.             RegDelete HKLM, SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Ahk2Exe.exe
  926.         }
  927.         SetRegView % regView
  928.     }
  929.    
  930.     /*  Install Files
  931.      */
  932.    
  933.     UpdateStatus("Copying files")
  934.    
  935.     ; If the following is "true", we have no source files to install,
  936.     ; but we may have settings to change.  This includes replacing the
  937.     ; binary files with %exefile% and %binfile%.
  938.     installInPlace := (A_WorkingDir = A_ScriptDir)
  939.    
  940.     ; Install all unique files.
  941.     if !installInPlace {
  942.         InstallMainFiles()
  943.         if opt.ahk2exe
  944.             InstallCompilerFiles()
  945.     }
  946.    
  947.     ; If the user deselected Ahk2Exe and it was previously installed,
  948.     ; ensure it is removed.
  949.     if !opt.ahk2exe
  950.         RemoveCompiler()
  951.    
  952.     ; Create the "default" binaries, corresponding to whichever version
  953.     ; the user selected.
  954.     if !installInPlace
  955.         InstallFile(exefile, "AutoHotkey.exe")
  956.     ;else: a workaround is needed later.
  957.     if opt.ahk2exe
  958.         InstallFile("Compiler\" binfile, "Compiler\AutoHotkeySC.bin")
  959.    
  960.     /*  Start Menu Shortcuts
  961.      */
  962.    
  963.     if CurrentStartMenu
  964.         FileRemoveDir %A_ProgramsCommon%\%CurrentStartMenu%, 1
  965.    
  966.     if opt.menu {
  967.         UpdateStatus("Creating shortcuts")
  968.         local smpath := A_ProgramsCommon "\" opt.menu
  969.         FileCreateDir %smpath%
  970.         FileCreateShortcut %A_WorkingDir%\AutoHotkey.exe, %smpath%\AutoHotkey.lnk
  971.         FileCreateShortcut %A_WorkingDir%\AU3_Spy.exe, %smpath%\AutoIt3 Window Spy.lnk
  972.         FileCreateShortcut %A_WorkingDir%\AutoHotkey.chm, %smpath%\AutoHotkey Help File.lnk
  973.         IniWrite %ProductWebsite%, %ProductName% Website.url, InternetShortcut, URL
  974.         FileCreateShortcut %A_WorkingDir%\%ProductName% Website.url, %smpath%\Website.lnk
  975.         FileCreateShortcut %A_WorkingDir%\Installer.ahk, %smpath%\AutoHotkey Setup.lnk
  976.             ,,,, %A_WinDir%\System32\appwiz.cpl,, -1499
  977.         if opt.ahk2exe
  978.             FileCreateShortcut %A_WorkingDir%\Compiler\Ahk2Exe.exe
  979.                 , %smpath%\Convert .ahk to .exe.lnk
  980.     }
  981.    
  982.     /*  Registry
  983.      */
  984.    
  985.     UpdateStatus("Configuring registry")
  986.    
  987.     RegWrite REG_SZ, HKLM, %AutoHotkeyKey%, InstallDir, %A_WorkingDir%
  988.     RegWrite REG_SZ, HKLM, %AutoHotkeyKey%, Version, %ProductVersion%
  989.     if opt.menu
  990.         RegWrite REG_SZ, HKLM, %AutoHotkeyKey%, StartMenuFolder, % opt.menu
  991.     else
  992.         RegDelete HKLM, %AutoHotkeyKey%, StartMenuFolder
  993.    
  994.     ; Might need to get rid of this to allow the ShellNew template to work:
  995.     RegDelete HKCR, ahk_auto_file
  996.     RegWrite REG_SZ, HKCR, .ahk,, %FileTypeKey%
  997.     RegWrite REG_SZ, HKCR, .ahk\ShellNew, FileName, Template.ahk
  998.    
  999.     RegWrite REG_SZ, HKCR, %FileTypeKey%,, AutoHotkey Script
  1000.     RegWrite REG_SZ, HKCR, %FileTypeKey%\DefaultIcon,, %A_WorkingDir%\AutoHotkey.exe`,1
  1001.    
  1002.     ; Set up system verbs:
  1003.     RegWrite REG_SZ, HKCR, %FileTypeKey%\Shell\Open,, Run Script
  1004.     RegWrite REG_SZ, HKCR, %FileTypeKey%\Shell\Edit,, Edit Script
  1005.     if opt.ahk2exe
  1006.         RegWrite REG_SZ, HKCR, %FileTypeKey%\Shell\Compile,, Compile Script
  1007.    
  1008.     local value
  1009.    
  1010.     ; Set default action, but don't overwrite.
  1011.     try
  1012.         RegRead value, HKCR, %FileTypeKey%\Shell,
  1013.     catch   ; Key likely doesn't exist.
  1014.         RegWrite REG_SZ, HKCR, %FileTypeKey%\Shell,, Open
  1015.    
  1016.     ; Set editor, but don't overwrite.
  1017.     try
  1018.         RegRead value, HKCR, %FileTypeKey%\Shell\Edit\Command,
  1019.     catch
  1020.         RegWrite REG_SZ, HKCR, %FileTypeKey%\Shell\Edit\Command,, notepad.exe `%1
  1021.    
  1022.     if opt.ahk2exe
  1023.         RegWrite REG_SZ, HKCR, %FileTypeKey%\Shell\Compile\Command,, "%A_WorkingDir%\Compiler\Ahk2Exe.exe" /in "`%l"
  1024.    
  1025.     local cmd
  1026.     cmd = "%A_WorkingDir%\AutoHotkey.exe"
  1027.     if opt.utf8
  1028.         cmd = %cmd% /CP65001
  1029.     cmd = %cmd% "`%1" `%*
  1030.     RegWrite REG_SZ, HKCR, %FileTypeKey%\Shell\Open\Command,, %cmd%
  1031.    
  1032.     ; If UAC is enabled, add a "Run as administrator" option.
  1033.     RegRead value, HKLM, SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System, EnableLUA
  1034.     if value
  1035.         RegWrite REG_SZ, HKCR, %FileTypeKey%\Shell\RunAs\Command,, "%A_WorkingDir%\AutoHotkey.exe" "`%1" `%*
  1036.    
  1037.     if opt.dragdrop
  1038.         RegWrite REG_SZ, HKCR, %FileTypeKey%\ShellEx\DropHandler,, {86C86720-42A0-1069-A2E8-08002B30309D}
  1039.     else
  1040.         RegDelete HKCR, %FileTypeKey%\ShellEx
  1041.    
  1042.     RegWrite REG_SZ, HKLM, SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AutoHotkey.exe,, %A_WorkingDir%\AutoHotkey.exe
  1043.     if opt.ahk2exe
  1044.         RegWrite REG_SZ, HKLM, SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Ahk2Exe.exe,, %A_WorkingDir%\Compiler\Ahk2Exe.exe
  1045.    
  1046.     RegDelete HKCR, Applications\AutoHotkey.exe
  1047.     if opt.isHostApp
  1048.         RegWrite REG_SZ, HKCR, Applications\AutoHotkey.exe, IsHostApp
  1049.    
  1050.     ; Write uninstaller info.
  1051.     RegWrite REG_SZ, HKLM, %UninstallKey%, DisplayName, %ProductName% %ProductVersion%
  1052.     RegWrite REG_SZ, HKLM, %UninstallKey%, UninstallString, "%A_WorkingDir%\AutoHotkey.exe" "%A_WorkingDir%\Installer.ahk"
  1053.     RegWrite REG_SZ, HKLM, %UninstallKey%, DisplayIcon, %A_WorkingDir%\AutoHotkey.exe
  1054.     RegWrite REG_SZ, HKLM, %UninstallKey%, DisplayVersion, %ProductVersion%
  1055.     RegWrite REG_SZ, HKLM, %UninstallKey%, URLInfoAbout, %ProductWebsite%
  1056.     RegWrite REG_SZ, HKLM, %UninstallKey%, Publisher, %ProductPublisher%
  1057.     RegWrite REG_SZ, HKLM, %UninstallKey%, NoModify, 1
  1058.    
  1059.     ; Notify other programs (e.g. explorer.exe) that file type associations have changed.
  1060.     ; This may be necessary to update the icon when upgrading from an older version of AHK.
  1061.     DllCall("shell32\SHChangeNotify", "uint", 0x08000000, "uint", 0, "int", 0, "int", 0) ; SHCNE_ASSOCCHANGED
  1062.    
  1063.     if installInPlace {
  1064.         ; As AutoHotkey.exe is probably in use by this script, the final
  1065.         ; step will be completed by another instance of this script:
  1066.         Run AutoHotkeyU32.exe "%A_ScriptFullPath%" /fin %exefile% %A_ScriptHwnd% %SilentMode%
  1067.         ExitApp
  1068.     }
  1069.    
  1070.     switchPage("done")
  1071. }
  1072.  
  1073. InstallFile(file, target="") {
  1074.     global
  1075.     if (target = "")
  1076.         target := file
  1077.     Loop { ; Retry loop.
  1078.         try {
  1079.             FileCopy %SourceDir%\%file%, %target%, 1
  1080.             ; If successful (no exception thrown):
  1081.             return
  1082.         }
  1083.         if SilentMode {
  1084.             SilentErrors += 1
  1085.             return  ; Continue anyway.
  1086.         }
  1087.         local error_message := RTrim(GetErrorMessage(), "`r`n")
  1088.         MsgBox 0x12, AutoHotkey Setup,
  1089.         (LTrim
  1090.         Error installing file "%target%"
  1091.        
  1092.         Specifically: %error_message%
  1093.        
  1094.         Click Abort to stop the installation,
  1095.         Retry to try again, or
  1096.         Ignore to skip this file.
  1097.         )
  1098.         IfMsgBox Abort
  1099.             ExitApp
  1100.         IfMsgBox Ignore
  1101.             return
  1102.     }
  1103. }
  1104.  
  1105. InstallMainFiles() {
  1106.     InstallFile("AutoHotkeyU32.exe")
  1107.     InstallFile("AutoHotkeyA32.exe")
  1108.     if A_Is64bitOS
  1109.         InstallFile("AutoHotkeyU64.exe")
  1110.    
  1111.     InstallFile("AU3_Spy.exe")
  1112.     InstallFile("AutoHotkey.chm")
  1113.     InstallFile("license.txt")
  1114.    
  1115.     InstallFile("Installer.ahk")
  1116.    
  1117.     if !FileExist(A_WinDir "\ShellNew\Template.ahk") {
  1118.         FileCreateDir %A_WinDir%\ShellNew
  1119.         InstallFile("Template.ahk", A_WinDir "\ShellNew\Template.ahk")
  1120.     }
  1121. }
  1122.  
  1123. InstallCompilerFiles() {
  1124.     FileCreateDir Compiler
  1125.     InstallFile("Compiler\Ahk2Exe.exe")
  1126.     InstallFile("Compiler\ANSI 32-bit.bin")
  1127.     InstallFile("Compiler\Unicode 32-bit.bin")
  1128.     ; Install the following file even if !isOS64bit() to support
  1129.     ; compiling scripts for 64-bit systems on 32-bit systems:
  1130.     InstallFile("Compiler\Unicode 64-bit.bin")
  1131. }
  1132.  
  1133. RemoveCompiler() {
  1134.     global
  1135.     FileDelete Compiler\Ahk2Exe.exe
  1136.     FileDelete Compiler\ANSI 32-bit.bin
  1137.     FileDelete Compiler\Unicode 32-bit.bin
  1138.     FileDelete Compiler\Unicode 64-bit.bin
  1139.     FileDelete Compiler\AutoHotkeySC.bin
  1140.     FileRemoveDir Compiler  ; Only if empty.    
  1141.     RegDelete HKLM, SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Ahk2Exe.exe
  1142. }
  1143.  
  1144. DefineUI:
  1145. html=
  1146. (%`
  1147. <html><head>
  1148. <style type="text/css">
  1149. body {
  1150.     background-color: ButtonFace;
  1151.     font-family: "Arial", sans-serif;
  1152.     font-size: 15px;
  1153.     overflow: hidden;
  1154.     cursor: default;
  1155.     padding: 0;
  1156.     margin: 0;
  1157. }
  1158. h1 {
  1159.     font-size: 37px;
  1160.     font-weight: normal;
  1161.     color: #405871;
  1162.     background-color: white;
  1163.     padding: 15px 25px;
  1164.     margin: 0 -15px;
  1165. }
  1166. p {
  1167.     margin-top: 15px;
  1168. }
  1169. a:link, a:visited {
  1170.     color: #0066CC;
  1171. }
  1172. .options {
  1173.     margin: 0 3em 0 2em;
  1174. }
  1175. .options a {
  1176.     display: block;
  1177.     padding: 0.8em 1em;
  1178.     margin: 0.3em 0;
  1179.     position: relative;
  1180.     width: 100%;
  1181. }
  1182. .marker {
  1183.     position: absolute;
  1184.     right: 10px;
  1185.     font-size: 25px;
  1186.     top: 50%;
  1187.     margin-top: -17px;
  1188. }
  1189. a.button,
  1190. .options a {
  1191.     cursor: hand;
  1192.     border: 1px solid ButtonShadow;
  1193.     background-color: white;
  1194.     text-decoration: none;
  1195. }
  1196. a.button {
  1197.     padding: .3em .5em;
  1198. }
  1199. a.button,
  1200. a.button:visited,
  1201. .options a,
  1202. .options a:visited {
  1203.     color: #405871;
  1204. }
  1205. a.button:hover,
  1206. a.button:active,
  1207. .options a:hover,
  1208. .options a:active {
  1209.     background-color: #F8F8FF;
  1210.     border-color: #4774B2;
  1211.     color: #4774B2;
  1212. }
  1213. .options a:active {
  1214.     left: 3px;
  1215. }
  1216. .options p {
  1217.     font-size: 80%;
  1218.     margin: 0.2em 1em;
  1219. }
  1220. #license, #extract {
  1221.     position: absolute;
  1222.     bottom: 1em;
  1223.     font-size: 80%;
  1224. }
  1225. #extract {
  1226.     right: 1em;
  1227. }
  1228. .page {
  1229.     width: 100%;
  1230.     height: 100%;
  1231.     padding: 0 15px;
  1232.     display: none; /* overridden by script */
  1233.     color: ButtonText;
  1234. }
  1235. .pager .page {
  1236.     padding: 15px 0;
  1237. }
  1238. .nav {
  1239.     background-color: #405871;
  1240.     color: white;
  1241.     margin: 0 -15px;
  1242.     padding: 3px 20px;
  1243.     height: 24px;
  1244. }
  1245. .nav a, .nav a:visited {
  1246.     color: #ddd;
  1247.     text-decoration: none;
  1248.     padding: 0;
  1249. }
  1250. .nav a:hover {
  1251.     color: white;
  1252. }
  1253. .nav .current {
  1254.     color: white;
  1255.     font-weight: bold;
  1256.     cursor: default;
  1257. }
  1258. .warning {
  1259.     display: none;
  1260.     background-color: #fee;
  1261.     color: #800;
  1262.     border: 1px solid #800;
  1263.     padding: 0.5em;
  1264.     margin: -0.5em 0 1em;
  1265. }
  1266. .textbox {
  1267.     border: 1px solid ButtonShadow;
  1268.     padding: 0 0.4em;
  1269.     height: 29px;
  1270.     line-height: 27px;
  1271.     vertical-align: top;
  1272.     margin-top: 9px;
  1273. }
  1274. #installdir, #startmenu {
  1275.     width: 70%;
  1276. }
  1277. #installdir_browse, #startmenu_del {
  1278.     height: 28px;
  1279.     margin-top: 10px;
  1280. }
  1281. label {
  1282.     padding: 5px;
  1283.     display: block;
  1284.     width: 90%;
  1285. }
  1286. label.indent {
  1287.     padding-left: 30px;
  1288. }
  1289. label p {
  1290.     font-size: 85%;
  1291.     margin: .3em 25px;
  1292.     color: #405871;
  1293. }
  1294. #install_button, #next-button {
  1295.     position: absolute;
  1296.     bottom: 15px;
  1297.     right: 15px;
  1298.     width: 5em;
  1299.     font-size: 125%;
  1300.     text-align: center;
  1301. }
  1302. #installcompilernote {
  1303.     display: none;
  1304.     font-weight: bold;
  1305. }
  1306. </style>
  1307. <script type="text/javascript">
  1308. function forEach(arr, fn) {
  1309.     var i;
  1310.     for (i = 0; i < arr.length; ++i)
  1311.         fn.apply(arr[i]);
  1312. }
  1313. function onload() {
  1314.     ci_nav_list.length = 0;
  1315.     forEach (ci_nav.getElementsByTagName("a"), function() {
  1316.         this.tabIndex = 1000;
  1317.         if (this.hash != "") {
  1318.             var list = this.parentNode == ci_nav_list ? ci_nav_list : null;
  1319.             if (list)
  1320.                 list[list.length++] = this;
  1321.             this.onclick = function() {
  1322.                 if (list) {
  1323.                     forEach (list.getElementsByTagName("a"), function() {
  1324.                         this.className = "";
  1325.                     })
  1326.                     this.className = "current";
  1327.                 }
  1328.                 event.returnValue = switchPage(this.hash.substr(1));
  1329.             }
  1330.         }
  1331.     })
  1332. }
  1333. function initOptions(curName, curVer, curType, newVer, instDir, smFolder, defType, is64) {
  1334.     if (onload) onload(), onload = null;
  1335.     var opt;
  1336.     var warn;
  1337.     var types = {Unicode: "Unicode 32-bit", ANSI: "ANSI 32-bit", x64: "Unicode 64-bit"};
  1338.     var curTypeName = types[curType];
  1339.     var defTypeName = types[defType];
  1340.     curTypeName = curTypeName ? " (" + curTypeName + ")" : "";
  1341.     if (curName == "AutoHotkey" && curVer <= "1.0.48.05") {
  1342.         start_intro.innerText = curName + " v" + curVer + " is installed. What do you want to do?";
  1343.         var uniType = is64 ? "x64" : "Unicode";
  1344.         var uniTypeName = types[uniType];
  1345.         opt = [
  1346.             "ahk://Upgrade/ANSI", "Upgrade to v" + newVer + " (" + types.ANSI + ")", "Recommended for compatibility.",
  1347.             "ahk://Upgrade/" + uniType, "Upgrade to v" + newVer + " (" + uniTypeName + ")", "",
  1348.             "ahk://Customize/", "Custom Installation", ""
  1349.         ];
  1350.         warn = '<strong>Note:</strong> Some AutoHotkey 1.0 scripts are <a href="ahk://ViewHelp//docs/Compat.htm">not compatible</a> with AutoHotkey 1.1.';
  1351.     } else if (curName == "") {
  1352.         start_intro.innerText = "Please select the type of installation you wish to perform.";
  1353.         opt = [
  1354.             "ahk://QuickInstall/", "Express Installation", "Default version: " + defTypeName + "<br>Install in: " + instDir,
  1355.             "ahk://Customize/", "Custom Installation", ""
  1356.         ];
  1357.     } else if (curVer != newVer) {
  1358.         start_intro.innerText = curName + " v" + curVer + curTypeName + " is installed. What do you want to do?";
  1359.         opt = [
  1360.             "ahk://Upgrade/" + defType, (curVer < newVer ? "Upgrade" : "Downgrade") + " to v" + newVer + " (" + defTypeName + ")", "",
  1361.             "ahk://Customize/", "Custom Installation", ""
  1362.         ];
  1363.     } else {
  1364.         start_intro.innerText = curName + " v" + curVer + curTypeName + " is installed. What do you want to do?";
  1365.         opt = [
  1366.             "ahk://QuickInstall/", "Repair", "",
  1367.             "ahk://Customize/", "Modify", "",
  1368.             "ahk://Uninstall/", "Uninstall", ""
  1369.         ];
  1370.     }
  1371.     var i, html = [];
  1372.     for (i = 0; i < opt.length; i += 3) {
  1373.         html.push('<a href="', opt[i], '" id="opt', Math.floor(i/3)+1, '"><span>', opt[i+1], '</span>');
  1374.         if (opt[i+2])
  1375.             html.push('<p>', opt[i+2], '</p>');
  1376.         if (opt[i] == 'ahk://Customize/')
  1377.             html.push('<div class="marker">\u00BB</div>');
  1378.         html.push('</a>');
  1379.     }
  1380.     start_options.innerHTML = html.join("");
  1381.     start_warning.innerHTML = warn;
  1382.     start_warning.style.display = warn ? "block" : "none";
  1383.     start_nav.innerHTML = '<em style="text-align:right;width:100%">version ' + newVer + '</em>';
  1384.     installtype.value = defType;
  1385.     installdir.value = instDir;
  1386.     startmenu.value = smFolder;
  1387.     startmenu.onblur();
  1388.     forEach (document.getElementsByTagName("a"), function() {
  1389.         if (/*this.className == "button" ||*/ this.parentNode.className == "options")
  1390.             this.hideFocus = true;
  1391.     })
  1392. }
  1393. document.onselectstart =
  1394. document.oncontextmenu =
  1395. document.ondragstart =
  1396.     function() {
  1397.         return window.event && event.srcElement.tagName == "INPUT" || false;
  1398.     };
  1399. function setInstallType(type) {
  1400.     installtype.value = type;
  1401.     ci_nav_list[1].click();
  1402.     event.returnValue = false;
  1403. }
  1404. function switchPage(page) {
  1405.     page = document.getElementById(page);
  1406.     if (page.id == "start")
  1407.         ci_nav_list[0].click();
  1408.     for (var n = page.parentNode.firstChild; n; n = n.nextSibling) if (n.className == "page") {
  1409.         if (n != page)
  1410.             n.style.display = "none";
  1411.         else
  1412.             n.style.display = "block";
  1413.     }
  1414.     var f;
  1415.     switch (page.id) {
  1416.     case "custom-install":
  1417.     case "ci_version":  f = "it_" + installtype.value; break;
  1418.     case "ci_location": f = "next-button"; break;
  1419.     case "ci_options":  f = "install_button"; break;
  1420.     case "done":        f = "done_exit"; break;
  1421.     }
  1422.     if (f) {
  1423.         // If page == ci_version, it mightn't actually be visible at this point,
  1424.         // which causes IE7 (and perhaps older) to throw error 0x80020101.
  1425.         try { document.getElementById(f).focus() } catch (ex) { }
  1426.     }
  1427.     return false;
  1428. }
  1429. function beforeCustomInstall() {
  1430.     if (startmenu.style.color == '#888')
  1431.         startmenu.value = '';
  1432. }</script>
  1433. </head><body>
  1434.  
  1435. <div class="page" id="start">
  1436.     <h1>AutoHotkey Setup</h1>
  1437.     <div class="nav" id="start_nav">&nbsp;</div>
  1438.     <p id="start_intro"></p>
  1439.     <div class="warning" id="start_warning"></div>
  1440.     <div class="options" id="start_options"></div>
  1441.     <div id="license">AutoHotkey is open source software: <a href="ahk://ReadLicense/">read license</a></div>
  1442.   <div id="extract"><a href="ahk://Extract/" title="Save program files without installing.">extract to...</a></div>
  1443. </div>
  1444.  
  1445. <div class="page" id="custom-install">
  1446.     <h1>AutoHotkey Setup</h1>
  1447.     <div class="nav" id="ci_nav">
  1448.         <a href="#start">start</a> &#187;
  1449.         <span id="ci_nav_list">
  1450.             <a href="#ci_version">version</a> &#187;
  1451.             <a href="#ci_location">location</a> &#187;
  1452.             <a href="#ci_options">options</a> &#187;
  1453.         </span>
  1454.         <a id="ci_nav_install" href="ahk://CustomInstall/" onclick="beforeCustomInstall()">install</a>
  1455.     </div>
  1456.     <div class="pager" id="ci_pager">
  1457.         <div class="page" id="ci_version">
  1458.             <p>Which version of AutoHotkey.exe should run by default?</p>
  1459.             <input type="hidden" id="installtype">
  1460.             <div class="options">
  1461.                 <a href="#" id="it_Unicode" onclick="setInstallType('Unicode')" tabindex="1">Unicode 32-bit
  1462.                     <p>Recommended for new installations/scripts.</p> <div class="marker">&#187;</div></a>
  1463.                 <a href="#" id="it_x64" onclick="setInstallType('x64')" tabindex="2">Unicode 64-bit
  1464.                     <p>Faster, but compiled scripts won't run on 32-bit systems.</p> <div class="marker">&#187;</div></a>
  1465.                 <a href="#" id="it_ANSI" onclick="setInstallType('ANSI')" tabindex="3">ANSI 32-bit
  1466.                     <p>Better compatibility with some legacy scripts.</p> <div class="marker">&#187;</div></a>
  1467.             </div>
  1468.         </div>
  1469.         <div class="page" id="ci_location">
  1470.             <label for="installdir" class="indent">Install location:<br>
  1471.             <input type="text" class="textbox" id="installdir" value="C:\Program Files\AutoHotkey" tabindex="11"> <a href="ahk://SelectFolder/installdir,Select the folder to install AutoHotkey in." id="installdir_browse" class="button" tabindex="12">Browse</a></label><br>
  1472.             <label for="startmenu" class="indent"> Create shortcuts in the following Start menu folder:<br>
  1473.             <input type="text" class="textbox" id="startmenu" value="AutoHotkey" tabindex="13"
  1474.                 onfocus="if (style.color == '#888') value='', style.color = '';"
  1475.                 onblur="if (value == '') value = '(don\'t create shortcuts)', style.color = '#888';">
  1476.             <a href="#" id="startmenu_del" class="button" style="color:red" tabindex="14"
  1477.                 onclick = "startmenu.value=''; startmenu.onblur(); return false;">X</a>
  1478.             </label><br>
  1479.             <a href="#" id="next-button" class="button" onclick="ci_nav_list[2].click(); return false;" tabindex="15">Next</a>
  1480.         </div>
  1481.         <div class="page" id="ci_options">
  1482.             <label for="installcompiler"><input type="checkbox" id="installcompiler" checked="checked"> Install script compiler
  1483.                 <p>Installs Ahk2Exe, a tool to convert any .ahk script into a stand-alone EXE.<br>
  1484.                 Also adds a "Compile" option to .ahk context menus.</p>
  1485.                 <p id="installcompilernote">Download and re-run the installer to reinstall Ahk2Exe.</p></label>
  1486.             <label for="enabledragdrop"><input type="checkbox" id="enabledragdrop" checked="checked"> Enable drag &amp; drop
  1487.                 <p>Files dropped onto a .ahk script will launch that script (the files will be passed as parameters).  This can lead to accidental launching so some users may wish to disable it.</p></label>
  1488.             <label for="separatebuttons"><input type="checkbox" id="separatebuttons"> Separate taskbar buttons
  1489.                 <p>Causes each script which has visible windows to be treated as a separate program, but prevents AutoHotkey.exe from being pinned to the taskbar.</p></label>
  1490.             <a href="ahk://CustomInstall/" onclick="beforeCustomInstall()" id="install_button" class="button">Install</a>
  1491.         </div>
  1492.     </div>
  1493. </div>
  1494.  
  1495. <div class="page" id="wait">
  1496.     <h1>AutoHotkey Setup</h1>
  1497.     <div class="nav">&nbsp;</div>
  1498.     <p style="color: #999; font-size: 120%; text-align: center; margin-top: 5em">this shouldn't take long...</p>
  1499.     <p style="text-align: center" id="install_status"></p>
  1500. </div>
  1501.  
  1502. <div class="page" id="done">
  1503.     <h1>AutoHotkey Setup</h1>
  1504.     <div class="nav">&nbsp;</div>
  1505.     <p>Installation complete.</p>
  1506.     <div class="options">
  1507.         <a href="ahk://ViewHelp//docs/AHKL_ChangeLog.htm">View Changes &amp; New Features</a>
  1508.         <a href="ahk://ViewHelp//docs/Tutorial.htm">View the Tutorial</a>
  1509.         <a href="ahk://RunAutoHotkey/">Run AutoHotkey</a>
  1510.         <a href="ahk://Quit/" id="done_exit">Exit</a>
  1511.     </div>
  1512. </div>
  1513.  
  1514. </body></html>
  1515. )
  1516. return
Advertisement
Add Comment
Please, Sign In to add comment