AppleScript – Change Column Visibility in Finder

applescript

When I change the visible property of a Finder column to false it does not have any effect even after closing and reopening the window.

tell application "Finder"
    activate
    set visible of column id kind column of list view options of Finder window 1 to false
end tell

It appears to be a bug. Any workarounds?

Best Answer

I do not believe it's a bug because if you look at the AppleScript Dictionary for Finder, it shows the following for list view options as a property:

  • list view options (list view options, r/o) : the list view options for the container window

The user cannot change a read only property, however there is a UI Scripting solution.

That said, I try to avoid UI Scripting whenever possible however, I have the daily need to open an external drive in a particular view, sorted by size and to do this a UI Scripting solution was this only choice I could find.

I've taken the relevant example AppleScript code from it to toggle the visibility of the Kind column:

tell application "Finder"
    if (current view of front Finder window) is equal to list view then
        activate
        my toggleCheckBox("Kind")
    end if
end tell

to toggleCheckBox(thisBox)
    tell application "System Events"
        keystroke "j" using command down
        tell window 1 of application process "Finder"
            click checkbox thisBox of group 1
            click button 2
        end tell
    end tell
end toggleCheckBox

Note that this particular UI Scripting solution does open and close the View Options for the front Finder window and is a bit of a visual distraction and one of the reasons I dislike UI Scripting however, sometimes one has no other choice to accomplish a given task. Note that I'm not saying it cannot be done any other way, just at the present time I don't know another way.


Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also Working with Errors. Additionally, UI Scripting may require the use of the delay command as appropriate, needed or wanted.