Use Python to monitor Windows and Mac and collect information on the apps you are working on

Introduction

I hear that more and more people work at home, and many people are worried that they are all working at home properly.

I think it's strange to be anxious in the first place, but I also wonder if some people will be in trouble if their boss becomes anxious.

So, I thought that if I could collect information on the apps I'm using in real time when using a computer, I could reduce that anxiety a little, so I considered a method.

Here, I will summarize the method of "getting and displaying the title of the active window every time the active window is changed".

Operating environment

Library installation

Library for operating Mac

For Mac, install the required library with the following command.

pip install -U PyObjC

Library for operating Windows

For Windows, install the required library with the following command.

pip install -U pywin32

Script creation

get_active_window_title.py


#!/usr/bin/env python

import sys

#For Mac
if sys.platform == "darwin":
    from AppKit import NSWorkspace
    from Quartz import (
        CGWindowListCopyWindowInfo,
        kCGWindowListOptionOnScreenOnly,
        kCGNullWindowID
    )

    def getActiveWindowTitle():
    
        curr_app = NSWorkspace.sharedWorkspace().frontmostApplication()
        curr_pid = NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationProcessIdentifier']
        curr_app_name = curr_app.localizedName()
        options = kCGWindowListOptionOnScreenOnly
        windowList = CGWindowListCopyWindowInfo(options, kCGNullWindowID)
        
        txt = ""
        
        for window in windowList:
            pid = window['kCGWindowOwnerPID']
            windowNumber = window['kCGWindowNumber']
            ownerName = window['kCGWindowOwnerName']
            geometry = window['kCGWindowBounds']
            windowTitle = window.get('kCGWindowName', u'Unknown')
            
            if curr_pid == pid:
                activeWindowTitle = ownerName + " - " + windowTitle

        return activeWindowTitle

#For windows
elif sys.platform == "win32":
    import win32gui

    def getActiveWindowTitle():
        
        activeWindowTitle = win32gui.GetWindowText(win32gui.GetForegroundWindow());
        return activeWindowTitle

else:
    def getActiveWindowTitle():
        return ""
    
def main():
    bufWindowTitle = ""
    
    try:
        while True:
            activeWindowTitle = getActiveWindowTitle()
            if bufWindowTitle != activeWindowTitle:
                print(activeWindowTitle)
                bufWindowTitle = activeWindowTitle
    except KeyboardInterrupt:
        sys.exit(0)
        
if __name__ == '__main__':
    main()

Run

After creating it, execute it with the following command.

python get_active_window_title.py

You should see the application name each time you switch the active window.

did it!

If you send this to the specified server, you will know that you are working properly, and it is okay to be told "Prove!"! !! ...All right! ......I think it's okay.

Recommended Posts

Use Python to monitor Windows and Mac and collect information on the apps you are working on
Python 3.6 on Windows ... and to Xamarin.
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
How to get started with the 2020 Python project (windows wsl and mac standardization)
I want to use Python in the environment of pyenv + pipenv on Windows 10
Steps to install the latest Python on your Mac
What to do if the Microsoft Store opens even if you run python on Windows
Steps to use the AWS command line interface (Python / awscli) on Mac OS X
Until you create Python Virtualenv on Windows and launch Jupyter
[Hyperledger Iroha] Notes on how to use the Python SDK
Install pyenv on MacBook Air and switch python to use
Update python on Mac to 3.7-> 3.8
[Python] When you want to import and use your own package in the upper directory
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
Use Python on Windows (PyCharm)
[Introduction to Mac] Convenient Mac apps and settings that I use
A note on what you did to use Flycheck with Python
I want to know if you install Python on Mac ・ Iroha
Use python on Raspberry Pi 3 to illuminate the LED (Hello World)
The road to installing Python and Flask on an offline PC
[Introduction to Python] How to use the Boolean operator (and ・ or ・ not)
How to use Dataiku on Windows
Integrate Modelica and Python on Windows
Steps to install python3 on mac
Sakura Use Python on the Internet
Update Python on Mac from 2 to 3
Use AWS lambda to scrape the news and notify LINE of updates on a regular basis [python]
Specify the browser to use with Jupyter Notebook. Especially Mac. (And Vivaldi)
Technical English> you use the boolean operators [and, or, and not] to ...> Boolean Operations — and, or, not
If you want to use NumPy, Pandas, Matplotlib, IPython, SciPy on Windows
Use python on Raspberry Pi 3 to light the LED with switch control!
Work memo to migrate and update Python 2 series scripts on the cloud to 3 series
Description and summary of what you need to install Chainer on Mac
[Python] How to use the enumerate function (extract the index number and element)
Try to build python and anaconda environment on Mac (by pyenv, conda)
Put MeCab binding for Python with pip on Windows, mac and Linux
Deploy and use the prediction model created in Python on SQL Server
How to install and use pandas_datareader [Python]
Python error messages are concrete and easy to understand "ga" (SyntaxError on the closing side of triple "" "comments)
[Kivy] How to install Kivy on Windows [Python]
What to do if you installed pyenv on Mac OS using Homebrew but the python version doesn't switch
I want to use Linux on mac
How to use Google Assistant on Windows 10
How to erase Python 2.x on Mac.
Memorandum on how to use gremlin python
Notes on building Python and pyenv on Mac
Use without installing python 2.x on Windows
python: How to use locals () and globals ()
See file and folder information on python
Install python on xserver to use pip
How to use Python zip and enumerate
GAE --With Python, rotate the image based on the rotation information of EXIF and upload it to Cloud Storage.
How to deal with "^ [[A ^ [[B ^ [[C ^ [[D"] when you press the arrow keys when executing python on mac
How to use is and == in Python
Python open and io.open are the same
I managed to solve the situation where Python does not work on Mac
Tips for those who are wondering how to use is and == in Python
The key to deploying Flask apps using Python Buildpack on Cloud Foundry (Diego)
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
I tried to use Resultoon on Mac + AVT-C875, but I was frustrated on the way.
To avoid seeing hell when installing django-toolbelt on windows, heroku and python3.4 (64bit) ...