Finder list view, change column with to optimal with using AppleScript

applescriptfindersnow leopard

I'm trying to create an AppleScript script that resizes the columns of Finder's list view (e.g. Name, Size …) to their optimal width (i.e. the minimum width where all text in the column is visible) on Mac OS X 10.6. This can be accomplished by double clicking the divider between two column's headers. But I can't find a way to do the same using AppleScript. This is what I've tried:

Finder's column class has the attributes width, minimum width and maximum width but there doesn't seem to be a way to use the width attribute to resize the column to it's optimal width.

I also tried to use UI Scripting to simulate a double click in the small region around the column header divider using the following script:

tell application "System Events"
    tell process "Finder"
        set nameButton to button "Name" of group 1 of outline 1 of scroll area 1 of splitter group 1 of first window

        set {px, py} to position of nameButton
        set {sx, sy} to size of nameButton
        set clickPos to {px + sx, py + 5}

        click at clickPos
        click at clickPos
    end tell
end tell

This should simulate a double click between the headers of the Name column and the column to it's right but it is instead processed by Finder as two distinct clicks on the header of the second column which just changes the sorting order of that column twice.

The Accessibility Inspector also doesn't "see" the small regions around the dividers of column headers and always shows the properties of either column header.

Is there any other way to accomplish this?

Best Answer

The only thing that I can think of off the top of my head would be to get a list of the items in the current folder, find the item with the longest name, and then resize the width per that value. That's really all that's happening with the optimal width double-click.