Skip to content

Instantly share code, notes, and snippets.

@mattetti
Created April 25, 2012 15:24
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattetti/2490636 to your computer and use it in GitHub Desktop.
Save mattetti/2490636 to your computer and use it in GitHub Desktop.
MacRuby script to grab an url and save it as a jpg
#!/usr/local/bin/macruby
# Copyright (c) 2009 Matt Aimonetti
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
framework 'Cocoa'
framework 'WebKit'
class Application
def initialize
NSApplication.sharedApplication.delegate = self
end
def capture(url)
view = BrowserView.new
puts "capturing #{url}"
start_time = Time.now
view.fetch(url)
while !view.captured?
if timed_out?(start_time)
puts "Request timed out... sorry!"
exit
else
NSRunLoop.currentRunLoop.runUntilDate(NSDate.distantFuture)
end
end
end
def timed_out?(start_time)
(Time.now.to_i - start_time.to_i) > 30
end
end
class BrowserView
attr_accessor :view, :config, :url
def initialize
@view = WebView.alloc.initWithFrame([0, 0, 1024, 768])
@captured = false
window = NSWindow.alloc.initWithContentRect([0, 0, 1024, 768],
styleMask:NSBorderlessWindowMask,
backing:NSBackingStoreBuffered,
defer:false)
window.contentView = view
# Use the screen stylesheet, rather than the print one.
view.mediaStyle = 'screen'
view.customUserAgent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_2; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10'
# Make sure we don't save any of the prefs that we change.
view.preferences.autosaves = false
# Set some useful options.
view.preferences.shouldPrintBackgrounds = true
view.preferences.javaScriptCanOpenWindowsAutomatically = false
view.preferences.allowsAnimatedImages = false
# Make sure we don't get a scroll bar.
view.mainFrame.frameView.allowsScrolling = false
view.frameLoadDelegate = self
@loading = 0
end
def captured?
@captured
end
def fetch(url)
@url = url
page_url = NSURL.URLWithString(url)
view.mainFrame.loadRequest NSURLRequest.requestWithURL(page_url)
end
def webView(view, didFinishLoadForFrame:frame)
@loading -= 1
print '.'
if @loading.zero?
save
exit
end
end
def webView(view, didFailLoadWithError:error, forFrame:frame)
puts "Failed to take snapshot: #{error.localizedDescription}"
NSApplication.sharedApplication.terminate nil
end
def webView(view, didFailProvisionalLoadWithError:error, forFrame:frame)
puts "Failed to take snapshot: #{error.localizedDescription}"
NSApplication.sharedApplication.terminate nil
end
def webView(view, didCommitLoadForFrame:frame)
@loading += 1
end
def save
filename = url.gsub('http://', '').gsub('/', '-') + '.jpg'
filepath = File.expand_path("#{File.dirname(__FILE__)}/#{filename}")
puts "saving #{filepath}"
docView = view.mainFrame.frameView.documentView
width = docView.bounds.size.width
height = docView.bounds.size.height
docView.window.contentSize = [width, height]
docView.frame = view.bounds
docView.needsDisplay = true
docView.displayIfNeeded
docView.lockFocus
bits = NSBitmapImageRep.alloc.initWithFocusedViewRect(docView.bounds)
bits.representationUsingType(NSJPEGFileType, properties:{}).writeToFile(filepath, atomically:true)
@captured = true
docView.unlockFocus
end
end
url = ARGV.shift
unless url
print "Pass a URL to download: $ ./url_to_jpg.rb http://matt.aimonetti.net\n"
exit
end
app = Application.new
app.capture(url)
NSApplication.sharedApplication.terminate(nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment