Skip to content

Instantly share code, notes, and snippets.

@kch
kch / gist:410051
Created May 22, 2010 12:50
convert all erb html templates to haml
find app/views -name '*.html.erb' |
perl -pe 's/\.erb$//' |
xargs -I @ html2haml --erb @.erb @.haml
@kch
kch / initializer_macro.rb
Created July 3, 2010 02:07
generate initializers that just set instance variables without wasting lines
#!/usr/bin/env ruby1.9 -ryaml
# encoding: UTF-8
class Class
def initialize_with_attributes(*attrs)
attr_reader *attrs
define_method(:initialize) do |*args|
attrs.zip(args).each { |attr, v| instance_variable_set("@#{attr}", v) }
end
end
@kch
kch / Copy Window's URLs.rb
Created July 3, 2010 23:16
Copies the URLs from all tabs in Safari/WebKit's front window to the pasteboard.
#!/usr/bin/env ruby
# encoding: UTF-8
require 'rubygems'
require 'appscript'
include Appscript
# Copy Window's URLs:
# Copies the URLs from all tabs in Safari/WebKit's front window to the pasteboard.
# `process_for_either_browser` will be any process named Safari, this includes
@kch
kch / ftff-events.rb
Created August 3, 2010 13:34
mac: watch home dir via fsevents and set encoding attr and reveal extension for txt and rb files
#!/usr/bin/ruby
# encoding: UTF-8
require 'rubygems'
require 'fsevent'
require 'shellwords'
class Time
def age_in_seconds(now = Time.now)
now - self
@kch
kch / plist-to-yaml.rb
Created August 4, 2010 09:54
converts plist files to really pretty, aligned yaml files with mostly literal utf8 strings
#!/usr/local/bin/ruby
# encoding: UTF-8
require 'yaml'
require "base64"
require 'rexml/document'
class PList < BasicObject
ELEMENT_PROCESSORS = {}
def self.process_element_named(name, &block); ELEMENT_PROCESSORS[name.to_s] = block; end
def self.process_element(elt) ; ELEMENT_PROCESSORS[elt.name][elt]; end
@kch
kch / Open URLs from Pastboard.rb
Created August 4, 2010 10:20
Script to open URLs from pasteboard. For use with FastScripts.
#!/usr/bin/env ruby
# encoding: UTF-8
# URL regexp from http://daringfireball.net/2010/07/improved_regex_for_matching_urls
# URL regexp adapted to have captures removed
RX_URL = %r[
\b
(?:
[a-z][\w-]+: # URL protocol and colon
@kch
kch / yaml-to-plist.rb
Created August 4, 2010 14:50
Writes ruby object structures to plists. When called directly, takes a YAML file.
#!/usr/bin/env ruby
# encoding: UTF-8
require 'yaml'
require 'rexml/document'
class PlistWriter
PLIST_STUB_DOC = %q[
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"></plist>]
@kch
kch / Info.plist
Created August 19, 2010 04:50
n00b attempt at standalone foundation tool OS X service in MacRuby
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key> <string>English</string>
<key>CFBundleExecutable</key> <string>upcase</string>
<key>CFBundleIdentifier</key> <string>com.caiochassot.services.upcase</string>
<key>CFBundleInfoDictionaryVersion</key> <string>6.0</string>
<key>CFBundlePackageType</key> <string>BNDL</string>
@kch
kch / .gitignore
Created August 26, 2010 08:19
Apple Mail plugin that fixes your sender for mailing lists
Contents/MacOS/main
@kch
kch / after.nu
Created August 27, 2010 08:50
after macro for Nu plus a few handy functions
;; drops the first element from a list and returns it
(macro shift! (l)
`(let (e (car ,l))
(set ,l (cdr ,l))
e))
;; drops the first cell from a list if it is equal to e
(macro cond-shift! (l e) `(if (eq (car ,l) ,e) (shift! ,l)))
;; appends e to the list l