Toggle sidebar visibility in all open Finder windows

Rob Griffiths
23 November, 2009
View more articles fromthe author
AAA
Help

When I publish hints here on Macworld, I usually try to walk through and explain how they work, so as to help you understand what’s going on behind the scenes. Sometimes, though, I’ll just present a solution without much of an explanation, such as with today’s hint.

Today’s hint is an AppleScript that will toggle the visibility of the sidebar in all open Finder windows. In 10.6, you can do this for any one window with the View -> Hide (or Show) Sidebar menu item, but it only applies to the current window. Using the script in today’s hint, you can toggle the visibility in all open windows.

While the script works in both 10.5 and 10.6, it’s most useful in 10.6, because the script can’t completely hide the sidebar in 10.5 (instead, it just shrinks down to its minimum width).

Another advantage to using this script in 10.6 is that it can be created as a Service, and then run via either the Services menu, or if you prefer, a keyboard shortcut.

The following instructions assume you’re running 10.6; if you’re running 10.5, you can either enter the code in the Script Editor, and save it as an application, or possibly create it in Automator and save it as a Finder Plug-In (though I haven’t tested that).

In 10.6, launch Automator, and when the template chooser window appears, click Service then click Choose to open a new workflow. Near the top of the window that opens, on the right, you’ll see two pop-up menus. Set the rightmost of those to Finder, then set the leftmost to “no input.” (If you set the leftmost one first, then the Finder will disappear from the rightmost pop-up.)

Next, in the search box on the left, type run apples, which should be enough text to find just one matching action, Run AppleScript. Drag that action into the work area on the right, and then delete all of the placeholder code that’s in the text area (on run {input...etc).

Insert the following code in its place:

set Showsidebar to truetell application "Finder"  activate  if exists window 1 then    set allwins to every window whose collapsed is false    set my_num to 0    repeat with onewin in allwins      set my_num to my_num + 1      tell application "System Events"        tell process "Finder"          set my_id to the name of onewin          tell application "Finder"            try              set my_test to get ¬                toolbar visible of window my_id            on error              set my_test to false            end try          end tell          if my_test then

            tell application "Finder"              if window my_id is window 1 then                set the_place to get sidebar width of window my_id

                if the_place = 0 then                  set Showsidebar to true                else                  set Showsidebar to false                end if              end if            end tell          end if          tell application "Finder"            if index of window my_id is not equal 1 then              open window my_id            else              if Showsidebar then                tell application "Finder"                  tell front window to set sidebar width to 150                end tell              else                tell application "Finder"                  tell front window to set sidebar width to 0                end tell              end if

            end if          end tell        end tell        if my_test then          if Showsidebar then            tell application "Finder"              tell front window to set sidebar width to 150            end tell          else            tell application "Finder"              tell front window to set sidebar width to 0            end tell          end if        end if      end tell      tell application "Finder"        set index of window my_id to my_num      end tell    end repeat  end ifend tell

Select File -> Save, and give your new Serivce a suitable name (Toggle Sidebar). At this point, you may want to test it by switching to the Finder, opening a few windows, then selecting Services -> Toggle Sidebar (or whatever you called it).

Assuming this works, you can then make it even simpler by creating a keyboard shortcut. Open System Preferences, and go to the Keyboard Shortcuts tab of the Keyboard System Preferences panel. Select Services in the left-hand column, then scroll to General (the final group) in the right-hand column. Find your saved Service, and double-click in the blank area to the right of its name.

Type in a new keyboard shortcut—I’ve had the best luck with Services shortcuts when using Control in conjunction with Shift and/or Option—and you’re done. Switch back to the Finder, and you should be able to hide (and then show again) the sidebar in all open windows by typing your keyboard shortcut.

Thanks to Mac OS X Hints contributor Laine Lee for this useful bit of AppleScript code.

Leave a Comment

Please keep your comments friendly on the topic.

Contact us