Feeds:
文章
留言

Archive for the ‘AutoIT’ Category

#2015/07/27#

 

最近在自己的電腦使用AutoIT的imagesearch來找圖片,但很奇怪的是會出錯,而且還是出在include裡的imagesearch.au3,其訊息如下:

;if $result[0]="0" then return 0
;if $result^ ERROR

這蠻奇怪的,在公司使用了好多年都沒問題,為何在家裡才要開始用就有給我出問題,當然先上網搜尋,看看是否可快速找到解答,還好很好運的找到作者更新的版本,我記得這個我有下載過,於是跟目前用的比較了一下差異,發現我用的是舊版本。

而且這2012的新版,可以不用限定bmp格式,於是我就用png格式來測試,果然可以正常運作了。

有點印象過去有處理過這種遭遇,但太久忘記了怎處理,想不起來過去是如何處理的,當時也沒做紀錄,今天就給它記下來,尤其是這種裝好後就忘了它的存在,過幾年後,大概連自己有發這篇記錄,也許也會忘了也不一定,XD,佛法說,諸法因緣生,諸法因緣滅,一切無常,所以記憶力也是無常的,偶是見證者。

 

ImageSearchDLL.dll

放到AutoIT3安裝的目錄。

 

ImageSearch.au3

放到AutoIT3安裝的目錄下的include資料夾。

 

使用:

#include <ImageSearch.au3>

 

裡面我只用過_ImageSearch Function:

_ImageSearch($findImage,$resultPosition,ByRef $x, ByRef $y,$tolerance, $HBMP=0)

範例:

$result = _ImageSearch(@ScriptDir & "\pic\" & $imageName,1,$x,$y,0)

 

_ImageSearch會以整個桌面做為搜尋對象。

找到圖片會傳回1,否則傳回0

$resultPosition若是1,則傳回圖片中央的座標位置,若是0則傳回左上方的座標位置。

$HBMP是什麼碗糕,微軟在Windows Data Types文件是說 ”A handle to a bitmap.”我也看不懂這個參數有什麼作用,作者是這樣描述 – optional hbitmap to search in. sending 0 will search the desktop.

 

下載:ImageSearchDLLwithHBmp.zip

https://dl.dropboxusercontent.com/s/43z29kgpgjyeo2m/ImageSearchDLLwithHBmp.zip?dl=1

 

 

還有另外在執行時會出現如下紅色訊息:

* Input file is UTF8 without BOM encoded, Obfuscator do not support UNICODE and will be skipped.*
* The file SHOULD BE encoded as UTF8 with BOM to continue processing by AutoIt3Wrapper. *
* ##### AutoIt3Wrapper will not show a GUI or update the script to avoid any damage to your scriptfile. ##### *
* When your file isn’t a UTF8 file without BOM then please report this to me for review.*

 

若用SciTE Version 3.3.0開的檔案,預設都是ANSI格式,若忘了先調整編碼,最簡單的方法之一是用Windows內建的Notepad打開後另存在UTF8後,再用SciTE編輯器打開執行,警告訊息就消失了。

snapshot-20150727-230650

 

include底下的檔案預設都是ANSI格式,請勿自找麻煩把它存成UTF8格式。

 

 

Read Full Post »

#2010/9/22#

 

之前的問題是當視窗失去焦點時,就停止運作,今天改成底下這樣就算GUI失去焦點,也能繼續抓滑鼠座標及顏色。

只不過問題還是有的,就是用暫停迴圈的方法,一直不斷的重覆抓,就算滑鼠不動還是在繼續迴圈運算。

 

#include <GUIConstants.au3>
#include <WinAPI.au3>

_Main()

Func _Main()
    $guiWidth = 120
    $guiHeight = 80
    Local $hwnd = GUICreate("滑鼠座標及顏色",$guiWidth ,$guiHeight,10,@DesktopHeight-$guiHeight*2)
    WinWait("滑鼠座標及顏色")
    WinSetOnTop("滑鼠座標及顏色","",1)
    $iOldOpt = Opt("GUICoordMode", 2)
    $guiLabel1=GUICtrlCreateLabel("0", 10, 10, $guiWidth-20)
    $guiLabel2=GUICtrlCreateLabel("0", -1, 0)
    $guiColor=GUICtrlCreateGraphic(10,48,$guiWidth-20)

GUISetState()
    Do
        $msg = GUIGetMsg ()
        Sleep(50)
        getPos()
        getColor()
        GUICtrlSetData($guiLabel1,$strXY)
        GUICtrlSetData($guiLabel2,"顏色:" & $iColor)
        GUICtrlSetBkColor(-1, "0x" & $iColor)
;~         If $msg = $GUI_EVENT_MOUSEMOVE Then
;~             getPos()
;~             GUICtrlSetData($guiGL,$strXY)
;~         EndIf
    Until $msg = $GUI_EVENT_CLOSE
EndFunc

Func getPos()
    $tPoint = _WinAPI_GetMousePos()
    Global $x=DllStructGetData($tPoint, "X")
    Global $y=DllStructGetData($tPoint, "Y")
    Global $strXY="X = " & $x & ", Y = " & $y
EndFunc

Func getColor()
    Global $iColor=Hex(PixelGetColor($x,$y),6)
EndFunc

Read Full Post »

構想:將每日的收盤資料按照代號歸檔,得到的資料可拿來分析研究。

結果:有很多資料斷層,不過懶得去整合了。

要改進的部份:更新速度有點慢,將當日的資料拆掉分別寫入每個檔案,6千多筆檔案花了約一分鐘有吧,沒仔細計時。

寫了兩個檔案,這是每日更新資料檔的部份:

——————————————–
;第一個檔案
;主呼叫檔案

$autoIt="C:\Program Files (x86)\AutoIt3\AutoIt3.exe " 
$scriptPath="D:\AutoIT\" 
$scriptName="history_daily_call.au3"

$year=@YEAR 
$month=@MON 
$day=@MDAY 
Run ($autoIt & '"' & $scriptPath & $scriptName & '"' & " " & $year & " " & $month & " " & $day)

——————————————–

;第二個檔案

;csv檔案處理

#include-once 
#include <GUIConstants.au3> 
#include <File.au3>

AutoItSetOption("MustDeclareVars", 1)

;路徑 
Dim $srcPath = "D:\盤後資訊-每日收盤行情\" 
Dim $tarPath = "D:\盤後資訊-每日收盤行情更新\"

;檔案 
Dim $srcFile,$srcOpen,$tarFile,$tarOpen

;字串 
Dim $srcFileLine,$strArr,$strStock,$str0,$str1,$strLen

;日期 
Dim $year,$month,$day,$date

;GUI參數 
Local $hwnd = GUICreate("正在處理的資料",200,50) 
Local $iOldOpt = Opt("GUICoordMode", 2) 
Local $guiGL=GUICtrlCreateLabel("", 10, 20, 180) 
Local $msg 
GUISetState()

;日期處理 
$year = $CmdLine[1] 
$month = $CmdLine[2] 
$day = $CmdLine[3] 
$date = $year & $month & $day

;來源資料檔 
$srcFile = $srcPath & $year & "" & "A112" & $date & "ALL_1.csv"

;GUI顯示 
GUICtrlSetData($guiGL,"目前處理日期:" & $date)

;檢查來源資料檔是否存在 
if FileExists($srcFile) Then 
    ;檢查檔案大小是否大於10k 
    if FileGetSize($srcFile) > 10240 Then 
    ;開檔 
    $srcOpen = FileOpen($srcFile) 
    ;開檔失敗則結束程式 
    If $srcOpen = -1 Then 
        MsgBox(1,"Error","開檔失敗") 
        Exit 
    EndIf 
    ;處理迴圈 
    
    While 1 
        ;讀一行 
        $srcFileLine = FileReadLine($srcOpen) 
        ;讀到檔尾,則跳離迴圈 
        If @error = -1 Then    ExitLoop 
        ;用逗號分解行成陣列 
        ;因只要讀取證券代號,後面的雖然不準確,不過沒有影響,因為不使用 
        $strArr = StringSplit($srcFileLine, ",") 
        ;分割數量 
        $str0 = $strArr[0] 
        ;股票代號字串 
        $str1 = $strArr[1] 
        ;如果分割數量有2筆以上 
        If $str0 > 2 Then 
            ;如果開頭字母是等於符號,取出第3到字串數減3,="xxxx" 
            If StringLeft($str1,1) == "=" Then 
                $strLen = StringLen($str1)-3 
                $strStock = StringMid($str1, 3, $strLen) 
            Else 
                $strStock = $str1 
            EndIf 
            ;如果開頭字母是雙引號,則取出第2到結尾的字母 
            If StringLeft($strStock,1) == '"' Then 
                $strStock = StringMid($strStock,2) 
            EndIf 
            ;debug 
            ;MsgBox(1,"test",$str0 & "," & $str1 & "," & $strStock) 
            ;設定欲產生資料的路徑及名稱 
            $tarFile = $tarPath & $strStock & ".csv" 
            ;如果檔案不存在則建新檔案 
            If not FileExists($tarFile) Then 
                If Not _FileCreate($tarFile) Then 
                    MsgBox(1,"建檔失敗",@error & "," & $tarFile) 
                EndIf 
            EndIf 
            ;如果檔案存在則新增資料到檔尾 
            If FileExists($tarFile) Then 
                ;開一個可寫檔案 
                $tarOpen = FileOpen($tarFile,1) 
                If $tarOpen = -1 Then 
                    MsgBox(1,"Error","開檔失敗") 
                EndIf 
                ;寫入檔案 
                FileWrite($tarOpen,$date & "," & $srcFileLine & @CRLF) 
                ;關閉檔案 
                Fileclose($tarOpen) 
            Else 
                MsgBox(1,"檔案不存在",$tarFile) 
            EndIf 
        EndIf 
    WEnd 
    ;關閉檔案 
    FileClose($srcOpen) 
    EndIf 
EndIf
;~ Do 
;~     $msg = GUIGetMsg () 
;~ Until $msg = $GUI_EVENT_CLOSE

Read Full Post »

AutoITv3筆記

◎滑鼠左鍵按一下視窗中的相對位置。

;$winName:視窗名稱。 
;$xOffset:相對於視窗原點的x軸偏移量 
;$yOffset:相對於視窗原點的y軸偏移量 
Func clickOffset($winName,$xOffset,$yOffset) 
    $aPos=WinGetPos($winName) 
    $xScreen=$aPos[0]+$xOffset 
    $yScreen=$aPos[1]+$yOffset 
    MouseClick("left", $xScreen, $yScreen) 
EndFunc

 

◎等待畫面顏色的出現及消失。

#cs 
*等待某個點的顏色出現 
*用於等待某視窗出現 
#ce 
Func checkColor_isShow($x,$y,$color) 
    Do 
        Sleep(1000) 
        $iColor=PixelGetColor($x,$y) 
    Until Hex($iColor,6)==$color 
EndFunc

#cs 
*等待某個點的顏色消失 
*用於等待某視窗關閉 
#ce 
Func checkColor_isEnd($x,$y,$color) 
    Do 
        Sleep(1000) 
        $iColor=PixelGetColor($x,$y) 
    Until Hex($iColor,6)<>$color 
EndFunc

 

 

◎日期的迴圈

有時會需要下載大量的歷史資料,所以須要知道某年的某個月有幾天,這樣可以方便產生運算迴圈。

for $year=2006 to 2009 
for $month=1 to 12 
  ;_DateDaysInMonth取得月份中的天數 
  $iday=_DateDaysInMonth($year,$month) 
  ;用這個天數來作迴圈 
  for $day=1 to $iday 
    ;此天在此星期中的代表數字(1~7) 
    $isHoliday=_DateToDayOfWeek($year, $month, $day) 
    ;如果是星期一~五才進入處理,六或日則不作處理 
    If $isHoliday > 1 And $isHoliday < 7 Then 
      ............................. 
      ............................. 
    EndIf 
  Next 
Next 
Next

 

#2011/01/19#

◎AutoIT3用Command line跑script的問題

因我用ImageSearch.au3偵測顯示畫面,用滑鼠連點二下script執行正常,但一丟到排程跑就停住,查了一下發現是圖片的路徑問題,放在script的所在目錄跑,不用指定完整路徑,但用排程由Command line模式啟動則要指定圖片的完整路徑。

例如:路徑 c:\myscript\bmp\1.bmp

這時候要改成 @ScriptDir & "\bmp\1.bmp"

 

 

#2012/04/23#

◎加入分頁控制碼到檔案裡,讓印表機能換頁列印。

;chr(12)=FF (換頁控制碼)

$srcFileLine = @CR & chr(12) & $srcFileLine

;autoit讀一行並不會把斷行控制碼也一起讀進來,所以寫入時要自行加入CR和LF

FileWrite($tarOpen,$srcFileLine & @CRLF)

 

◎找螢幕畫面中與BMP圖片相同的區塊,並在圖片正中間點按一下左鍵。

Func clickImage($imageName)

Do 
    $x=0 
    $y=0 
    $result = _ImageSearch(@ScriptDir & "\bmp\" & $imageName,1,$x,$y,0) 
    if $result=1 Then 
        MouseMove($x,$y) 
        MouseClick("left",$x,$y) 
    EndIf 
    Sleep(1000) 
Until $result==1

EndFunc

 

#2012/10/23#

◎執行外部程式或指令時,設定路徑問題

;讀取機碼test.exe的預設值
$Path=RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\test.exe", "")
$strlen=StringLen($Path)
$PName=StringRight($Path,12)
$PPath=StringLeft($Path,$strlen-13)
$prog='start /d ' & '"' & $PPath2 & '" ' & $PName & ' '
$ini="\\computer\test.ini"
$para='/INI="' & $ini & '"'
$runp='cmd /c ' & $prog & $para
ConsoleWrite($runp & @CRLF)
Run($runp)

——

在AutoIT裡呼叫程式執行,要注意雙引號及單引的應用。

像此例呼叫的程式路徑中有空白字元,又帶入程式中的INI檔路徑又要包雙引號。

cmd /c start /d "C:\Program Files (x86)\test" test.exe /INI_FILE="\\computer\test.ini"

 

陸續增加…

Read Full Post »