Coder Social home page Coder Social logo

mac-activity-logger's Introduction

Mac Activity Logger

This script logs the currently active application and related metadata to a local SQLite database on macOS. The goal of the project is to help me understand how I spend my time on my computer. Some ways how this data could be used later:

  • Generate reports showing the amount of time spent in each application. Identify trends in usage over time.
  • Analyze time spent on websites and categorize them as productive, leisure, educational, etc.
  • Set goals for certain metrics, like maximum hours per day spent on leisure websites. Track progress towards these goals over time.
  • Use URL data to identify patterns.
  • Apply machine learning to app and website categories to automatically detect productive vs distracting usage patterns.

Features

  • Logs the foreground application name every time the script is run
  • If Google Chrome is the active app, also logs the active tab URL and title
  • Saves timestamped records to a SQLite database for persistent storage
  • Uses AppKit via AppleScript to get active app information
  • Errors are logged to a separate file for diagnostics

Requirements

  • macOS
  • Python 3
  • SQLite 3

Usage

  • Run the mac_activity_logger.py script on a schedule via Launchpad or cron
  • The local SQLite database will be created at ~/Documents/app_activity_db.sqlite
  • Log files will be created at ~/Documents/app_activity_errors.log
  • View and query the database with SQLite tools like DB Browser for SQLite
$ sqlite3 ~/Documents/app_activity_db.sqlite "SELECT * FROM app_activity ORDER BY timestamp DESC LIMIT 10;"
2023-08-29 12:29:47.289955|Terminal||
2023-08-29 12:29:37.109373|Terminal||
2023-08-29 12:29:26.904139|Google Chrome|...
2023-08-29 12:29:16.644958|Google Chrome|...
2023-08-29 12:29:06.383001|Google Chrome|...
2023-08-29 12:28:56.088728|Google Chrome|...
2023-08-29 12:28:45.836765|Google Chrome|...
2023-08-29 12:28:35.571242|Google Chrome|...
2023-08-29 12:28:25.285999|Google Chrome|...
2023-08-29 12:28:14.999757|Telegram||

Run on Startup

To run this script in the background on startup every 5 seconds, you can create a launchd plist file.

Here are the steps:

  1. Create a file ~/Library/LaunchAgents/com.user.app_activity.plist with this content:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>com.user.app_activity</string>

  <key>ProgramArguments</key>
  <array>
    <string>python</string>
    <string>/full/path/to/your_script.py</string>
  </array>

  <key>StartInterval</key>
  <integer>5</integer>
  
  <key>RunAtLoad</key>
  <true/>
  
  <key>KeepAlive</key>
  <true/>
</dict>
</plist>
  1. Load the plist to start the script:
launchctl load ~/Library/LaunchAgents/com.user.app_activity.plist

mac-activity-logger's People

Contributors

alex000kim avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.