VBS でアプリを操作

Option Explicit

main()

sub main()

 dim aa
 set aa = new AutoApp

 aa.FileName = "CMD"
 aa.exec()
 aa.writelnStdIn("dir")
 aa.waitt(1)
 aa.quit()
 msgbox(aa.readStdOut())

 aa.FileName = "notepad.exe"
 aa.exec()
 aa.waitApp()
 aa.ActivateAndSendKeys "TEST",100
 aa.waitt(3)
 aa.quit()


end sub

Class AutoApp

    dim objShell
    dim objExec

    Private m_FileName
    Private m_ProcessId

    Public Property Get FileName
        FileName = m_FileName
    End Property

    Public Property Let FileName(vData)
        m_FileName = vData
    End Property

    Public Property Get ProcessId
        m_ProcessId = objExec.ProcessId
        ProcessId = m_ProcessId
    End Property

    Private Sub Class_Initialize()
 Set objShell = WScript.CreateObject("WScript.Shell")
    End Sub

    Public Function exec()
        Set objExec = objShell.Exec(m_FileName) 
    End Function

    Public Function readStdOut()
        readStdOut = objExec.StdOut.ReadAll
    End Function

    Public Function writeStdIn(str)
        objExec.StdIN.write str
    End Function

    Public Function writelnStdIn(str)
        objExec.StdIN.writeline str
    End Function

    Public Sub quit()
        If objExec.Status = 0 Then
            objExec.Terminate
        End If
    End Sub

    Public Sub waitApp()
 Do Until objShell.AppActivate(objExec.ProcessID)
     WScript.Sleep 100
 Loop
    End Sub

    Public Sub waitWindow(title)
 Do Until objShell.AppActivate(title)
     WScript.Sleep 100
 Loop
    End Sub

    Public Sub SendKeys(strKey)
 objShell.SendKeys strKey
    End Sub

    Public Sub waitt(t)
        WScript.Sleep t*1000
    End Sub

    '*********************************************************
    '用途: 指定したプロセスIDのウィンドウをアクティブにし、指定
    '      したキー・コードを送り、数ミリ秒待つ
    '受け取る値:    
    '              strKey: 送るキー・コード(String)
    '             intWait: キー・コードを送った後待つミリ秒数
    '                      (Integer)
    '戻り値: 成功したらTrue、失敗したらFalseを返す(Boolean)
    '*********************************************************
    Function ActivateAndSendKeys(strKey, intWait)
        Dim intCounter
        '10回試行する
        For intCounter = 1 To 10
            'AppActivateメソッドを実行し、戻り値がTrueなら、
            If objShell.AppActivate(objExec.ProcessId) Then
                WScript.Sleep 100
                'キー・コードを送る
                objShell.SendKeys strKey
                'intWaitミリ秒待つ。
                WScript.Sleep intWait
                '成功を意味するTrueを返し、ループを抜ける
                ActivateAndSendKeys = True
                Exit For
            Else
                WScript.Sleep 1000
                '失敗を意味するFalseを返し、続行
                ActivateAndSendKeys = False
            End If
        Next
    End Function

    Public Sub waitQuit()
        Do While objExec.Status = 0
            WScript.Sleep 100
        Loop
    End Sub

    Public Sub run()
        objShell.Run m_FileName
    End Sub

    Public Sub run_sync()
        objShell.Run m_FileName,,True
    End Sub


    Private Sub Class_Terminate()
        Set objShell = Nothing
        Set objExec = Nothing
    End Sub

End Class


コメント

このブログの人気の投稿

Python OpenCVとWebカメラでバーコードリーダー

OpenCV 画像の足し算

OpenCV3とPython3で形状のある物体の輪郭と方向を認識する(主成分分析:PCA、固有ベクトル)