0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pywinautoで「クロック」のタイマーを自動セッティング

Last updated at Posted at 2025-10-11

スクリプトの動作

Windowsアプリ「クロック」で新規タイマーを追加する。

実行方法

書式
py [スクリプト名].py --h [時間] --m [分] --s [秒]
例:1時間2分3秒のタイマーを設定
py script.py --h 1 --m 2 --s 3

スクリプト

関数化前

# デスクトップ上の既存ウィンドウを操作するクラスをインポート
from pywinauto import Desktop

# アプリを起動したり、そのプロセスを操作するためのクラスをインポート
## pywinauto の中の application モジュール から Application クラスだけを直接インポート
from pywinauto.application import Application

# コマンドライン引数を解析するためのモジュール。標準ライブラリに存在。
import argparse

# 時間に関する処理のためのモジュール。標準ライブラリに存在。
import time

# コマンドラインからの引数を処理
## ArgumentParser() で 引数解析用のパーサーを作成。
## パーサーとは、「入力を読み取って意味を理解するプログラムの部分」
parser = argparse.ArgumentParser()
## type=int → 値を整数として扱う
## default=0 → 引数が渡されなかった場合は 0 が使われる
## Pythonスクリプト内では args.h でアクセス可能(-- は除く)
parser.add_argument("--h", type=int, default=0)
parser.add_argument("--m", type=int, default=1)
parser.add_argument("--s", type=int, default=0)
## コマンドラインから渡された値を解析して args に格納
args = parser.parse_args()
## 2桁ゼロ埋めにする
hour_str = str(args.h).zfill(2)
minute_str = str(args.m).zfill(2)
second_str = str(args.s).zfill(2)

try:
    # クロックを起動
    ## Microsoft Storeアプリをexplorer.exe 経由で起動し、appに格納
    clock_app = Application(backend="uia").start('explorer.exe ms-clock:')

    # 一秒待つ
    time.sleep(1)

    # クロックウインドウにフォーカス
    ## デスクトップ上のウィンドウから対象ウィンドウの特定し、target_winに格納
    target_win = Desktop(backend="uia").window(title="クロック", control_type="Window")
    ## 対象ウィンドウにフォーカス
    target_win.set_focus()

    # 新しいタイマーを追加する
    ## クリック対象の特定し、target_itemに格納
    target_item = target_win.child_window(title="新しいタイマーの追加", control_type="Button")
    ## 実際に操作可能な UI要素オブジェクト(ラッパー) を取得し、item_to_doに格納
    item_to_do = target_item.wrapper_object()
    ## クリック
    item_to_do.click_input()

    # 一秒待つ
    time.sleep(1)

    # 時間を入力
    target_item = target_win.child_window(title="時間", control_type="Custom")
    item_to_do = target_item.wrapper_object()
    ## 対象をクリックして確実にフォーカス
    item_to_do.click_input()
    ## 数値を入力
    item_to_do.iface_value.SetValue(hour_str)

    # 分を入力
    target_item = target_win.child_window(title="", control_type="Custom")
    item_to_do = target_item.wrapper_object()
    item_to_do.click_input()
    item_to_do.iface_value.SetValue(minute_str)

    # 秒を入力
    target_item = target_win.child_window(title="", control_type="Custom")
    item_to_do = target_item.wrapper_object()
    item_to_do.click_input()
    item_to_do.iface_value.SetValue(second_str)

    # タイマー名を入力
    target_item = target_win.child_window(control_type="Edit")
    item_to_do = target_item.wrapper_object()
    item_to_do.click_input()
    ## テキストボックス内の文字を消去
    item_to_do.type_keys("^a{DEL}")
    ## テキストボックス内に入力
    item_to_do.type_keys(f"{hour_str}:{minute_str}:{second_str}")

    # 保存をクリック
    target_item = target_win.child_window(title="保存", control_type="Button")
    item_to_do = target_item.wrapper_object()
    item_to_do.click_input()

# 例外が発生した時の処理
## Exceptionですべてのエラーをキャッチ
## ExceptionはPythonの組み込みクラスで、すべての例外(エラー)の基底クラス。
except Exception as e:
    print(f"エラーが発生しました: {e}")
    ## type()でエラーの種類を表示
    print(f"エラー内容: {type(e)}")

関数化後

from pywinauto import Desktop
from pywinauto.application import Application
import argparse
import time

def setup_timer():
    # 引数解析
    parser = argparse.ArgumentParser()
    parser.add_argument("--h", type=int, default=0)
    parser.add_argument("--m", type=int, default=1)
    parser.add_argument("--s", type=int, default=0)
    args = parser.parse_args()
    
    hour_str = str(args.h).zfill(2)
    minute_str = str(args.m).zfill(2)
    second_str = str(args.s).zfill(2)
    
    try:
        # クロックアプリ起動
        clock_app = Application(backend="uia").start('explorer.exe ms-clock:')
        time.sleep(1)
        target_win = Desktop(backend="uia").window(title="クロック", control_type="Window")
        target_win.set_focus()
        
        # 新しいタイマー追加
        target_item = target_win.child_window(title="新しいタイマーの追加", control_type="Button")
        item_to_do = target_item.wrapper_object()
        item_to_do.click_input()
        time.sleep(1)
        
        # 時間、分、秒を設定
        for title, value_str in [("時間", hour_str), ("", minute_str), ("", second_str)]:
            target_item = target_win.child_window(title=title, control_type="Custom")
            item_to_do = target_item.wrapper_object()
            item_to_do.click_input()
            item_to_do.iface_value.SetValue(value_str)
        
        # タイマー名設定
        target_item = target_win.child_window(control_type="Edit")
        item_to_do = target_item.wrapper_object()
        item_to_do.click_input()
        item_to_do.type_keys("^a{DEL}")
        item_to_do.type_keys(f"{hour_str}:{minute_str}:{second_str}")
        
        # 保存
        target_item = target_win.child_window(title="保存", control_type="Button")
        item_to_do = target_item.wrapper_object()
        item_to_do.click_input()
        
        print(f"タイマー設定完了: {hour_str}:{minute_str}:{second_str}")
        
    except Exception as e:
        print(f"エラーが発生しました: {e}")
        print(f"エラー内容: {type(e)}")

if __name__ == "__main__":
    setup_timer()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?