On May 29, 2011, at 1:18 PM, John Baltutis wrote: On 5/27/11, Jon Pugh < email@hidden> wrote: At 6:21 PM -0700 5/27/11, Jon Pugh wrote:
-- turn invisibles on and off in the Finder, requires restarting the Finder
tell application "Finder" to quit
set inOff to do shell script "defaults read com.apple.finder
AppleShowAllFiles"
if inOff = "NO" or inOff = "OFF" then
set onOffCmd to "ON"
else
set onOffCmd to "OFF"
end if
set onOffCmd to "defaults write com.apple.finder AppleShowAllFiles "& onOffCmd
do shell script onOffCmd
delay 1
tell application "Finder" to launch
I like this one: if (do shell script "defaults read com.apple.finder AppleShowAllFiles") is equal to "0" then do shell script "defaults write com.apple.finder AppleShowAllFiles 1" else do shell script"defaults write com.apple.finder AppleShowAllFiles 0" end if do shell script "killall Finder"
The problem with both of these is that a true value for AppleShowAllFiles can be specified as:
defaults write com.apple.finder AppleShowAllFiles -string YES defaults write com.apple.finder AppleShowAllFiles -string ON defaults write com.apple.finder AppleShowAllFiles -string true defaults write com.apple.finder AppleShowAllFiles -int 1 # or any non-zero integer. "-int" is optional
In all of those, "-string" or "-int" can be omitted with no change in meaning, or replaced by "-bool", which does change the meaning but still works. To set it to a false value, use any of NO, OFF, false, or 0.
Finder itself is going to test the value using the _expression_:
[[NSUserDefaults standardUserDefaults] boolForKey:@"AppleShowAllFiles"]
The -(BOOL)boolForKey: method folds all of those values to one of the BOOL values YES or NO (not the string values "YES" or "NO"). However, defaults read will return the actual value used in the last defaults write command (unless the write explicitly specified a type of -bool, in which case the read will return 1 or 0). Finder itself never sets the value, so we have no way of saying that one form or another is canonical.
John Pugh's method tests for ON/OFF or YES/NO, but misses testing for 1/0 or true/false. John Baltutis' method tests for 1/0, but omits testing for ON/OFF, YES/NO, or true/false.
Kudos to Pugh, for using 'tell application "Finder" to quit', BEFORE examining or changing its defaults. It's unsafe and generally ineffective to modify defaults for a running application.
Baltutis: "killall" is extremely rude, and should be avoided whenever possible. The killed application gets no chance to clean up after itself. Any state Finder has been holding in RAM, like for example the sizes, positions, and views of open windows, dies with the application. Anything Finder was doing at the time, like updating .DS_Store files, gets aborted in the middle, potentially leaving corrupted files in its wake. I'm not sure what happens to file copies Finder was in the middle of. (It farms those off to raccoon, and I don't know if raccoon gets killed too.) There's also a race condition: Finder could manage to overwrite your value for AppleShowAllFiles with its own just before it dies. That's a tiny window of opportunity, but it's there.
I would suggest Pugh's method, but the test should be:
if inOff is in {"NO", "OFF", "false", "0"} then
-Ron Hunsinger |