Tips for writing command line tools in ruby

  1. Option parsing: Read this article by Allen Wei on RubyLearning Blog for a great overview.  I recommend sticking with the built-in OptionParser if you want to reduce dependencies.
  2. If you want your code to be loadable so you can access functions and classes in the irb console for testing, use the following pattern:
    def main
     #option parsing and execution code here
    end
    if __FILE__ == $0
     main()
    end

    This way the main function will only be automatically called if the script is being executed on the command line.
    And in irb, you can call your functions and classes as you see fit for testing without triggering your whole script to run.

    Note that this is similar to the python __main__ test if you are coming from a python background.

  3. Naming without ruby’s.rb extension: You can name your executable without the rb extension if you wish, just be sure to include your shebang (#!)

    To use the user’s default ruby, use:
    #!/usr/bin/env ruby
    But in some cases you may want the specify the path to ruby so you can use macruby or rubycocoa if you are need those frameworks to be available
    #!/usr/bin/ruby

    When testing in irb,
    require 'script-name'
    won’t work without the rb extension, but
    load 'script-name'
    does work.
  4. Use exit codes. When your script fails or needs to communicate status at exit, use standard exit status codes.
    Exit zero for default success status
    exit 0
    Exit any other number for a failure or warning status. You choose the exit codes for your tool, but be sure to document them if they require more explanation than simple success or failure.
    exit 27
  5. Output to stderr using:
    $stderr.puts "error: problem ...."
Advertisements

dockutil 1.0 released

As a Mac sysadmin, I’ve had the need to manipulate the dock on hundreds of systems at a time.

I used to cobble together terrible shell scripts to do the job, but now thanks to plistlib and python, plist manipulation is really easy. I am releasing this utility free under the Apache 2.0 license. Hopefully some other sysadmins will find it useful.

dockutil is a command line utility for managing Mac OS X dock items.
It can add, replace, list, move, find, and delete dock items. It supports Applications, Folders, Stacks, and URLs. It can act on a specific dock plist or every dock plist in a folder of home directories.
It is compatible with Mac OS X Tiger and Leopard.

Download dockutil here.

Here is the usage information:

usage: dockutil -h
usage: dockutil --add (path to item) | (url) [--label (label)] [ folder_options ] [ position_options ] [ plist_location_specification ]
usage: dockutil --remove (dock item label) [ plist_location_specification ]
usage: dockutil --move (dock item label) position_options [ plist_location_specification ]
usage: dockutil --find (dock item label) [ plist_location_specification ]
usage: dockutil --list [ plist_location_specification ]

position_options:
--replacing (dock item label name) replaces the item with the given dock label or adds the item to the end if item to replace is not found
--position [ index_number | beginning | end | middle ] inserts the item at a fixed position: can be an position by index number or keyword
--after (dock item label name) inserts the item immediately after the given dock label or at the end if the item is not found
--before (dock item label name) inserts the item immediately before the given dock label or at the end if the item is not found
--section [ apps | others ] specifies whether the item should be added to the apps or others section

plist_location_specifications:
(path to a specific plist) default is the dock plist for current user
(path to a home directory)
--allhomes attempts to locate all home directories and perform the operation on each of them
--homeloc overrides the default /Users location for home directories

folder_options:
--view [grid|fan|list|automatic] stack view option
--display [folder|stack] how to display a folder's icon
--sort [name|dateadded|datemodified|datecreated|kind] sets sorting option for a folder view

Examples:
The following adds TextEdit.app to the end of the current user's dock:
dockutil --add /Applications/TextEdit.app

The following replaces Time Machine with TextEdit.app in the current user's dock:
dockutil --add /Applications/TextEdit.app --replacing 'Time Machine'

The following adds TextEdit.app after the item Time Machine in every user's dock on that machine:
dockutil --add /Applications/TextEdit.app --after 'Time Machine' --allhomes

The following adds ~/Downloads as a grid stack displayed as a folder for every user's dock on that machine:
dockutil --add '~/Downloads' --view grid --display folder --allhomes

The following adds a url dock item after the Downloads dock item for every user's dock on that machine:
dockutil --add vnc://miniserver.local --label 'Mini VNC' --after Downloads --allhomes

The following removes System Preferences from every user's dock on that machine:
dockutil --remove 'System Preferences' --allhomes

The following moves System Preferences to the second slot on every user's dock on that machine:
dockutil --move 'System Preferences' --position 2 --allhomes

The following finds any instance of iTunes in the specified home directory's dock:
dockutil --find iTunes /Users/jsmith

The following lists all dock items for all home directories at homeloc in the form: item(tab)path(tab)(section)tab(plist)
dockutil --list --homeloc /Volumes/RAID/Homes --allhomes

Notes:
When specifying a relative path like ~/Documents with the --allhomes option, ~/Documents must be quoted like '~/Documents' to get the item relative to each home

Bugs:
Names containing special characters like accent marks will fail

Contact:
Send bug reports and comments to kcrwfrd at gmail.

Play mp3 from python on Mac

This site has a few nice little PyObjC samples.
One of them shows how to play a sound using AppKit’s NSSound. This is exactly what I was trying to do today.

And since PyObjC is built-in in 10.5, no addtional software is required.

Python is already batteries included, with PyObjC, you’ve got batteries galore.

ipython output:

In [1]: from AppKit import NSSound
In [2]: sound = NSSound.alloc()
In [3]: sound.initWithContentsOfFile_byReference_('/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/TransitionSection.bundle/Contents/Resources/intro-sound.mp3', True)
Out[3]:
In [4]: sound.play()
Out[4]: True
In [5]: sound.stop()
Out[5]: True