Folder Action Setup Password Protecting Folders

Hi guys,

I’m just a little frustrated right now and would just like some straight up answers to my problem.

I’ve just started programming in applescript and I’ve learned everything I know from the wikibook on applescripting. What I want to do is, when I click to open the folder that I attached my script to, using Folder Actions Setup, it would then pop-up a display dialog asking for the password and if correct then open said folder.

Here’s my code so far:

set strpassword to "heyhey"
set submission to ""
set intcount to 0
repeat until submission = strpassword
	display dialog "Password Please" buttons {"Cancel", "Submit"} default button 2 default answer ""
	set submission to text returned of result as string
	
	if submission = strpassword then
		
	else
		set intcount to intcount + 1
		
		if intcount = 3 then
			return "User Cancelled"
		else
			display dialog "Try Again" buttons "OK" default button 1
		end if
	end if
end repeat

Thanks for the help.

Welcome. :slight_smile:

Code Exchange: Post only full snippets/solutions

thanks for moving it :slight_smile:

Hi,

IMHO this is not very secure for a couple of reasons.

¢ you can access the folder from the terminal anyway
¢ the folder action can be detached very easily
¢ while running the script you can switch to the finder and open the folder
¢ the most crucial reason is:
To avoid reason #3 you have to close the folder immediately before retrieving the password.
Reopen it by the script causes in infinite loop.

Better use a password protected disk image :slight_smile:

I have never made that work, Stefan. If I go to the Disk Utility, click on “New Image” in its tool bar, enter a name, select a size, select Encription, leave it as a Read/Write disk image, and click “Create”, I get a dialog asking for a Password, enter one twice, and click “OK”, I always get ‘Unable to Create “someName.dmg” - Authentication error.’ Works fine without a password, never with one.

that’s odd, on my machine it works how it should.

PS: Is the behavior the same, when you uncheck to keep the password in the keychain?

I just want to know how to finish it, even if it is insecure, because it will be just used to fool my family on this computer.

Okay Sir, try this

property strpassword : "heyhey"

on opening folder thisFolder
	set intcount to 0
	repeat
		try
			display dialog "Password Please" buttons {"Cancel", "Submit"} default button 2 default answer ""
			set submission to text returned of result as string
			if submission = strpassword then exit repeat
			set intcount to intcount + 1
			if intcount = 3 then
				error -128
			else
				display dialog "Try Again" buttons "OK" default button 1 giving up after 2
			end if
		on error
			tell application "Finder" to close window (name of thisFolder)
			exit repeat
		end try
	end repeat
end opening folder

save it in /Library/Scripts/Folder Action Scripts/ and attach it to your folder

Thank you very much for your quick response, I’m just going to change it so it first closes the window and then opens it again when the password is correct

This won’t work.
If you open the window in the script, the handler will be called again and so on.
My script avoids this problem by closing the window, if the password is false or another error occured

yeah but you can still click on the folder behind it, how could we fix this?

could we minimize or hide the window and if its brought to the front again, it is then minimized or hide
or
write a save file, reading false, the program checks it and closes the folder, writes true, then next time when the program runs to open the folder, the program checks the file, doesn’t close the folder, and writes false.

As mentioned above, protecting a folder with AppleScript is a joke,
you can’t do this seriously

Yeah but if I knew how to write to a file and and display the characters in the display dialog as “*”, it would fool any basic user

Like suggested, try to create an encrypted sparseimage (which grows, unlike static dmgs). Using folder action, anyone can add the folder to the Dock and browse from there. Not to mention folder action is NOT activated by default. Anyone with enough permissions to read your folder can access it if he disable folder action.

I agree that AS isn’t a very secure way to protect anything, but I’ve previously added some rudimentary privacy functions by way of shifting folders into an invisible folder’s location (or making them invisible with a scripting addition). Basically, if someone attempts to open your folder, it immediately closes and relocates out of view, presenting a dialog that makes the folder available again on password receipt. Maybe that approach might work okay for you.