Skip to content

Rewrite screenshot cleanup in bash with cross-platform support#68

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/rewrite-screenshot-cleanup-bash
Draft

Rewrite screenshot cleanup in bash with cross-platform support#68
Copilot wants to merge 5 commits intomainfrom
copilot/rewrite-screenshot-cleanup-bash

Conversation

Copy link
Contributor

Copilot AI commented Feb 5, 2026

Screenshot Cleanup Bash Rewrite Plan

  • Create new bash script
    • Add OS detection function (get_screenshot_dir()) that uses uname -s to determine macOS or Linux
    • Add function to extract timestamp from filename (extract_timestamp())
    • Add function to calculate age in days (age_in_days())
    • Add function to trash a file (trash())
    • Add function to archive a file (archive())
    • Add function to process archive files (process_archive())
    • Add function to process screenshot files (process_screenshots())
    • Add main function that orchestrates the workflow
    • Support macOS via defaults read com.apple.screencapture location
    • Support Linux/GNOME via gsettings for screenshot location and filename template
  • Create BATS test file
    • Test OS detection function
    • Test timestamp extraction
    • Test age calculation
    • Test file operations (dry run mode)
    • All 17 tests passing (removed 2 unused tests)
  • Create GitHub Action workflow
    • Run on macOS and Ubuntu runners
    • Execute only when script or test file changes
    • Install dependencies (bats, trash)
    • Fixed YAML formatting issues
    • Fixed Dependabot compatibility for action versions
    • Improved apt hygiene
  • Address code review feedback
    • Simplified redundant condition check
    • Improved test clarity
  • Address PR feedback
    • Renamed script from screenshot_cleanup.sh to screenshot_cleanup
    • Fixed uses: format for Dependabot compatibility
    • Improved apt install command with --yes and --no-install-recommends
    • Removed unused get_screenshot_pattern() function
    • Made Linux regex case-insensitive for "from"
  • Run security checks (CodeQL - no issues found)
  • Delete old Ruby script
Original prompt

This section details on the original issue you should resolve

<issue_title>Rewrite screenshot cleanup in bash</issue_title>
<issue_description>screenshot_cleanup.rb doesn't work anymore on macOS 15.

I asked Copilot to rewrite it in Rust and bash and I'll go with the bash version because I don't want to add rust-script and rust as a whole to my Brewfile just for one thing.

#!/usr/bin/env bash

set -euo pipefail

DRY_RUN=${DRY_RUN:-false}
SCREENSHOT_DIR=$(defaults read com.apple.screencapture location)
SCREENSHOT_PREFIX="Screenshot"
DEFAULT_SCREENSHOT_GLOB="${SCREENSHOT_DIR}/${SCREENSHOT_PREFIX}*"
SCREENSHOT_GLOB=${SCREENSHOT_GLOB:-$DEFAULT_SCREENSHOT_GLOB}
DEFAULT_ARCHIVE_GLOB="${SCREENSHOT_DIR}/Screenshot Archive/${SCREENSHOT_PREFIX}*"
ARCHIVE_GLOB=${ARCHIVE_GLOB:-$DEFAULT_ARCHIVE_GLOB}
ARCHIVE_DIR=$(dirname "$ARCHIVE_GLOB")

# Extract timestamp from filename
extract_timestamp() {
  local filename="$1"
  if [[ $filename =~ ([0-9]{4}-[0-9]{2}-[0-9]{2})\ at\ ([0-9]{1,2}\.[0-9]{2}\.[0-9]{2})(\ [AP]M)? ]]; then
    local date="${BASH_REMATCH[1]}"
    local time="${BASH_REMATCH[2]//./:}"
    echo "${date} ${time}"
  else
    echo "Error: Unable to extract timestamp from filename: $filename" >&2
    exit 1
  fi
}

# Calculate age in days
age_in_days() {
  local timestamp="$1"
  local now=$(date +%s)
  local file_time=$(date -j -f "%Y-%m-%d %H:%M:%S" "$timestamp" +%s)
  echo $(( (now - file_time) / 86400 ))
}

# Trash a file
trash() {
  local file="$1"
  echo "🗑 ⬅ $(basename "$file")"
  if [[ "$DRY_RUN" != "true" ]]; then
    trash "$file"
  fi
}

# Archive a file
archive() {
  local file="$1"
  echo "🗄 ⬅ $(basename "$file")"
  if [[ ! -d "$ARCHIVE_DIR" ]]; then
    echo "Creating $ARCHIVE_DIR"
    if [[ "$DRY_RUN" != "true" ]]; then
      mkdir -p "$ARCHIVE_DIR"
    fi
  fi
  local new_name="${ARCHIVE_DIR}/$(basename "$file")"
  if [[ "$DRY_RUN" != "true" ]]; then
    mv "$file" "$new_name"
  fi
}

# Process archive files
echo "Looking at dirglob $ARCHIVE_GLOB for deletion…"
find "$ARCHIVE_DIR" -type f -name "${SCREENSHOT_PREFIX}*" | while read -r file; do
  timestamp=$(extract_timestamp "$(basename "$file")")
  age=$(age_in_days "$timestamp")
  if (( age > 15 )); then
    trash "$file"
  else
    echo "🗄 ⬇ $(basename "$file")"
  fi
done

# Process screenshot files
echo "Looking at dirglob $SCREENSHOT_GLOB for archival…"
find "$SCREENSHOT_DIR" -type f -name "${SCREENSHOT_PREFIX}*" | while read -r file; do
  if [[ -d "$file" ]]; then
    continue
  fi
  timestamp=$(extract_timestamp "$(basename "$file")")
  age=$(age_in_days "$timestamp")
  if (( age > 7 )); then
    archive "$file"
  else
    echo "📂 ⬇ $(basename "$file")"
  fi
done

I have not evaluated this.</issue_description>

Comments on the Issue (you are @copilot in this section)

@colindean A good submission will include:
  • Change SCREENSHOT_DIR=$(defaults read com.apple.screencapture location) to a function call that looks at the OS output via uname -s.
  • refactor the Bash script above to put all code not in a function into functions to facilitate testing
  • bats tests to verify each of these functions
  • a GitHub Action that runs on macos and ubuntu runners, and executes only when this screenshot_cleanup script or its test file changes.</comment_new>
    <comment_new>@colindean
    A good submission will also include GNOME's default screenshot location and filename template when uname -s is Linux.

The filename template is something like "Screenshot From %Y-%m-%d %H-%i-%s.png" and the default location is ~/Pictures/Screenshots, but it may be possible to retrieve this with gsettings.</comment_new>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits February 5, 2026 01:02
Co-authored-by: colindean <197224+colindean@users.noreply.github.com>
Co-authored-by: colindean <197224+colindean@users.noreply.github.com>
Co-authored-by: colindean <197224+colindean@users.noreply.github.com>
Copilot AI changed the title [WIP] Rewrite screenshot cleanup script in bash Rewrite screenshot cleanup in bash with cross-platform support Feb 5, 2026
Copilot AI requested a review from colindean February 5, 2026 01:05
Co-authored-by: colindean <197224+colindean@users.noreply.github.com>

# Create test files with valid Linux screenshot names
local old_date
old_date=$(date -d "10 days ago" "+%Y-%m-%d")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: @copilot This doesn't work on macOS:

(in test file tests/test_screenshot_cleanup.bats, line 184)
     `old_date=$(date -d "10 days ago" "+%Y-%m-%d")' failed
   date: illegal option -- d
   usage: date [-jnRu] [-I[date|hours|minutes|seconds|ns]] [-f input_fmt]
               [ -z output_zone ] [-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]
               [[[[mm]dd]HH]MM[[cc]yy][.SS] | new_date] [+output_fmt]

Add something here so that it'll work on macOS.


# Calculate a timestamp from 10 days ago
local ten_days_ago
ten_days_ago=$(date -d "10 days ago" "+%Y-%m-%d %H:%M:%S")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: @copilot This doesn't work on macOS:

(in test file tests/test_screenshot_cleanup.bats, line 184)
     `ten_days_ago=$(date -d "10 days ago" "+%Y-%m-%d")' failed
   date: illegal option -- d
   usage: date [-jnRu] [-I[date|hours|minutes|seconds|ns]] [-f input_fmt]
               [ -z output_zone ] [-r filename|seconds] [-v[+|-]val[y|m|w|d|H|M|S]]
               [[[[mm]dd]HH]MM[[cc]yy][.SS] | new_date] [+output_fmt]

Add something here so that it'll work on macOS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rewrite screenshot cleanup in bash

2 participants