Created
November 30, 2023 01:12
-
-
Save hitode909/e1e1592086159cdc71aa94e74cbba57c to your computer and use it in GitHub Desktop.
Continuous Screen Capture Script for xbar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/ruby | |
require 'logger' | |
require 'json' | |
LOGGER = Logger.new(STDOUT) | |
if ENV['DEBUG'] | |
LOGGER.level = Logger::Severity::DEBUG | |
else | |
LOGGER.level = Logger::Severity::WARN | |
end | |
OUTPUT_DIR=File.join(Dir.home, 'Desktop', 'continuous-screen-capture') | |
MAX_AGE=3600 * 24 * 7 | |
unless File.exists?(OUTPUT_DIR) | |
LOGGER.debug "Creating output directory: #{OUTPUT_DIR}" | |
Dir.mkdir(OUTPUT_DIR) | |
end | |
def capture_screenshot | |
display_info = `system_profiler SPDisplaysDataType -json` | |
display_ids = JSON.parse(display_info)['SPDisplaysDataType'].first['spdisplays_ndrvs'].map{|display| display['_spdisplays_displayID'] } | |
output_filenames = display_ids.map { | display_id| | |
File.join(OUTPUT_DIR, "#{Time.now.strftime('%Y%m%d-%H%M%S')}-#{display_id}.jpg") | |
} | |
LOGGER.debug "Capturing screenshot to #{output_filenames.join(' ')}" | |
system "screencapture", "-x", "-t", "jpg", "-T", "0", *output_filenames | |
end | |
def delete_old_screenshots | |
all_files = Dir.glob("#{OUTPUT_DIR}/*.jpg").sort | |
all_files.each{|file| | |
enough_old = (Time.now - File.mtime(file)) > MAX_AGE | |
if enough_old | |
LOGGER.debug "Deleting old file: #{file}" | |
File.delete(file) | |
end | |
} | |
end | |
capture_screenshot | |
delete_old_screenshots | |
puts "📸 #{Time.now.strftime('%H:%M:%S')}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment