Skip to content

Instantly share code, notes, and snippets.

@keighl
keighl / photo_processor.rb
Created October 7, 2013 12:06
Example image manipulation worker from S3 using rmagick and aws-sdk-ruby
require 'RMagick'
class PhotoProcessor
def self.process(entry_id)
entry = Entry.find entry_id
raise "The photo for this entry has already been processed" if entry.image_processed?
class Document: NSDocument {
@IBOutlet var fontSizeField: NSTextField?
dynamic var currentFontSize: CGFloat = 0
override func windowControllerDidLoadNib(aController: NSWindowController) {
super.windowControllerDidLoadNib(aController)
fontSizeField.bind("value",
toObject: self,
withKeyPath: "currentFontSize",
class Document: NSDocument {
@IBAction func fontSizeChanged(sender: NSControl) {
var newSize = CGFloat(sender.floatValue)
if var font = textView?.typingAttributes[NSFontAttributeName] as? NSFont {
font = NSFontManager.sharedFontManager().convertFont(font, toSize: newSize)
applyNewAttributes([NSFontAttributeName: font])
}
}
func applyNewAttributes(attributes: [String: AnyObject]) {
func textViewDidChangeTypingAttributes(notification: NSNotification) {
if let font = textView?.typingAttributes[NSFontAttributeName] as? NSFont {
if let faces = NSFontManager.sharedFontManager().availableMembersOfFontFamily(font.familyName) {
// faces is an array of arrays like this:
// [[Helvetica, Regular, 5, 0], [Helvetica-Light, Light, 3, 0], [Helvetica-Oblique, Oblique, 5, 1], [Helvetica-LightOblique, Light Oblique, 3, 1], [Helvetica-Bold, Bold, 9, 2], [Helvetica-BoldOblique, Bold Oblique, 9, 3]]
fontFacesLabels = faces.map({face in (face[1] as! String)})
}
}
}
dynamic let fontFamilyNames = NSFontManager.sharedFontManager().availableFontFamilies
fontFamilyName!.bind("content", toObject: self, withKeyPath: "fontFamilyNames", options: nil)
@IBAction func fontFamilyChanged(sender: NSPopUpButton) {
let newIDX = sender.indexOfSelectedItem
if var font = textView?.typingAttributes[NSFontAttributeName] as? NSFont {
font = NSFontManager.sharedFontManager().convertFont(font, toFamily: fontFamilyNames[newIDX])
applyNewAttributes([NSFontAttributeName: font])
}
@IBAction func fontFaceChanged(sender: NSPopUpButton) {
let newIDX = sender.indexOfSelectedItem
if let currentFont = textView?.typingAttributes[NSFontAttributeName] as? NSFont {
let newFaceName = fontFaceNames[newIDX]
if let newFont = NSFontManager.sharedFontManager().convertFont(currentFont, toFace: newFaceName) {
applyNewAttributes([NSFontAttributeName: newFont])
}
}
}
if var font = textView?.typingAttributes[NSFontAttributeName] as? NSFont {
font = NSFontManager.sharedFontManager().convertFont(font, toHaveTrait: .BoldFontMask)
// or ...
font = NSFontManager.sharedFontManager().convertFont(font, toNotHaveTrait: .BoldFontMask)
applyNewAttributes([NSFontAttributeName: font])
}