Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Last active April 18, 2020 19:15
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ttscoff/4514795 to your computer and use it in GitHub Desktop.
Save ttscoff/4514795 to your computer and use it in GitHub Desktop.
OS X System Service script for filing Jekyll images and putting Markdown links on the clipboard
require 'fileutils'
# An OS X System Service for quickly filing image files to a Jekyll blog folder,
# putting Markdown links to the files on the clipboard.
# Copyright Brett Terpstra 2013
# Config
# ======
# Where to store the images
base_path = '~/Sites/dev/octopress/source/uploads/'
# What to output as the base image path in the clipboard links
base_url = '/uploads' # or set "http://yoursite.com/uploads"
link_type = "liquid" # or "reference", defines generated Markdown link style
def e_sh(str)
str.to_s.gsub(/(?=[^a-zA-Z0-9_.\/\-\x7F-\xFF\n])/, '\\').gsub(/\n/, "'\n'").sub(/^$/, "''")
end
input = ARGF
urls = []
input.each {|file_path|
if File.basename(file_path) =~ /\.(jpeg|jpg|png|gif)$/
extension = File.basename(file_path).match(/\.(jpeg|jpg|png|gif)$/)[0]
target_file = File.basename(file_path).gsub(/#{extension}$/,'').strip
target_dir = File.expand_path(base_path).gsub(/\/$/,'') + "/" + Time.now.strftime('%Y/%m/').strip
FileUtils.mkdir_p target_dir unless (File.directory? target_dir)
while File.exists?(File.join(target_dir, target_file + extension)) do
target_file = target_file + "_00" unless target_file =~ /_\d+/
target_file.next!
end
target = File.join(target_dir, target_file + extension)
begin
FileUtils.cp(file_path.strip,target)
case File.extname(target)
when /jpe?g/
# log.info("Compressing JPEG")
if File.exists?('/usr/local/bin/jpegoptim')
res = %x{/usr/local/bin/jpegoptim -fopt --strip-all -m60 "#{target}"}
end
when /png/
# log.info("Compressing PNG")
if File.exists?('/usr/local/bin/pngcrush')
res = %x{/usr/local/bin/pngcrush -q -ow "#{target}"}
end
end
if link_type == "reference"
urls.push("[#{target_file.gsub(/[^a-z0-9]/i,'')}]: #{target.gsub(/^#{File.expand_path(base_path).gsub(/\/$/,'')}/,"#{base_url.gsub(/\/$/,'')}")}")
elsif link_type == "liquid"
width = %x{sips -g pixelWidth "#{target}"|tail -n 1}.gsub(/pixelWidth: /,'').strip
height = %x{sips -g pixelHeight "#{target}"|tail -n 1}.gsub(/pixelHeight: /,'').strip
imgclass = width.to_i > 300 ? "aligncenter" : "alignright"
urls.push("{% img #{imgclass} #{width} #{height} #{target.gsub(/^#{File.expand_path(base_path).gsub(/\/$/,'')}/,"#{base_url.gsub(/\/$/,'')}")} %}")
else
urls.push("![](#{target.gsub(/^#{File.expand_path(base_path).gsub(/\/$/,'')}/,"#{base_url.gsub(/\/$/,'')}")})")
end
rescue => e
puts e
%x{/usr/local/bin/growlnotify -a "Dropzone.app" -s -m "Couldn't file #{File.basename(file_path)}" -t "Jekyll Image Filer"} if File.exists?("/usr/local/bin/growlnotify")
end
end
}
%x{echo #{e_sh urls.join("\n")}|pbcopy}
%x{/usr/local/bin/growlnotify -a "Automator.app" -s -m "Filed #{urls.length.to_s} images, urls in clipboard"} if File.exists?("/usr/local/bin/growlnotify")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment