rictirse 發表於 2012-9-23 12:04

Micro Duo教室之AutoIt 自動安裝開班授課 第一課

本帖最後由 rictirse 於 2014-12-24 08:28 編輯

第一課的教材 這邊使用 FileZilla_3.5.3_win32-setup.exe 來做演示 載點:點我任意門

AutoIt的版本是 3.3.8.1,User Options 說明檔案的載點這邊都有,還沒安裝的快點載吧 點我任意門

1.新增一個AutoIt Script


2.重新命名後,雙擊使用Script


3.我們的第一個指令是

ShellExecute ( "檔案名稱" [, "程式參數" [, "工作目錄" [, "動態參數(verb)" [, 顯示_旗標]]]] )

開發軟體 IDE(Integrated Development Environment) 有內建Keyword 的功能

所以只要輸入前兩個關鍵字後用↑↓可以選你要的指令,選定後按Tab就可以Insert




在指令 ShellExecute (這裡面插入引數)
這邊使用 FileZilla_3.5.3_win32-setup.exe 當教學
ShellExecute ("FileZilla_3.5.3_win32-setup.exe")4.完成後按下F5 可以看結果,如果您輸入正確的 函式 與 引數會啟動 FileZilla Setup的 頁面



如果出現這樣,恭喜你,你的第一個AutoIt 函式就已經成功嘍

5.每一台電腦執行速度不同,所以這邊要寫一個等待視窗出現的指令

WinWaitActive ( "標題" [, "文字" [, 逾時]] )

標題與文字可以直接使用AutoIt 內建的小工具Au3Info來查詢

WinWaitActive ( "FileZilla Client 3.5.3 Setup", "Please review the license terms before installing FileZilla Client 3.5.3.")請注意,一些軟體的標題會隨頁面不同而改變
拿FileZilla來講

雖然兩個視窗只差一個空白,但是在AutoIt上辨識是截然不同的視窗

所以要非常注意這種小地方,不然程式會莫名其妙的卡住

接著自動安裝最基本的方法是用快速鍵來達成

這邊觀察一下 FileZilla Focus在 I agree 這個 Button上

然後 I agree 上面 a 有底線,代表他的Default hotkey是 a

這時候只要按下 Alt + a 這個快速鍵,就會到下一個頁面

6.這邊用Send 函式,來模擬Alt + a

Send ( "按鍵" [, 旗標] ) Send ("!a")Send ("!a") 系統會判斷為 Alt + a 但是 Send ("!A") 系統會判斷為 Alt + A

兩者是截然不同的東西喔,請注意,一般 hotkey 是用小寫來撰寫

**按鍵的搭配幫助文件內有詳細的介紹這邊不多說明**

7.接下來再按F5 測試一下是不是可以自動進入下一個頁面呢?
如果不行檢查看看跟噹噹寫的差在哪,看看是不是標題的地方空白卡住了呢?ShellExecute ("FileZilla_3.5.3_win32-setup.exe")
WinWaitActive ( "FileZilla Client 3.5.3 Setup", "Please review the license terms before installing FileZilla Client 3.5.3.")
Send ("!a")8.剩下的就當作業出給各位完成,寫好後記得按F5 確定一下是不是可以正常執行喔!#cs ----------------------------------------------------------------------------

AutoIt Version: 3.3.8.1
Author:      Dang Wang

Script Function: FileZilla_Install
      Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

ShellExecute ("FileZilla_3.5.3_win32-setup.exe")

WinWaitActive ( "FileZilla Client 3.5.3 Setup", "Please review the license terms before installing FileZilla Client 3.5.3.")
Send ("!a")

WinWaitActive ( "FileZilla Client 3.5.3 Setup", "Who should this application be installed for?")
Send ("!n")

WinWaitActive ( "FileZilla Client 3.5.3 Setup", "Choose which features of FileZilla Client 3.5.3 you want to install.")
Send ("!n")

WinWaitActive ( "FileZilla Client 3.5.3 Setup", "Choose the folder in which to install FileZilla Client 3.5.3.")
Send ("!n")

WinWaitActive ( "FileZilla Client 3.5.3 Setup", "Choose a Start Menu folder for the FileZilla Client 3.5.3 shortcuts.")
Send ("!i")

WinWaitActive ( "FileZilla Client 3.5.3 Setup", "FileZilla Client 3.5.3 has been installed on your computer.")
Send ("!f")

gary8349 發表於 2013-2-15 09:04

本帖最後由 gary8349 於 2013-2-15 17:09 編輯

~~~補上遺失的樓層~~~

bee:
================================================================請問版主:
執行時停於 line 28: Send ("!i")
等待按 install 是否正確?

以後 的 finish 會正常執行,
而且自動開啟檔案!================================================================
rictirse: Re:bee
================================================================不太懂你的意思 WinWaitActive ( "FileZilla Client 3.5.3 Setup","Choose a Start Menu folder for the FileZilla Client 3.5.3shortcuts.");這個是等待 視窗出現的指令Send ("!i") ;當 上面的視窗出現以後 才會輸入 alt + i 的這個 快速鍵指令 有問題再提出來吧~~================================================================
ken670128: Re:bee
================================================================Send 命令,只要視窗一脫離現用視窗(焦點)或者系統較繁忙時,就會變成沒按到而卡在那邊
即便你沒有自己動到鍵盤滑鼠,但一些背景程式有時會影響到,解決方式可用 Do...Until 或視窗元件控制命令( Controlxxxxxx...等 )

自動開啟檔案 & 安裝廣告元件 <== 這通常在自動安裝時要取消核取,可利用 ControlCommand 命令,用法請參考 Autoit 本身內建的說明
Autoit 本身內建的說明非常完整,當初我學習 Autoit時沒有像現在有那麼多資訊可取得,更加別說有中文資訊了,完全是靠內建說明一步一步學起來的

先提醒一下,基本上FileZilla_3.5.3_win32-setup.exe 有自動安裝的參數 /S,可以不需使用 Autoit 來寫自動安裝,不過可以拿來作為學習範例

剛剛特地去翻很久以前,我有用 Autoit 來自動安裝軟體時所燒錄的光碟片...
以下拿 Alcohol 120% v2.0.0.1331 作為範例,沒有新版的,因為現在沒有在用Autoit 做自動安裝
雖然這個源碼是以 Alcohol 120% 來撰寫,但當時我都把這架構視為模版,稍作修改就直接套用在別的軟體上Opt("TrayIconHide", 1)

環境設定
;====================================================================================================================================

; 安裝檔名稱
$InstFile = "Alcohol120_retail_2.0.0.1331.exe"

; 安裝檔標題
$InstTitle = "Alcohol 120% 2.0.0.1331"
$PatchTitle = "Setup - PatCh Alcohol 120% 1.9.8.x - 2.0.0.x Retail"

; 開始功能表名稱
$SMenuName = "Alcohol 120%"

; 程式分類目錄
$Catagory = "工具程式\光碟工具\光碟應用工具"

; 登錄檔預先設定
Local $hklmkey = "HKLM\Software\Alcohol Soft\Alcohol 120%"
Local $hkcukey = "HKCU\Software\Alcohol Soft\Alcohol 120%"
RegWrite($hklmkey & "\AddRemove\Components\SecStarWind", "Installed", "REG_DWORD", "0")

; 執行腳本
;====================================================================================================================================

; 執行安裝程式
Run($InstFile)

; 等待安裝程式的頁面出現
WinWait($InstTitle)

; 移動安裝視窗到畫面外
WinMove($InstTitle, "", -50 , -50 , 0 , 0)

Do
          Select
          ; 等待安裝程式的歡迎頁面出現
                  Case WinExists($InstTitle,"Welcome to the Alcohol 120% Setup Wizard")
                        ; 按下一步
                        ControlClick($InstTitle,"Welcome to the Alcohol 120% Setup Wizard","Button2")

          ; 安裝程式的授權頁面出現
                  case WinExists($InstTitle,"License Agreement")
                        ; 按 "I Agree"
                        ControlClick($InstTitle,"License Agreement","Button2")

          ; 安裝程式的選擇元件頁面出現
                  Case WinExists($InstTitle,"Choose Components")
                        ; 按下一步
                        ControlClick($InstTitle,"Choose Components","Button2")

          ; 安裝程式的安裝路徑頁面出現
                  Case WinExists($InstTitle,"Choose Install Location")
                        ; 取得 Edit1 控制項裏的安裝目錄,並把它存到 $path 變數中
                        $path = ControlGetText($InstTitle,"Choose Install Location","Edit1")
                        ; 按安裝
                        ControlClick($InstTitle,"Choose Install Location","Button2")

          ; 安裝程式的完成頁面出現
                  Case WinExists($InstTitle,"Completing the Alcohol 120% Setup Wizard")
                        ; 按 Finish 完成安裝
                        ControlCommand($InstTitle,"Completing the Alcohol 120% Setup Wizard","Button4","UnCheck","")
                        ControlClick($InstTitle,"Completing the Alcohol 120% Setup Wizard","Button2")
          EndSelect
Until Not WinExists($InstTitle)

; 軟體個別設定
;====================================================================================================================================

; 登錄資訊
RegWrite($hkcukey & "\Options\General", "AutoCheckCurrentVersionViaInternetAtStart", "REG_SZ", "0")
RegDelete($hkcukey & "\Info")
RegDelete("HKCU\Software\StarSynergy")

Sleep(1000)

FileCopy("PatCh5122ML.exe",$path,9)
Run($path & "\" & "PatCh5122ML.exe",$path)

WinWait($PatchTitle)
WinMove($PatchTitle, "", -50 , -50 , 0 , 0)

Do
          Select
          ; 等待安裝程式的歡迎頁面出現
                  Case WinExists($PatchTitle,"Welcome to the PatCh Alcohol 120% 1.9.8.x - 2.0.0.x Retail Setup Wizard")
                        ; 按下一步
                        ControlClick($PatchTitle,"Welcome to the PatCh Alcohol 120% 1.9.8.x - 2.0.0.x Retail Setup Wizard","TNewButton1")
          EndSelect
Until Not WinExists($PatchTitle)

Sleep(2000)

RegWrite($hkcukey & "\Options", "Language", "REG_SZ", "1028")
RegWrite($hkcukey & "\Info", "UserName", "REG_SZ", "User")
RegWrite($hkcukey & "\Info", "Serverkey", "REG_SZ", "ALBM3P4TVV96BM3T8M3T8BCDFDFPQXYC3YBV98YQRBBCDDD6B6B9QP4F6CDPQK2896KFGT82R9FBC6B342RDFT88967BC8P23MPJ9BCBCBCBCBCQR6")
================================================================
bee:
================================================================就是停在我附的畫面上。
要等我 按 install 後才會繼續執行。
但我執行 Lesson 3 時,一鍵完成。
誠如 4樓 ken大 所言,應是 加了 Do...Until。
謝謝 二位回答,
更謝 4F ken大,又提供了一個好範例。================================================================
bee:
================================================================自動安裝-圖示說明:

安裝軟體更新頻繁,避免以後新學習者不容易找到該軟體,
小弟借花獻佛,以圖說輔助說明,其能讓新學習者有所了解!
檔案 FileZilla_3.5.3_win32-setup.exe 到這裡下載
圖顏色說明:自動安裝-第一課 採用紅色框,第二課採用藍色框

圖.1 - licenseterms AgreeWinWaitActive ( "FileZilla Client 3.5.3Setup", "Please review the license terms before installing FileZillaClient 3.5.3.")
Send ("!a")
圖.2 -Choose InstallationWinWaitActive ( "FileZilla Client 3.5.3Setup", "Who should this application be installed for?")
Send ("!n")
自動安裝-第二課ControlCommand ( "FileZilla Client 3.5.3Setup", "Who should this application be installed for?","Button5", "Check", "")
圖.3 -Choose ComponentsWinWaitActive ( "FileZilla Client 3.5.3Setup", "Choose which features of FileZilla Client 3.5.3 you want toinstall.")
Send ("!n")圖.4 - Choose folderWinWaitActive ( "FileZilla Client 3.5.3Setup", "Choose the folder in which to install FileZilla Client3.5.3.")
Send ("!n")
圖.5 -Choose Start MenuWinWaitActive ( "FileZilla Client 3.5.3Setup", "Choose a Start Menu folder for the FileZilla Client 3.5.3shortcuts.")
Send ("!i")
圖.6 -FinishWinWaitActive ( "FileZilla Client 3.5.3Setup", "FileZilla Client 3.5.3 has been installed on yourcomputer.")
Send ("!f")
自動安裝-第二課ControlCommand ("FileZilla Client3.5.3 Setup", "FileZilla Client 3.5.3 has been installed on yourcomputer.", "Button4", "UnCheck", "")
最後留個引:
1. 自動更新每換版本,都要重新修改以上步驟,
    不知有沒有一套軟體,能在安裝前執行,
    紀錄每一安裝步驟後,就自動將程式寫好?
2. 如何取得 title、text 及 圖.2 第二課 Button5 的名稱?================================================================
ken670128:
================================================================有別的可以錄製的工具軟體,也有自動生成 Autoit 源碼的錄製工具,因為我都有看到過,但都沒有使用過,所以無法跟你說好不好用,
因為軟體自動安裝,說真得用不太到 Autoit ,既使要用到,就如我先前所說的使用上面的源碼作模版,簡單修改幾行就可應用在別的
軟體上,實在不需再用額外的軟體去錄製
別的錄製工具軟體可以在 Google 搜看看
錄製 Autoit 源碼的工具可以去 Autoit 中文論壇 的軟件安裝區看看
剛去 IT 天空一看,斗大的標題「 i-Autoit-1.01正式版[自動安裝腳本輔助工具]」,看了一下似乎就是用來錄製用的~~

視窗標題、文字、控件名稱,可用安裝 Autoit 時所附的 AutoIt Window Info 工具來取得
第一課~第二課附的幾張圖中有教如何取得這些資訊的方式================================================================
bee:
================================================================5.每一台電腦執行速度不同,所以這邊要寫一個等待視窗出現的指令
WinWaitActive ( "標題" [, "文字" [, 逾時]] )
標題與文字可以直接使用AutoIt 內建的小工具Au3Info來查詢怪小弟眼拙,沒發現到!
i-Autoit-1.01正式版[自動安裝腳本輔助工具努力研讀!
謝謝 ken大 回覆!==============================================================
rictirse: Re:ken670128:================================================================一樣是 Alcohol1202.0.1.2033 的 install我之前也有寫過一個 英雄所見略同
因為途中需要重新開機,所以分為install_1 跟install_2
Install 2 重開機前$WindowsName = "Alcohol 120% 2.0.1.2033"
$Text_1 = "Click Next to continue."
$Text_2 = "If you accept the terms of the agreement, click I Agree to continue. You must accept the agreement to install Alcohol 120%."
$Text_3 = "Setup must restart Windows and continue installation after reboot."
$Text_4 = "Check the components you want to install and uncheck the components you don't want to install. Click Next to continue."
$Text_5 = "Setup will install Alcohol 120% in the following folder. To install in a different folder, click Browse and select another folder. Click Install to start the installation."
$Text_6 = "Alcohol 120% has been installed on your computer."

AutoItSetOption ( "TrayIconHide", 1)
ShellExecute (@ScriptDir&"\Alcohol120 retail 2.0.1.2033.exe")
FileCreateShortcut ( @ScriptDir&"\Install_2.exe", @StartupCommonDir&"\Install_2.lnk")
FileCopy ( @ScriptDir&"\PatCh513ML.exe", @ProgramFilesDir&"\Alcohol Soft\Alcohol 120\PatCh513ML.exe", 9)
;-----------(安裝開始)-----------
WinWaitActive ( $WindowsName, $Text_1)
ControlClick ( $WindowsName, $Text_1, "Button2")
WinWaitActive ( $WindowsName, $Text_2)
ControlClick ( $WindowsName, $Text_2, "Button2")
ToolTip(@CR&@CR&"         "&$WindowsName&" 一鍵安裝"&@CR&@CR&@CR&"     ***** 系統判讀中請勿移動滑鼠 *****   "&@CR&@CR&@CR &"                   Power By 噹噹 2011/09/22"&@CR,@DesktopWidth/2-189,@DesktopHeight/2-90)
If WinWaitActive ( $WindowsName, $Text_3, 2) Then
          WinActivate ( $WindowsName, $Text_3)
          ControlSetText ( $WindowsName, $Text_3, "Static2", "是否馬上重新開機")
          ToolTip ("")
Else
          ToolTip ("")
          WinWaitActive ( $WindowsName, $Text_4)
          ControlClick ( $WindowsName, $Text_4, "Button2")
          WinWaitActive ( $WindowsName, $Text_5)
          ControlClick ( $WindowsName, $Text_5, "Button2")
          WinWaitActive ( $WindowsName, $Text_6)
          ControlCommand ( $WindowsName, $Text_6, "Button4", "UnCheck", "")
          ControlCommand ( $WindowsName, $Text_6, "Button5", "UnCheck", "")
          ControlClick ( $WindowsName, $Text_6, "Button2")
          ;-----------(破解)-----------
          ShellExecute (@ProgramFilesDir&"\Alcohol Soft\Alcohol 120\PatCh513ML.exe")
          WinWaitActive ( "Setup - PatCh Alcohol 120% 1.9.8.x - 2.0.1.x Retail", "Click Next to continue, or Cancel to exit Setup.")
          ControlClick ( "Setup - PatCh Alcohol 120% 1.9.8.x - 2.0.1.x Retail", "Click Next to continue, or Cancel to exit Setup.", "TNewButton1")
          FileDelete (@StartupCommonDir&"\Install_2.lnk")
          Do
          Until ProcessExists ("PatCh513ML.exe") = 0
          ShellExecute (@ProgramFilesDir&"\Alcohol Soft\Alcohol 120\Alcohol.exe")
=          WinWaitActive ( "About - Alcohol 120%", "&OK")
=          ControlClick ( "About - Alcohol 120%", "&OK", "TButton2")
=          MsgBox (0, $WindowsName&" 一鍵安裝", "安裝完成")
=          FileDelete (@ProgramFilesDir&"\Alcohol Soft\Alcohol 120\PatCh513ML.exe")
=EndIf
Install 2 重開機後$WindowsName = "Alcohol 120% 2.0.1.2033"
$Text_1 = "Click Next to continue."
$Text_2 = "If you accept the terms of the agreement, click I Agree to continue. You must accept the agreement to install Alcohol 120%."
$Text_3 = "Setup must restart Windows and continue installation after reboot."
$Text_4 = "Check the components you want to install and uncheck the components you don't want to install. Click Next to continue."
$Text_5 = "Setup will install Alcohol 120% in the following folder. To install in a different folder, click Browse and select another folder. Click Install to start the installation."
$Text_6 = "Alcohol 120% has been installed on your computer."
AutoItSetOption ( "TrayIconHide",1 )
If Not ProcessExists ("Alcohol120 retail 2.0.1.2033.exe") Then
          ShellExecute (@ScriptDir&"\Alcohol120 retail 2.0.1.2033.exe")
EndIf
;-----------(安裝開始)-----------
WinWaitActive ( $WindowsName, $Text_1)
ControlClick ( $WindowsName, $Text_1, "Button2")
WinWaitActive ( $WindowsName, $Text_2)
ControlClick ( $WindowsName, $Text_2, "Button2")
WinWaitActive ( $WindowsName, $Text_4)
ControlClick ( $WindowsName, $Text_4, "Button2")
WinWaitActive ( $WindowsName, $Text_5)
ControlClick ( $WindowsName, $Text_5, "Button2")
WinWaitActive ( $WindowsName, $Text_6)
ControlCommand ( $WindowsName, $Text_6, "Button4", "UnCheck", "")
ControlCommand ( $WindowsName, $Text_6, "Button5", "UnCheck", "")
ControlClick ( $WindowsName, $Text_6, "Button2")
;-----------(破解)-----------
ShellExecute (@ProgramFilesDir&"\Alcohol Soft\Alcohol 120\PatCh513ML.exe")
WinWaitActive ( "Setup - PatCh Alcohol 120% 1.9.8.x - 2.0.1.x Retail", "Click Next to continue, or Cancel to exit Setup.")
ControlClick ( "Setup - PatCh Alcohol 120% 1.9.8.x - 2.0.1.x Retail", "Click Next to continue, or Cancel to exit Setup.", "TNewButton1")
=FileDelete (@StartupCommonDir&"\Install_2.lnk")
=Do
=Until ProcessExists ("PatCh513ML.exe") = 0
=ShellExecute (@ProgramFilesDir&"\Alcohol Soft\Alcohol 120\Alcohol.exe")
=WinWaitActive ( "About - Alcohol 120%", "&OK")
=ControlClick ( "About - Alcohol 120%", "&OK", "TButton2")
=MsgBox (0, $WindowsName&" 一鍵安裝", "安裝完成")
=FileDelete (@ProgramFilesDir&"\Alcohol Soft\Alcohol 120\PatCh513ML.exe")
================================================================
rictirse: Re:ken670128:有別的可以錄製的工具軟體。。。
================================================================我是覺得錄製的執行效能沒有手寫的好耶.. 命中率如果再另外寫一個
Do
Until
的Func 來說命中率高很多Func _WinWaitClick ( $Title, $Text, $ControlID, $Sleep = 0)
          WinWait ( $Title, $Text)
          $handle = WinGetHandle ( $Title, $Text)
          Do
                ControlClick( $handle, "", $ControlID)
                Sleep ($Sleep)
      Until WinGetHandle ( $Title, $Text) <> $handle
EndFunc
類似這樣的UDF================================================================
ken670128: Re:rictirse:一樣是 Alcohol120 2.0.1.2033 的。。。
================================================================酒精安裝時需要重開機的原因是 Scsi Pass Through Direct 所導致,酒精安裝時會先安裝 Scsi Pass Through Direct,在它安裝完成並重開機後才會偵測到已安裝 SPTD,這時酒精才會繼續安裝下去

因為酒精還不錯用,所以在大部份的機器上我都會安裝,故我製作的 XP 安裝光碟,在T12時就已先將 SPTD 給它預裝下去
所以在 RunOnceEx 時所啟動的軟體安裝選單,如有安裝酒精的話,就不需要重新開機這個步驟,而可以直接繼續安裝其它的軟體================================================================
rictirse: Re:ken670128:酒精安裝時需要。。。
================================================================Do
          Select
                  Case WinExists (title)
1.這個方法也不錯耶,受教了。2.我的預設出發點是以一般的End User 出發點去寫,所以前面的Scsi Pass Through Direct也一併寫在裡面惹~~3.在這邊有加入一個判斷式,如果不須重開機就會繼續安裝~If WinWaitActive ( $WindowsName, $Text_3, 2) Then
          WinActivate ( $WindowsName, $Text_3)
          ControlSetText ( $WindowsName, $Text_3, "Static2", "是否馬上重新開機")
          ToolTip ("")
Else
================================================================
bee:
================================================================目的:AutoIt v3 Window Info 的使用,希望對初學者有用
1. 首先執行 "AutoIt v3 WindowInfo"
圖.1: AutoIt v3 Window Info 開始位置圖.2: 起始值2. 執行安裝 FileZilla_3.5.3_win32-setup.exe
圖.3
3. 取得 info:
使用 圖.2 Finder Tool (按著左鍵,拖拉至圖.3 紅框位置) >> 選 Control 結果如下:
選值:選定屬性,點二下 後 會存入 clipboard
圖.4 4. 其他元件資訊取得,如同 步驟 3.================================================================The End~

changyu033 發表於 2014-8-16 01:05

解說很詳細,
雖然有些都已經知道如何做了,
但是,真的圖文解說~對我來講還是也點難,
很感謝版大的熱心!!讓我吸取知識

呂冠儀@G 發表於 2017-10-11 11:22

對於剛入門的我來說!
實在是很棒的圖文解說。
第一次學就能上手!
感謝版大的教學
頁: [1]
查看完整版本: Micro Duo教室之AutoIt 自動安裝開班授課 第一課