Show or Hide Invisible Files Utility - Feedback wanted

Tested on OS X 10.8. Thoughts?

set {_prompts, _buttons} to {{_yes:"Files are visible.", _no:"Files are hidden."}, {_yes:"Hide Files", _no:"Show Files"}}

repeat
	
	set visibility to (do shell script "defaults read com.apple.finder AppleShowAllFiles")
	
	if visibility is "YES" then
		set _prompt to _yes of _prompts
		set _button to _yes of _buttons
	else if visibility is "NO" then
		set _prompt to _no of _prompts
		set _button to _no of _buttons
	end if
	
	activate me
	
	set action to button returned of (display dialog _prompt buttons {"Quit", _button} with title "Show/Hide Files" cancel button 1 default button 2 with icon 2)
	
	if action is "Hide Files" then
		do shell script "defaults write com.apple.finder AppleShowAllFiles NO"
	else if action is "Show Files" then
		do shell script "defaults write com.apple.finder AppleShowAllFiles YES"
	end if
	
	display dialog "Restart Finder?" with title "Show/Hide Files" buttons {"No", "Yes"} default button 2
	if button returned of result is "No" then exit repeat
	
	tell application "Finder" to quit
	display dialog "Finder restarting..." with title "Show/Hide Files" buttons {""} giving up after 2
	
	if gave up of result is false then
		delay 2
	else
		tell application "Finder" to activate
	end if
	
end repeat

Model: MacBook Pro 15"
AppleScript: AppleScript 2.3.1
Browser: Safari 537.74.9
Operating System: Mac OS X (10.8)

Hi,

the key AppleShowAllFiles returns a boolean value which is passed thru the shell as "0’ or “1”.
To get a real boolean value you have to write


	set visibility to ((do shell script "defaults read com.apple.finder AppleShowAllFiles") as integer) as boolean

then you can check


	if visibility then
		set _prompt to _yes of _prompts
		set _button to _yes of _buttons
	else
		set _prompt to _no of _prompts
		set _button to _no of _buttons
	end if

to set the value the proper syntax is


do shell script "defaults write com.apple.finder AppleShowAllFiles -bool NO" -- or YES

I’m using this to just toggle the state of AppleShowAllFiles and restart the Finder


try
	set state to ((do shell script "/usr/bin/defaults read com.apple.finder AppleShowAllFiles") as integer) as boolean
on error
	set state to false
end try
do shell script "/usr/bin/defaults write com.apple.finder AppleShowAllFiles -bool " & ((not state) as text) & "; killall Finder"


If you want to choose, rather than toggle:

set buttonpressed to button returned of (display dialog "Show Hidden Files?" buttons {"Yes", "No"})

try
	if the buttonpressed is "No" then do shell script "defaults write com.apple.finder AppleShowAllFiles OFF"
	if the buttonpressed is "Yes" then do shell script "defaults write com.apple.finder AppleShowAllFiles ON"
	do shell script "killall Finder"
end try

No need to coerce text to integer to boolean:

"yes" as boolean
--> true

I’m not sure what you excactly mean but the returned results of a boolean in shell is either “0” or “1” and not “YES” or “NO”. That is only how you are setting the data. StefanK’s code is correct, a direct as boolean will return in an error.


"1" as boolean
--> error