GetActiveWIndows

chatgpt

GetActiveWIndows

解決追蹤使用習慣問題

啟發於Track 應用程式,感覺可以自己寫一個簡單的AP來自己紀錄、統計、產生週報

成果展示

import pygetwindow as gw
import ctypes
import ctypes.wintypes
import psutil
import time

LOG_FILE_PATH = 'window_titles.txt'
STATISTICS_INTERVAL = 1 * 6  # 15 分鐘,單位為秒

# 定義 Windows API 函數
user32 = ctypes.windll.user32
kernel32 = ctypes.windll.kernel32

def get_focused_window_info():
    active_window = gw.getActiveWindow()
    if active_window is not None:
        title = active_window.title
        hwnd = active_window._hWnd
        pid = ctypes.wintypes.DWORD()
        user32.GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
        process = psutil.Process(pid.value)
        return title, hwnd, pid.value, process.name()
    return None, None, None, None

def record_window_titles():
    titles = {}
    start_time = time.time()

    while True:
        focused_window_title, hwnd, pid, process_name = get_focused_window_info()
        current_time = time.time()

        if focused_window_title is not None:
            title_utf8 = focused_window_title.encode('utf-8')
            if title_utf8 not in titles:
                titles[title_utf8] = {'start_time': current_time, 'total_time': 0, 'hwnd': hwnd, 'pid': pid, 'process_name': process_name}
            else:
                title_info = titles[title_utf8]
                title_info['total_time'] += current_time - title_info['start_time']
                title_info['start_time'] = current_time

        elapsed_time = current_time - start_time

        if elapsed_time >= STATISTICS_INTERVAL:
            with open(LOG_FILE_PATH, 'a', encoding='utf-8') as file:
                file.write('--- Window Title Statistics ---\n')
                for title, title_info in titles.items():
                    file.write(f"Title: {title.decode('utf-8')}\n")
                    file.write(f"HWND: {title_info['hwnd'] or 0}\n")
                    file.write(f"PID: {title_info['pid']}\n")
                    file.write(f"Process Name: {title_info['process_name']}\n")
                    file.write(f"Total Time: {title_info['total_time']:.2f} 秒\n")
                    file.write('\n')
                file.write('--------------------------------\n')

            titles = {}
            start_time = current_time

        time.sleep(1)  # 每秒偵測一次

record_window_titles()

情境

使用python自己製作track、寫在toggl外掛試試看?

展示

Robert,4658876,3924,LINE.exe,845.16
ELK_Python_Client - Joplin_MK - Obsidian v1.2.8,7019822,28072,Obsidian.exe,493.62
,65960,4720,explorer.exe,338.40
LINE,661192,3924,LINE.exe,2.00
橘子工坊官網 - willis.ko@yfyshop.com - Outlook,198650,22212,OUTLOOK.EXE,6.01
寄件備份 - willis.ko@yfyshop.com - Outlook,198650,22212,OUTLOOK.EXE,118.13
正在開啟郵件附件,1120818,22212,OUTLOOK.EXE,0.00
C:\Users\Willis.KO\AppData\Local\Microsoft\Windows\INetCache\Content.Outlook\GRHMOTAZ\結案報告.zip\,2429442,18268,7zFM.exe,1.00
C:\Users\Willis.KO\AppData\Local\Microsoft\Windows\INetCache\Content.Outlook\GRHMOTAZ\結案報告.zip\結案報告\,2429442,18268,7zFM.exe,2.00
OB嚴選結案報告-0523.pdf - Adobe Acrobat Reader (64-bit),4919646,6656,Acrobat.exe,82.09
安全性警告,1250798,6656,Acrobat.exe,0.00
未命名 和其他 3 個頁面 - 個人 - Microsoft​ Edge,4067956,23316,msedge.exe,1.00
OB嚴選待辦事項 - Google 試算表 和其他 3 個頁面 - 個人 - Microsoft​ Edge,4067956,23316,msedge.exe,2.00
Skype,198272,16784,Skype.exe,20.03
logs,3215838,4720,explorer.exe,64.07
新增磁碟區 (D:),3215838,4720,explorer.exe,5.00
龍貓時鐘 - Google 搜尋 和其他 1 個頁面 - 個人 - Microsoft​ Edge,989706,23316,msedge.exe,26.02
龍貓時鐘 日本代購 - Google 搜尋 和其他 1 個頁面 - 個人 - Microsoft​ Edge,989706,23316,msedge.exe,1.00
https://shopee.tw/search?keyword=龍貓咕咕鐘 和其他 1 個頁面 - 個人 - Microsoft​ Edge,989706,23316,msedge.exe,0.00
LauncherSearchWindow,9509516,17492,Listary.exe,1.00
temp,2167442,25188,explorer.exe,170.21
戰情室,791484,4720,explorer.exe,14.01
resource,791484,4720,explorer.exe,1.00
規劃,791484,4720,explorer.exe,14.02
SearchBarWindow,8654672,17492,Listary.exe,2.00

設定

tips

指定虛擬環境