Sha256: c5a1f58fe0862f22061de2a2fdcd12fd1544c6c8a16117a4b73ff8e2df8f24db

Contents?: true

Size: 1.34 KB

Versions: 67

Compression:

Stored size: 1.34 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 figure
#    called `figure_name.png` in the `images/figures/` directory.
# The method is to look for the most recently modified PNG file on the desktop
# and move it (while renaming it) to the `images/figures` directory.


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

end.parse!

name = ARGV.shift
if name.nil?
  puts "usage: rename_screenshot filename [options]"
  exit 1
end

unless File.directory?('images/figures')
  $stderr.puts "ERROR: 'images/figures' doesn't exist. Create and re-run."
  exit 1
end

target = "images/figures/#{name}.png"

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
if response.downcase == 'y'
  FileUtils.mv(most_recent, target)
  system "git add #{target}"
end

Version data entries

67 entries across 67 versions & 1 rubygems

Version Path
softcover-1.10.5 bin/rename_screenshot
softcover-1.10.4 bin/rename_screenshot
softcover-1.10.3 bin/rename_screenshot
softcover-1.10.2 bin/rename_screenshot
softcover-1.10.1 bin/rename_screenshot
softcover-1.10.0 bin/rename_screenshot
softcover-1.9.1 bin/rename_screenshot
softcover-1.9.0 bin/rename_screenshot
softcover-1.8.3 bin/rename_screenshot
softcover-1.8.2 bin/rename_screenshot
softcover-1.8.1 bin/rename_screenshot
softcover-1.8.0 bin/rename_screenshot
softcover-1.7.5 bin/rename_screenshot
softcover-1.7.4 bin/rename_screenshot
softcover-1.7.3 bin/rename_screenshot
softcover-1.7.2 bin/rename_screenshot
softcover-1.7.1 bin/rename_screenshot
softcover-1.7.0 bin/rename_screenshot
softcover-1.6.5 bin/rename_screenshot
softcover-1.6.4 bin/rename_screenshot