Show Hidden Files

Any idea what is wrong with the syntax of this script?


tell application "Finder"
	
	display alert "Would you like to show or hide the hidden files?" buttons {"Hide", "Show"} default button "Hide" as warning
	if button returned of result is "Hide" then
		
		do shell script "defaults write com.apple.finder AppleShowAllFiles False ; killall Finder"
	else
		do shell script "defaults write com.apple.finder AppleShowAllFiles TRUE ; killall Finder"
		
	end if
end tell

I have tried changing the do shell scripts with dialogs and it works just fine and the shell scripts by themselves work just fine.


tell application "Finder"
	
	display alert "Would you like to show or hide the hidden files?" buttons {"Hide", "Show"} default button "Hide" as warning
	if button returned of result is "Hide" then
		
		display dialog "Hide"
	else
		display dialog "Show"
		
	end if
end tell


I find it easier to forget the dialog, and just toggle the setting when run:

-- toggle hidden files
try
	set toggle to do shell script "defaults read com.apple.finder AppleShowAllFiles"
	if toggle = "FALSE" then
		do shell script "defaults write com.apple.finder AppleShowAllFiles TRUE"
	else if toggle = "TRUE" then
		do shell script "defaults write com.apple.finder AppleShowAllFiles FALSE"
	end if
end try

-- restart/activate Finder
do shell script "killall Finder"
delay 0.5
activate application "Finder"

Hi,

the syntax is wrong. The value of the domain AppleShowAllFiles must be of type boolean

do shell script "defaults write com.apple.finder AppleShowAllFiles -bool TRUE"

without the -bool flag a string is written into the plist file.

By the way, I’m using this to toggle the state


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"


That is great that is actually what I was trying to do to begin with but it was not working for me. Thanks for the post!

This is still not working for me this is what I have.

tell application "Finder"
	
	display alert "Would you like to show or hide the hidden files?" buttons {"Hide", "Show"} default button "Hide" as warning
	if button returned of result is "Hide" then
		
		do shell script "defaults write com.apple.finder AppleShowAllFiles -bool false ; killall Finder"
	else
		
		do shell script "defaults write com.apple.finder AppleShowAllFiles -bool true ; killall Finder"
		
	end if
end tell

Both seem to show or hide the files when used by themselves

do shell script "defaults write com.apple.finder AppleShowAllFiles -bool TRUE"

or

do shell script "defaults write com.apple.finder AppleShowAllFiles TRUE ; killall Finder"

What is the disadvantage to writing it to the plist file?

I like the variations on the toggle theme thanks!

Cut the Finder tell block. It’s actually not needed.
Or at least put the shell script lines out of the application tell block.

Plist properties (called domains) can contain several classes like String, Number, Date, Array, Dictionary, Data.
The primitive types like integer, float and boolean derive from Number.

The classes are specified by the appropriate application. Changing the class by writing into the plist file via the command line can cause unexpected behavior to the point of crashing the application.

Wonderful thanks for the plist explanation that is good to know. The script works great now, it makes sense that the do shell script wouldn’t work in the tell finder statement, I see it now.

display alert "Would you like to show or hide the hidden files?" buttons {"Hide", "Show"} default button "Hide" as warning
if button returned of result is "Hide" then
	
	do shell script "defaults write com.apple.finder AppleShowAllFiles -bool false ; killall Finder"
else
	
	do shell script "defaults write com.apple.finder AppleShowAllFiles -bool true ; killall Finder"
end if

Always appreciated.