Created
November 30, 2023 01:12
-
-
Save hitode909/e1e1592086159cdc71aa94e74cbba57c to your computer and use it in GitHub Desktop.
Revisions
-
hitode909 created this gist
Nov 30, 2023 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ #! /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')}"