Toggle Sidebar in all open Finder windows

Sep 23, '09 07:30:03AM

Contributed by: llee

Here's an AppleScript that toggles the presence of the sidebar in all Finder windows. Toggle status is determined by that of the front window. In Tiger, it sets the sidebar to its minimum width; in Snow Leopard, it hides it completely. The script leaves the toolbar alone.

set Showsidebar to true
tell 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 ≠ 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 if
end tell
[robg adds: I tested this in both 10.5 and 10.6, and it works as described.]

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090905195206513