Create JPEG Images from PNG's on your Mac Desktop

I like to use the PNG format for Mac screen-captures to ensure that I don’t lose any clarity in the source images. I often convert these images to JPEG format for inclusion in email or posts. I got tired of running the conversions by hand.

tojpeg.rb is a Ruby script for OS/X and MacOS that uses the sips utility to convert the PNG files in ~/Desktop to JPEG counterparts. The PNG files are left intact.

In addition, spaces in the filenames are converted to underscores. If the target JPEG filename already exists, no conversion is performed.

#!/usr/bin/env ruby
# Copyright (c) 2017 by James K. Lawless
# See full MIT / X11 license at
# http://jimlawless.net/license2017.htm  
#
pattern = Dir.home + "/Desktop/*.png"
puts "Processing files:" + pattern

Dir[pattern].each do |file_name|
   if File.file? file_name
      jpg_name = file_name.chop.chop.chop.chop + ".jpg"
      jpg_name=jpg_name.gsub(/\s/,"_")
      if ! (File.file? jpg_name)
         system("cp \"" + file_name + "\" " + jpg_name)    
         system("sips -s format jpeg " + jpg_name)
      end
   end
end