<WSH, win32com, Python> WSHとwin32comの関係
WSH(Windows Script Host)
と、win32com
を眺めてみた。
win32com
はどうやら、おそらく、WSH
とほぼ同じ。
WScript
でCalc
を起動して、キーを送るスクリプト。
https://msdn.microsoft.com/ja-jp/library/cc364423.aspx
set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run "calc" WScript.Sleep 100 WshShell.AppActivate "Calculator" WScript.Sleep 100 WshShell.SendKeys "1{+}" WScript.Sleep 500 WshShell.SendKeys "2" WScript.Sleep 500 WshShell.SendKeys "~" WScript.Sleep 500 WshShell.SendKeys "*3" WScript.Sleep 500 WshShell.SendKeys "~" WScript.Sleep 2500
これのpython/win32com
版。
#!/usr/bin/env python # -*- coding: utf-8 -*- import win32api as WScript import win32com.client if __name__ == '__main__': WshShell = win32com.client.Dispatch("WScript.Shell") WshShell.Run("calc") WScript.Sleep(100) WshShell.AppActivate("Calculator") WScript.Sleep(100) WshShell.SendKeys("1{+}", 0) WScript.Sleep(500) WshShell.SendKeys("2") WScript.Sleep(500) WshShell.SendKeys("~") WScript.Sleep(500) WshShell.SendKeys("*3") WScript.Sleep(500) WshShell.SendKeys("~") WScript.Sleep(2500)
動作はまったく同じ。
ちなみに、WScriptをsubprocess
で呼び出しても同じ。
#!/usr/bin/env python # -*- coding: utf-8 -*- import subprocess if __name__ == '__main__': subprocess.run("calc.vbs", shell=True)
なるほど。。。
と、いうことで、win32com
での制御は、基本WScript
どおり。
下記、マイクロソフトのところで、調べものすれば、結構思い通りに制御できそう。
参考。大変役にたちました。