Hatena::Grouprubyist

Rubyで遊ぶよ

 | 

2010-01-10

Mac OS X 用、open コマンドの zsh 補完関数

11:33

を作るための ruby スクリプト。アプリケーションフォルダの中身をスキャンして補完関数に埋め込む方針。

その前に2つ準備がいる。

その1。$HOME/.zsh/functions というディレクトリを作成しておく。

その2。zshrc に、

fpath=(~/.zsh/functions ${fpath})
autoload -U compinit
compinit

と書く。(もし autoload -U compinit が既に書かれてる場合は、fpath をそれより上で指定しないとだめ)


準備ができたら、以下のスクリプトを実行する。

#!/usr/bin/ruby

home = ENV['HOME']

$apps = []
def find_apps (dir)
  return unless File.directory?(dir)
  Dir.foreach(dir) do |file|
    if file != '.' && file != '..' && File.directory?(File.join(dir,file))
      if file =~ /^(.*)\.app$/
        $apps.push($1)
      else
        find_apps(File.join(dir,file))
      end
    end
  end
end

['/Applications','/Developer/Applications',"#{home}/Applications"].each do |dir|
  find_apps (dir)
end

filename = "#{home}/.zsh/functions/_open"

str = <<END
#compdef open

_arguments \\
  '-a[open with specified application]:application name:( \\
#{$apps.map{|app| "\"#{File.basename(app)}\""}.join(' ')} \\
)' \\
'-e[open with TexEdit]' \\
'*:files:_files'
END

open(filename, 'w') do |f|
  f.print str
end

そうすると、$HOME/.zsh/functions/_open として以下のようなファイルができる。(うちの場合)

#compdef open

_arguments \
  '-a[open with specified application]:application name:( \
"Address Book" "All2MP3" "AppCleaner" "Automator" "BBT2" "Caffeine" "Calculator" "Chess" "Chromium" "ClamXav" "Dashboard" "Dictionary" "Dropbox" "DTerm" "DVD Player" "EIJIRO Viewer" "ffmpegX" "Firefox" "WMV Player" "Font Book" "Freedom" "Front Row" "GoogleAppEngineLauncher" "UninstallGoogleJapaneseInput" "gyazo" "iCal" "iChat" "Image Capture" "iPaint" "iSync" "iTunes" "LyX" "MacKeyHoleTV" "AquaTerm" "Build Applet" "IDLE" "Python Launcher" "Mail" "Mendeley Desktop" "Microsoft Language Register" "Remove Office" "Microsoft Document Connection" "Microsoft Entourage" "Microsoft Excel" "Microsoft Messenger" "Microsoft PowerPoint" "Microsoft Word" "Alerts Daemon" "Equation Editor" "Microsoft Cert Manager" "Microsoft Chart Converter" "Microsoft Clip Gallery" "Microsoft Database Daemon" "Microsoft Database Utility" "Microsoft Graph" "Microsoft Office Reminders" "Microsoft Office Setup Assistant" "Microsoft Project Gallery" "Microsoft Sync Services" "My Day" "Organization Chart" "OpenOffice.org" "Opera" "Opera1050" "Paintbrush" "Photo Booth" "Preview" "Quicksilver" "QuickTime Player" "Reggy" "RichFLV" "Safari" "Songbird" "Stickies" "System Preferences" "BibDesk" "Excalibur" "LaTeXiT" "TeX Live Utility" "TeXShop" "TeXworks" "TextEdit" "Time Machine" "Activity Monitor" "Adobe AIR Application Installer" "Adobe AIR Uninstaller" "AirPort Utility" "AppleScript Editor" "Audio MIDI Setup" "Bluetooth File Exchange" "Boot Camp Assistant" "ColorSync Utility" "Console" "DigitalColor Meter" "Disk Utility" "Expose" "Grab" "Grapher" "i-Installer" "Java Preferences" "Keychain Access" "Migration Assistant" "Network Utility" "Podcast Capture" "RAID Utility" "Remote Install Mac OS X" "Spaces" "System Profiler" "Terminal" "VoiceOver Utility" "X11" "AU Lab" "HALLab" "Dashcode" "Core Image Fun House" "OpenGL Driver Monitor" "OpenGL Profiler" "OpenGL Shader Builder" "Pixie" "Quartz Composer Visualizer" "Quartz Debug" "Instruments" "Interface Builder" "BigTop" "CHUD Remover" "Reggie SE" "SpindownHD" "Saturn" "MallocDebug" "Quartz Debug" "Shark" "Spin Control" "Quartz Composer" "Accessibility Inspector" "Accessibility Verifier" "Applet Launcher" "Bluetooth Diagnostics Utility" "Bluetooth Explorer" "PacketLogger" "CrashReporterPrefs" "FileMerge" "Help Indexer" "Icon Composer" "IORegistryExplorer" "iSync Plug-in Maker" "Jar Bundler" "Build Applet" "PackageMaker" "Property List Editor" "Build Applet" "SleepX" "Repeat After Me" "SRLanguageModeler" "Syncrospector" "USB Prober" "VisualVM" "Xcode" \
)' \
'-e[open with TexEdit]' \
'*:files:_files'

次に zsh を起動したら補完がきいてるはず。

open のオプションは -a と -e 以外にもいくつかあるけど、それらは考慮してない。あしからず。

トラックバック - http://rubyist.g.hatena.ne.jp/edvakf/20100110
 |