Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.4: Use Automator to toggle hidden Unix files' visibility System 10.4
Tiger only hintQuite a long time ago, this hint explained how to use the command line to make the Finder show all those hidden unix files. I turned it into a little shell script, and stuck it where I could find it. Then more recently, a bunch of AppleScript solutions were hinted at, in a thread that started in mid 2003, and had still more postings as of a couple of months ago.

Now with Tiger, however, it's possible to make showing such files a contextual menu selection via Automator. I made an Automator action called 'Show/hide unix files,' and told Automator to save the file as a plug-in for the Finder. Read on for the details...

There are two steps in the workflow; one shell script to show/hide the files, and one AppleScript to quit the Finder and restart it, so the changes can be immediately seen. The scripts were compiled from some knowledge of the bash shell, and some help from the posters on the original hints. I particularly like the error checking provided by 'hofman' (so my AppleScript is basically his).

Launch Automator, and add the Action named Run Shell Script, with the shell set to /bin/bash, and the script set to the following:
showFiles="$(defaults read com.apple.finder AppleShowAllFiles)"
if [ "$showFiles" = 1 ]
 then defaults write com.apple.finder AppleShowAllFiles -bool FALSE
 else defaults write com.apple.finder AppleShowAllFiles -bool TRUE
fi
This figures out whether the hidden files are currently showing, and then executes the appropriate command to toggle the preference. Next, add the Action called Run AppleScript, with the script set to this:
try
  tell application "Finder" to quit
on error
  -- Note that the error line below is shown on two lines
  -- (in order to narrow the display here on macosxhints),
  -- but must be entered as one line; just remove the line break
  error "Unable to quit Finder. You may want to try force quitting it
   (Command-Option-Escape) to have the change take effect."
end try
set finderIsActive to false
set errorCount to 0
repeat until finderIsActive
  try
    tell application "Finder" to activate
    set finderIsActive to true
  on error
    set errorCount to errorCount + 1
    if (errorCount > 30) then
      -- Note that the error line below is shown on two lines
      -- (in order to narrow the display here on macosxhints),
      -- but must be entered as one line; just remove the line break
      error "Unable to restart Finder. Please click the Finder 
       icon in the dock to restart it."
    end if
    delay 0.1
  end try
end repeat
This code tries to quit the Finder, presenting an error message if it doesn't work, then tries to restart the Finder every tenth of a second for three seconds until it either succeeds, or gives up upon failure, and presents another error message

Now all that's left is to save the file: Choose File -> Save As Plug-in, give it a name, and make sure the pop-up menu is set to Finder. You're done! All those unix files are now just a control- or right-click away.
    •    
  • Currently 2.20 / 5
  You rated: 2 / 5 (5 votes cast)
 
[33,295 views]  

10.4: Use Automator to toggle hidden Unix files' visibility | 14 comments | Create New Account
Click here to return to the '10.4: Use Automator to toggle hidden Unix files' visibility' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Use Automator to toggle hidden Unix files' visibility
Authored by: luhmann on Aug 17, '05 10:42:02AM

This doesn't work. Initially it was telling me that I was missing a "]" in the script. I replaced the one I copied and pasted with one from my keyboard. Then I started getting this error message:

"An error -: -c: line 2: syntax error near unexpected token 'then'"

No idea what to do about that.

And yes, I made sure it was set to /bash.



[ Reply to This | # ]
10.4: Use Automator to toggle hidden Unix files' visibility
Authored by: landers on Aug 17, '05 11:09:17AM

there's a missing ; after the ] in the bash script if statement.



[ Reply to This | # ]
10.4: Use Automator to toggle hidden Unix files' visibility
Authored by: WillisBWhite on Aug 17, '05 11:07:52AM

This hint worked out really well. Thanks! It will be nice now to be able to delete all the dotfiles and such without having to use only the Terminal.

One thing that I noticed, when I would have hidden files enabled, all of my files/folders show up as "invisible" or transparent icons. It's no big deal, but I was just wondering what would be the cause of that.



[ Reply to This | # ]
10.4: Use Automator to toggle hidden Unix files' visibility
Authored by: jacobolus on Aug 17, '05 07:19:00PM

As far as i can tell this is a bug in the finder. I'll go file a bug report.



[ Reply to This | # ]
10.4: Use Automator to toggle hidden Unix files' visibility
Authored by: albertoricart on Aug 17, '05 11:37:12AM

This can be more easily accomplished using DotFiles, a service that allows you to not only toggle the visibility of all files, but also toggle the display of icons on the desktop.

DotFiles can be found at: http://www.smartsoft.com/products/dotfiles/dotfiles.html.



[ Reply to This | # ]
10.4: Use Automator to toggle hidden Unix files' visibility
Authored by: sjmills on Aug 17, '05 01:12:15PM
After adding the missing ; after the ], I get an error:

An error /System/Library/Automator/Run Shell Script.action/Contents/Resources/main.command: line 20: [: too many arguments

Could it be because my default shell is still tcsh? If so, how do I fix the script?

[ Reply to This | # ]

10.4: Use Automator to toggle hidden Unix files' visibility
Authored by: googoo on Aug 17, '05 01:31:01PM

I am having the same problem. My default shell is tcsh. The Run shell script action in Automator has a shell selecter, but it is blank does not have any options. What gives?

-Mark



[ Reply to This | # ]
10.4: Use Automator to toggle hidden Unix files' visibility
Authored by: googoo on Aug 21, '05 10:13:39PM

I am convinced that there is a problem with the Run Shell Script action in my copy of Automator [v 1.01 (54) on OS X 10.4.2]. The shell pull-down is blank (with no choices at all). When I try to use the Run Shell Script action with any script of more than one line, I get the error mentioned above. When I look at the "/System/Library/Automator/Run Shell Script.action/Contents/Resources" directory, there is a file called Shells.plist, which is not blank. In fact, it has many shells in it (bash, tcsh, perl, python, etc). The command line plutil does not find any problem with Shells.plist or any other plist file in the Run Shell Script action. Any ideas?

-Mark



[ Reply to This | # ]
10.4: Use Automator to toggle hidden Unix files' visibility
Authored by: FFFish on Aug 17, '05 02:33:17PM

Some of you may be encountering problems with cut/pasting the bash script; try deleting and inserting the line break characters by hand. Worked for me.



[ Reply to This | # ]
10.4: Use Automator to toggle hidden Unix files' visibility
Authored by: luhmann on Aug 17, '05 03:28:14PM

Yes, re-entering all the carriage returns, as well as putting in the ";" after the "]" seems to have solved the problems.

I suggest MacOS X hints employ the same code that the MacScripter BBS uses so that there aren't these problems.



[ Reply to This | # ]
10.4: Use Automator to toggle hidden Unix files' visibility
Authored by: sjk on Aug 18, '05 03:08:58PM

I completely agree and have made similar suggestions in the past. Quite a few posts on this site are because of trouble caused by copy-paste of code from hints so everyone could benefit from anything that helps solve or minimize that problem. Rob?



[ Reply to This | # ]
10.4: Use Automator to toggle hidden Unix files' visibility
Authored by: echo on Aug 17, '05 03:59:30PM
I have been using my simplified version created from that 2003 hint, as an AppleScript in the menubar script menu. The funny thing is in subsequent OS versions, it worked sporadically, so I would need to sometimes run it multiple times for it to work, + I never perfected it. Since Tiger, the same script works flawlessly! It's debatable whether it's easier to access it from the menubar, where I have it inline with only 2 scripts, or by automator contextual menu, where you would find it in a submenu. Here is the script:

set OnOff to do shell script "defaults read com.apple.finder AppleShowAllFiles"
if inOff= "OFF" or inOff= "NO" or inOff= "0" then
	set OnOffCommand to "defaults write com.apple.finder AppleShowAllFiles ON"
else
	set OnOffCommand to "defaults write com.apple.finder AppleShowAllFiles OFF"
end if
tell application "Finder" to quit
do shell script OnOffCommand
delay 1
tell application "Finder" to launch


[ Reply to This | # ]
10.4: Use Automator to toggle hidden Unix files' visibility
Authored by: fracai on Aug 18, '05 09:01:47AM

put the Finder quit bit after the do shell script

my bet is that sometimes the Finder quits and restarts before your defaults write command has finished. in this case the Finder would load with hidden files still hidden as the new setting hasn't been read yet. running the toggle script again would toggle the setting back to hidden and it would again be down to luck.

or just create two scripts for on and off. then you don't need to worry about this issue at all.

---
i am jack's amusing sig file



[ Reply to This | # ]
10.4: Use Automator to toggle hidden Unix files' visibility
Authored by: verlhum on Oct 16, '05 07:23:25AM

I beleive you meant to say:
[code]
set OnOff to do shell script "defaults read com.apple.finder AppleShowAllFiles"
if OnOff= "OFF" or OnOff= "NO" or OnOff= "0" then
set OnOffCommand to "defaults write com.apple.finder AppleShowAllFiles ON"
else
set OnOffCommand to "defaults write com.apple.finder AppleShowAllFiles OFF"
end if
tell application "Finder" to quit
do shell script OnOffCommand
delay 1
tell application "Finder" to launch
[/code]

It threw me for a loop until I realized that inOff was supposed to be OnOff



[ Reply to This | # ]