Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 / cd-last.sh
Created May 7, 2010 12:42
make each new terminal tab open in the last dir you cd'd to. put this in your .bashrc
lastdir_store="$HOME/.last_cd_path"
function cd () {
builtin cd "$@"
exitcode=$?
pwd > "$lastdir_store"
return $exitcode
}
[ -f "$lastdir_store" ] && target=`cat "$lastdir_store"`
@kch
kch / Rewind 30 Seconds.rb
Created May 2, 2010 21:40
Rewind currently playing track 30 seconds in iTunes
#!/usr/bin/env ruby1.9
# encoding: UTF-8
require 'appscript'
include Appscript
itunes = app("iTunes")
pos = itunes.player_position.get
pos = pos > 30 ? pos - 30 : pos / 2
itunes.player_position.set pos
@kch
kch / rb_main.rb
Created May 2, 2010 16:09
A more idiomatic rb_main for MacRuby applications
# Loading the Cocoa framework. If you need to load more frameworks, you can do that here too.
framework 'Cocoa'
# Loading all the Ruby project files.
[__FILE__].concat(Dir[File.join(NSBundle.mainBundle.resourcePath.fileSystemRepresentation, '*.{rb,rbo}')])
.map { |path| File.basename(path, File.extname(path)) }.uniq[1..-1]
.each { |name| require(name) }
# Starting the Cocoa main loop.
NSApplicationMain(0, nil)