Sha256: 0542da24e1cf5e60abc15226c9b5ba08a6ec45f98a8bed495392dfbc261c6bac

Contents?: true

Size: 1.89 KB

Versions: 9

Compression:

Stored size: 1.89 KB

Contents

#!/usr/bin/env ruby
require 'fileutils'
require 'optparse'

# Renames a screenshot.
# This is intended for use with Shift-Cmd-4 on Mac OS X.
# 1. Use Shift-Cmd-4 to take a screenshot
# 2. Run `rename_screenshot figure_name` to create a full-size figure
#    called `figure_name.png` in the `full_size_figures` directory.
# A corresponding smaller resized figure is automagically created
# in the `images/figures` directory. The idea is to keep the full-size figures
# in case they're ever needed, but to resize them so they're not too big
# for inclusion in the document.
# The method is to look for the most recently modified PNG file on the desktop
# and move it (while renaming it) to the `full_size_figures` directory. Then
# a normal-size figure is generated using the `make_figures` script.

def figure(name)
  "images/figures/#{name}.png"
end

def rm(filename)
  FileUtils.rm(filename) if File.exist?(filename)
end

options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: rename_screenshot [options]"

  opts.on("-f", "Force overwrite of existing file") do
    options[:force] = true
  end

  opts.on("-i", "Invert a renaming by removing the relevant images") do
    options[:invert] = true
  end
end.parse!

name = ARGV.shift
if name.nil?
  puts "usage: rename_screenshot filename [options]"
  exit 1
end
FileUtils.mkdir("full_size_figures") unless File.exist?("full_size_figures")
target = "full_size_figures/#{name}.png"

if options[:invert]
  rm(target) if File.exist?(target)
  rm(figure(name))
end

most_recent = Dir["#{ENV['HOME']}/Desktop/*.png"].sort_by do |a|
                File.stat(a).mtime
              end.reverse.first
response = 'y'
if File.exist?(target) && !options[:force]
  puts "File #{target} already exists. Replace? [Y/n]"
  response = gets.strip.downcase
end
unless response.downcase == 'n'
  FileUtils.mv(most_recent, target)
  rm(figure(name))
  system "make_figures"
  system "git add -A"
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
softcover-1.1.23 bin/rename_screenshot
softcover-1.1.22 bin/rename_screenshot
softcover-1.1.21 bin/rename_screenshot
softcover-1.1.20 bin/rename_screenshot
softcover-1.1.19 bin/rename_screenshot
softcover-1.1.18 bin/rename_screenshot
softcover-1.1.17 bin/rename_screenshot
softcover-1.1.16 bin/rename_screenshot
softcover-1.1.15 bin/rename_screenshot