Little Help Please: Password protected hidden file toggle

Hi all!

I wonder if you can help me. I have used code from this forum to allow me toggle hidden files on and off. I am using this on teacher laptops in a school that i work in because us techs often have hidden files we need to see.

I would like to have a crude password protect on the script so that the tech has to type a password before the script will unhide the files.

this is what i have so far

on idle application "Finder"
	set thePass to text returned of (display dialog "Password?:" default answer "" buttons {"MR CRYPTO"} default button 1 giving up after 20 with hidden answer)
	if thePass is not "PASSWORD" then tell application "Finder" to shut down
end idle

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


I can seem to get it to work. I want it to shutdown the computer if the user gets the password wrong and for them to only have 20sec to enter the password.

Im not very good at scripting and i may have pieced together the wrong parts.

Any help very much appreciated.:smiley:
Mat

Hi GyroMat, you have a lot of unnecessary stuff in your code so I just rewrote it. I put a bunch of comments in my code so hopefully this will help you learn.

NOTE: we do not need to tell the Finder to do anything until we get to the end and quit/relaunch it. This is because the commands we use are applescript commands, not Finder commands.
NOTE: in my plist the 2 states of AppleShowAllFiles are “ON” or “OFF”, not 0 or 1. And when I try to use 0 or 1 it doesn’t work.

try
	set thePass to text returned of (display dialog "Password?:" default answer "" buttons {"MR CRYPTO"} default button 1 giving up after 20 with hidden answer)
	
	-- passwords should be case sensitive so we "consider case" when checking it
	considering case
		if thePass is not "PASSWORD" then tell application "Finder" to shut down
	end considering
	
	-- ask the user if he wants to perform the opposite state
	set currentState to do shell script "/usr/bin/defaults read com.apple.Finder AppleShowAllFiles"
	if currentState is "ON" then
		set oppositeState to "OFF"
		set dialogText to "Hide"
	else
		set oppositeState to "ON"
		set dialogText to "Show"
	end if
	display dialog (dialogText & " all invisible files in Finder?") buttons {"Cancel", (dialogText & " Invisible Files")} default button 2 with icon note
	
	-- the user does want to perform the opposite state so make it happen
	do shell script "defaults write com.apple.finder AppleShowAllFiles " & oppositeState
	RelaunchFinder()
on error errorMsg number errorNum
	if errorNum is not -128 then -- this is the cancel button error number
		display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons "Cancel" default button 1 with icon caution
	end if
end try

on RelaunchFinder()
	tell application "Finder" to quit
	
	-- delay until the Finder quits
	repeat
		delay 0.1
		try
			-- Here we check for the Finder process. When it can no longer be found this code errors. So when that happens we know the Finder is no longer running thus we can exit the repeat loop.
			tell application "System Events" to get first process whose name is "Finder"
		on error
			exit repeat
		end try
	end repeat
	
	tell application "Finder" to activate
end RelaunchFinder

Thats fantastic work thank you.

I have found some issues tho, for example, if you enter just any old password the user can get to the Show/Hide files part because OS X throw a messages saying shutdown aborted.

Is there any way around this? Also if they leave it for 20 sec rather than timing out and shutting down the machine it keep moves on to the next stage of show/hide files without a password.

Any ideas?

Thats again your fantastic!:smiley:

Any one else have any ideas? :confused:

Cheers

No idea why. When something’s running that cannot be quit the system would tell you what it is - at least in 10.6. Something along the lines of ‘could not shut down because app x cannot be quit’

set dialogResult to display dialog "Password?:" default answer "" buttons {"MR CRYPTO"} default button 1 giving up after 3
-- I did nothing, honest!
--> {text returned:"", button returned:"", gave up:true}

You should test the gave up property.