Skip to content

Instantly share code, notes, and snippets.

@kch
kch / browser.rb
Created January 28, 2011 02:24
browser function used in my appscripts to target Safari or WebKit dynamically
#!/usr/bin/env ruby
# encoding: UTF-8
require 'appscript'
VALID_BROWSERS = %w[ WebKit Safari ]
# Used in my appscripts to target Safari or WebKit dynamically.
# Returns an Appscript::Application for one of these browsers following this priority:
# 1. is frontmost
# 2. is running and default
@kch
kch / mvdn
Created October 19, 2010 21:51
Rename files to all lowercase in two steps, works on a case-insensitive FS like HFS.
#!/usr/bin/env ruby
# encoding: UTF-8
require 'fileutils'
require 'unicode_utils'
ARGV.each do |f|
f_ = UnicodeUtils.downcase(f)
f__ = "#{f_}_#{rand}"
FileUtils.mv(f, f__)
@kch
kch / ipv6.rb
Created October 14, 2010 21:22
QUIZZZZS validate an ip v6
#!/usr/bin/env ruby
# encoding: UTF-8
require 'yaml'
def ipv6?(s)
raise NotImplementedError
end
good, bad = YAML::load(DATA.read).values_at *%w[ GOOD BAD ]
results = []
framework 'Cocoa'
# store any new classes created during the requires that follow
LOADED_RUBY_CLASSES = []
class << Object
def inherited(m)
LOADED_RUBY_CLASSES << m
end
end
@kch
kch / gist:602174
Created September 29, 2010 01:53
NSArrays are tricky bastards
#!/usr/bin/env macruby
framework 'Cocoa'
cocoa_array = NSArray.new
ruby_array = []
puts ruby_array.count # => 0
puts ruby_array.count { true } # => 0
puts ruby_array.count("whatever") # => 0
@kch
kch / rb-magic-comment
Created September 27, 2010 20:25
Add the encoding magic comment to the ruby files passed as arguments
#!/usr/bin/env ruby
# encoding: UTF-8
ARGV.reject! { |path| !File.file?(path) }
(puts DATA.read.gsub("$0", File.basename($0)); exit 1) if ARGV.empty?
ARGV.each do |path|
ls = IO.readlines(path)
ix = ls[0] !~ /^#!/ ? 0 : 1
next if ls[ix] =~ /#.*?coding\s*[:=]\s*\S/
@kch
kch / gemfile-from-dotgems.rb
Created September 23, 2010 14:15
Convert a Heroku .gems file to a Bundler Gemfile
#!/usr/bin/env ruby
# encoding: UTF-8
require 'strscan'
(puts DATA.read.gsub(/\$0/, File.basename($0)); exit 1) unless ARGV.empty? # handled -h and anything else since we don't take args
# tokenize lines
lines = STDIN.read.strip.split(/\n+/)
gems = lines.inject({}) do |h, line|
next h if line =~ /^\s*(#.*)?$/
@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
@kch
kch / .gitignore
Created August 26, 2010 08:19
Apple Mail plugin that fixes your sender for mailing lists
Contents/MacOS/main
@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>