So I noticed someone posted a “show/hide hidden files in Finder” app to the front page today. It’s run-only, so I ran the script through /usr/bin/strings, and (of course) the shell commands aren’t full paths. Booooo.
I also don’t like the UI. It doesn’t reflect the state of the system–you don’t need to show hidden files if they’re already showing, for example.
I wrote one of these for an Apple Discussions post a while back, and figured I’d post it here.
-- toggle invisible files in finder
-- org dot bungie at mikey-san
on run
set hiddenState to (do shell script "/usr/bin/defaults read com.apple.Finder AppleShowAllFiles")
try
if hiddenState = "0" or hiddenState contains "does not exist" then
tell application "Finder" to set showPrompt to display alert "Are you sure you want to show all hidden files?" message "The Finder will quit and relaunch afterwards." buttons {"Cancel", "Show Hidden Files"} default button "Show Hidden Files"
if button returned of showPrompt = "Show Hidden Files" then
tell application "Finder" to quit
do shell script "/usr/bin/defaults write com.apple.Finder AppleShowAllFiles 1"
else if button returned of showPrompt = "Cancel" then
return 0
end if
else if hiddenState = "1" then
tell application "Finder" to set showPrompt to display alert "Are you sure you want to hide all hidden files?" message "The Finder will quit and relaunch afterwards." buttons {"Cancel", "Hide Hidden Files"} default button "Hide Hidden Files"
if button returned of showPrompt = "Hide Hidden Files" then
tell application "Finder" to quit
do shell script "/usr/bin/defaults write com.apple.Finder AppleShowAllFiles 0"
else if button returned of showPrompt = "Cancel" then
return 0
end if
end if
on error theError
FinderCheck()
display alert theError as critical message "Changes may or may not have been made."
end try
FinderCheck()
end run
on FinderCheck()
tell application "System Events"
if (name of every process) does not contain "Finder" then
tell application "Finder" to launch
end if
end tell
end FinderCheck
Moving to Code Exchange. [A nice change. ;)]
Ack. I swear I checked the forum section before I posted. I swear.
I deleted that key to test the results. It throws an error:
This seems to work nicely:
try
do shell script "/usr/bin/defaults read com.apple.Finder AppleShowAllFiles"
get result as integer
on error
get 0
end try
set i to (result + 1) mod 2 -- Get opposite of current value
tell application "Finder"
activate
get item (i + 1) of {"Hide", "Show"}
display dialog (result & " all hidden files in Finder?") buttons {"Cancel", (result & " Hidden Files")} default button 2 with icon note
quit
end tell
try
do shell script "/usr/bin/defaults write com.apple.Finder AppleShowAllFiles " & i
launch application "Finder"
on error errorMsg number errorNum
display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
end try
Interesting that neither of these restarts my Finder - I have to click it in the Dock to get the window back.
Edit: I should add that I can quit my Finder from the File Menu.
I’d be interested to know why it’s worked on multiple machines for me in the last couple of months, yet not for /either/ of you.
Heck, I just tested the original again and it worked without issue. Stock box, no mods or third-party script software at all.
AppleScript: The land of a thousand inconsistencies.
Edit: Can you guys give me some well-logged output of the original so I can see what it’s doing or not doing on your setups?
Check your e-mail if you haven’t already.
I’ve been following threads on various web sites for about a year, dealing with “toggle hidden files” scripts. One of the most common problems people report, is the error also described in this thread, where “read com.apple.Finder AppleShowAllFiles” returns the error “The domain/default pair of (com.apple.Finder, AppleShowAllFiles) does not exist”. This error is returned since the plist file com.apple.Finder (at ~/Library/Preferences) doesn’t normally contain a sibling named AppleShowAllFiles, but issuing the command ‘do shell script "defaults write com.apple.Finder AppleShowAllFiles " & i’ (where i is either 1 or 0) will insert the AppleShowAllFiles sibling into com.apple.finder.plist, after which it can be read, naturally, without returning that error. Another method of inserting AppleShowAllFiles into an unmodified com.apple.finder.plist, is to use a utility like Onyx, Cocktail, etc. to show hidden files.
Bruce’s script handles the condition where “read com.apple.Finder AppleShowAllFiles” returns the “does not exist” error, by using “on error” and “set i to.” to determine what value it should write to AppleShowAllFiles–none of the other toggle scripts I’ve seen has an “on error” handler for this condition, or a proper one. I know this explanation is obvious to a scripter, but I thought I’d explain it to people not totally familiar with scripting who may come across this thread looking for an explanation for the “does not exist” error.
Since I’m only an occasional Applescripter, I’m not clear on Mikey-san’s use of /usr/bin/ placed before “defaults write” and “defaults read”. I found that removing /usr/bin/ still allowed the script to work. He says “I ran the script through /usr/bin/strings, and (of course) the shell commands aren’t full paths.” Is it just a way of guaranteeing the path to com.apple.Finder.plist is followed?
As for the problem some people encounter, where “launch application Finder” or similar doesn’t work, here’s a solution (which I’ve inserted into Bruce’s script) from a user named hofman at www.macosxhints.com/article.php?story=20030409015020645
–it quits from and reliably relaunches the Finder, using a loop to accomplish the relaunch, since sometimes issuing the command just once is ineffective.
try
do shell script "/usr/bin/defaults read com.apple.Finder AppleShowAllFiles"
get result as integer
on error
get 0
end try
set i to (result + 1) mod 2 -- Get opposite of current value
tell application "Finder"
activate
get item (i + 1) of {"Hide", "Show"}
display dialog (result & " all hidden files in Finder?") buttons {"Cancel", (result & " Hidden Files")} default button 2 with icon note
quit
end tell
try
do shell script "/usr/bin/defaults write com.apple.Finder AppleShowAllFiles " & i
my RelaunchFinder()
on error errorMsg number errorNum
display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
end try
on RelaunchFinder()
try
tell application "Finder" to quit
on error
error "Unable to quit Finder. You may want to force quitting it (cmd-option-esc) 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 > 20) then
error "Unable to restart Finder. Please click the Finder icon in the dock to restart it."
end if
delay 0.2
end try
end repeat
end RelaunchFinder
Hi,
considering Panther and Tiger uses a string value (ON/OFF) and Leopard a boolean value for the AppleShowAllFiles key
I’m using this script, it just toggles the value, I had never ever any problem with the Finder relaunch
set Leopard to (system attribute "sysv") mod 4096 div 16 = 5
quit application "Finder"
try
if Leopard then
set b to (not ((do shell script "/usr/bin/defaults read com.apple.finder AppleShowAllFiles") as integer as boolean)) as integer
else
set b to (((do shell script "/usr/bin/defaults read com.apple.finder AppleShowAllFiles") is "ON") as integer) + 1
end if
on error
set b to 1
end try
if Leopard then
do shell script "/usr/bin/defaults write com.apple.finder AppleShowAllFiles -bool " & item (b + 1) of {"No", "Yes"}
else
do shell script "/usr/bin/defaults write com.apple.finder AppleShowAllFiles O" & item b of {"N", "FF"}
end if
delay 1
launch application "Finder"
Hi Stefan,
I don’t know where you are seeing this ‘ON/OFF’ value. By default, there is no value for this preference, as only third-party utilities will write it. A boolean would be the expected type for this sort of pref, and works fine on my 10.4 machine.
Anyway, here is a revised script. (Not that we really need another, it’s just shorter)
try
set newPref to (do shell script "/usr/bin/defaults read com.apple.finder AppleShowAllFiles") is "0"
on error
-- The default value is 'false', so the opposite is 'true'.
set newPref to true
end try
set enableText to item ((newPref as integer) + 1) of {"Disable", "Enable"}
display dialog enableText & " showing invisible files in the Finder? The Finder will be relaunched." & return & return & "This command will automatically proceed in 5 seconds." giving up after 5
quit application "Finder"
set boolString to item ((newPref as integer) + 1) of {"NO", "YES"}
do shell script "/usr/bin/defaults write com.apple.finder AppleShowAllFiles -bool " & boolString
tell application "Finder" to launch
Note: I did see launch application “Finder” occasionally not work. I’ve used tell application “Finder” to launch instead; I don’t really know if it makes a difference. Otherwise, just use something like application “Finder”'s properties, which will ensure that the Finder is open.
Qwerty:
You’re right that there doesn’t seem to be an OS X default for the type of variable used by AppleShowAllFiles, since there are no built-in OS X routines or utilities to create or set this variable (maybe in Developer Tools?), but I’ve found that OS 10.4 and 10.5 are happy with any of these–whether boolean (yes/no) or a string (On/Off, or 0/1).
As you’ve found (and as I found with your script’s ‘tell application “Finder” to launch’), trying to relaunch the Finder doesn’t always work the first time. The relaunchFinder routine in my post above will relaunch Finder more reliably, since it contains a loop that tries 20 times, and if Finder still won’t launch, displays an error to that effect.
It works reliably, if there is a delay (1 sec. is sufficient) between quit and relaunch of the Finder
Thanks, Stefan. I wasn’t sure what the missing factor was. I inserted a ‘delay 1’ into Qwerty’s script, just prior to ‘tell application “Finder” to launch’, and now it reliably relaunches the Finder. There may be no need for the slightly elaborate loop in the script I posted above.
That’s what I’m trying to say with:
hey all,
I was just browsing around in this section of the forums, and decided it would be a nice script to use. I really enjoy making icons as well, as I like a more direct connection to scripts than having to just go up to the script menu in the menu bar. I used the script written by StefanK (which works perfectly) and made a little app called "hide’
I also modified one of Apple’s icons to use for the app. works very well as a Dock app, and even better as one in the finder Toolbar:
http://homepage.mac.com/andrew.hake/Images/hide_screenshot01.png
http://homepage.mac.com/andrew.hake/Images/hide_screenshot02.png
and here is the App:
http://homepage.mac.com/andrew.hake/Developer/hide_1.0.dmg