VBS でアプリを操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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カメラでバーコードリーダー

VB.net Dictionaryクラスの初期化