10.3: A set of AppleScripts to toggle item visiblity

Nov 26, '03 09:27:00AM

Contributed by: biggyfishy

Well, I've seen SetFile mentioned around as the tool for making files visible and invisible in the Finder (among other things), but no-one seems to have wrapped it up nicely in an Applescript. Note the following scripts require SetFile, which is only included in the Developer Tools. Unfortunately, I have nowhere to post these so I'll simply have to paste them in here. Also, I had to change them to work with Panther; hence, I don't think they will work reliably (if at all) in Jaguar.

The first script (Make Invisible) simply makes the current Finder selection invisible. If you don't want confirmation every time you run it, change the safe_user flag from true to false. The second script (Make Visible) brings up a dialog box listing all invisible files in the front window. Simply choose the ones you want to be made visible. This script is very slow in folders with many items -- if anyone can suggest speed improvements I'd be most grateful.

I use these scripts to rearrange my Applications folder without moving any Apple apps. This is probably unnecessary now, but I haven't heard one way or another and I'm too lazy to do testing. Basically what I do is make an alias of each of the iApps, say, and put them in an iApps folder. Then I can make the Applications themselves invisible and cut down the number of items in my Applications folder. Hurrah!

Read the rest of the article for the scripts...

Make Invisible:


-- Applescript: Make Invisible
-- For Mac OS X 10.3 Panther
-- This script is intended to be run from the scripts menu.
-- Its function is to set files to be invisible. To make
-- invisible: select some files to be made invisible. Run the
-- script. Enter your admin password if you are not the
-- owner of the files.
-- Will Robertson Nov 2003

property safe_user : false

-- Change this to your username:
tell application "Finder"
  set user to the system attribute "USER"
  set selec to the selection
  set N to the number of items in selec
  set proceed_flag to false
  
  if safe_user then
    set selec_names to {}
    
    repeat with ii from 1 to the count of selec
      if ii ? 1 then
        set the end of selec_names to "; "
      end if
      set the end of selec_names to ¬
        (the name of item ii of selec) as string
    end repeat
    
    if the button returned of (display dialog ¬
      ("Make the following items in the current folder invisible?" & ¬
        return & return & selec_names as string) buttons ¬
      {"Make invisible", "Cancel"} default button ¬
      "Cancel") is "Make invisible" then
      set proceed_flag to true
    end if
  else
    set proceed_flag to true
  end if
  
  if N > 0 then
    -- THERE IS A SELECTION: Make it/them INVISIBLE
    repeat with ii from 1 to N
      set this_item to (item ii of selec as alias)
      my invis(this_item, user) -- subroutine to make item invisible
    end repeat
    if N = 1 then
      say "Item made invisible"
    else
      say "Items made invisible"
    end if
  else
    say "No items are selected"
  end if
end tell

-- Subroutine to make an item (file or folder) invisible:
on invis(this_item, user)
  tell application "Finder"
    if the owner of item this_item is user then
      do shell script "/Developer/Tools/SetFile -a V '" & ¬
      POSIX path of this_item & "'"
    else
      do shell script "/Developer/Tools/SetFile -a V '" & ¬
      POSIX path of this_item & "'" with administrator privileges
    end if
  end tell
end invis
Make Visible:

-- Applescript: Make visible
-- For Mac OS X 10.3 Panther
-- The function of this script is to remove invisibility from
-- items. Note it does not work with files that begin with a
-- dot (ie .DS_store), because those files are not shown in
-- the Finder, invisible or not.
-- Will Robertson Nov 2003

set the item_choices to {}
set the target_items to {}

set user to the system attribute "USER"

tell application "Finder"
  
  set the target_folder to the folder of the front window as alias
  
  set item_names to list folder (the target_folder) with invisibles
  repeat with ii from 1 to the count of the item_names
    if (the visible of the (info for ((target_folder as string) & ¬
      (item ii of the item_names) as alias)) is false) and ¬
      (the first item of (item ii of the item_names as string) ¬
        is not ".") then
      set the end of the item_choices to item ii of the item_names
    end if
  end repeat
  
  if the number of items of item_choices is 0 then
    display dialog ("No invisible files in the selected folder:" & return & ¬
      POSIX path of target_folder as string) buttons "OK" default button "OK"
  else
    set the chosen_files to choose from list item_choices with prompt ¬
      ("Choose item(s) to make visible in:" & return & POSIX path of ¬
        target_folder as string) with multiple selections allowed
    if the result is not false then
      repeat with ii from 1 to the count of the chosen_files
        set this_item to (target_folder as string) & (item ii of the ¬
          chosen_files) as alias
        if the owner of this_item is user then
          do shell script "/Developer/Tools/SetFile -a v '" & ¬
            POSIX path of this_item & "'"
        else
          do shell script "/Developer/Tools/SetFile -a v '" & ¬
            POSIX path of this_item & "'" with administrator privileges
        end if
      end repeat
      
      display dialog ¬
        "The Finder will have to be relaunched in order to view these files. Do that now?" buttons ¬
        {"Relaunch", "Not now"} default button "Not Now"
      if the button returned of the result is "Relaunch" then
        tell application "Finder" to quit
        delay 1
        tell application "Finder" to launch
      end if
    end if
  end if
end tell
Put these scripts in the folder ~/Library -> Scripts -> Applications -> Finder for them to appear in the Script menu when the Finder is the frontmost app. I use these scripts to rearrange my Applications folder without moving any Apple apps. This is probably unnecessary now, but I haven't heard one way or another and I'm too lazy to do testing. Basically what I do is make an alias of each of the iApps, say, and put them in an iApps folder. Then I can make the Applications themselves invisible and cut down the number of items in my Applications folder. Hurrah!

Comments (6)


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