noobie here...Need help with visible/invisible in Finder

Hello all, this is an awesome website, and i hope to learn a lot here. This is my first “program” in AppleScript, and I just need some pointers. Here is the code (go easy:)):
set HideFile to display dialog “Hidden Files Visible or Invisible?” buttons {“Visible”, “Invisible”} default button “Visible”

set HideFile to display dialog "Hidden Files Visible or Invisible?" buttons {"Visible", "Invisible"} default button "Visible"
if HideFile = "Visible" then
	do shell script "defaults write com.apple.finder AppleShowAllFiles TRUE"
else
	do shell script "defaults write com.apple.finder AppleShowAllFiles FALSE"
end if
do shell script "killall Finder"

I know some terminal commands, but I’m new to AppleScript, so if there is a way to do this “terminal free”, that would be awesome.

The code will not make the files invisible. If I do the Invisible command through the Terminal, however, the AppleScript “Visible” WILL work. Can someone tell me what’s breaking it?

Also, how would I apply a “sudo” password through AppleScript?

Thanks for the help!

with administrator privileges is the line you need.

set HideFile to display dialog "Hidden Files Visible or Invisible?" buttons {"Visible", "Invisible"} default button "Visible"
if HideFile = "Visible" then
	do shell script "defaults write com.apple.finder AppleShowAllFiles TRUE" with administrator privileges
else
	do shell script "defaults write com.apple.finder AppleShowAllFiles FALSE" with administrator privileges
end if
do shell script "killall Finder"

Thanks for the quick reply, but I’m getting this now:

Expected “given”, “with”, “without”, other parameter name, etc. but found identifier.

EDIT: There is no space in between “administrator” and “priveleges”.

The code still will not make the files invisible however. :confused:

it took me a second but it was always coming up invisable notice the change in the second line.
but this works for me with out admin priveleges

nice little script though i even thought of making dot files visible, but thats cause i terminal more than finder


display dialog "Hidden Files Visible or Invisible?" buttons {"Visible", "Invisible"} default button "Visible"
set HideFile to button returned of the result
if HideFile = "Visible" then
	
	do shell script "defaults write com.apple.finder AppleShowAllFiles TRUE "
else
	do shell script "defaults write com.apple.finder AppleShowAllFiles FALSE "
end if

do shell script "killall Finder"

That worked perfectly, I guess I forgot to declare the variable “HidFile” so that it could actually equal the results of the button. I’m learning! :smiley:

Is there a command that simply “does nothing”? I’m wanting to add a “nevermind”, so that I don’t have to kill Finder. Whenever I click “nevermind” I’d like the program to close when “nevermind” is clicked. Anyway to do that?

do some checking before the statement it will only kill finder if the button is not equal to nevermind

display dialog "Hidden Files Visible or Invisible?" buttons {"Visible", "Invisible", "nevermind"} default button "Visible"
set HideFile to button returned of the result
if HideFile is not equal to "nevermind" then
	if HideFile = "Visible" then
		
		do shell script "defaults write com.apple.finder AppleShowAllFiles TRUE "
	else
		do shell script "defaults write com.apple.finder AppleShowAllFiles FALSE "
	end if
	do shell script "killall Finder"
end if

If you have a lengthy script, and wish to avoid wrapping the rest of your routine in an if/then block, there are also a number of alternatives that you might like to consider.

Simplest of all, you can use a button labelled “Cancel” - and clicking this will halt the script. (If you don’t specify any buttons, {"Cancel, “OK”} will be used anyway - with “OK” being the default.)

display dialog "Continue?" buttons {"Cancel", "Continue"} default button 2
display dialog "You clicked Continue!"

If you really don’t want your cancel button to be labelled “Cancel”, then you can simulate a user cancellation by invoking an error number -128 (the number of the error produced when a user cancels).

display dialog "Continue?" buttons {"Never Mind", "Continue"} default button 2 cancel button 1
if button returned of result is "Never Mind" then error number -128
display dialog "You clicked Continue!"

However, since AppleScript 1.10 (Mac OS X 10.4), the display dialog command has a cancel button parameter that specifies the name or number of the cancel button. So, as long as you don’t need your script to work on a pre-Tiger machine, you can now do this:

display dialog "Continue?" buttons {"Never Mind", "Continue"} cancel button 1 default button 2
display dialog "You clicked Continue!"

:slight_smile: