A little history first:
I have a hidden folder that requires admin privileges to open. It contains admin tools and scripts. For quick access I have saved a script bundle to the desktop which will open the hidden folder. Just for fun I have added three icons to it’s Resource folder (applet.icns, openLock.icns & closedLock.icns) so that the bundle’s icon changes; triggered by actions in the script.
You can download the icons for the script here: http://www.mediafire.com/?sharekey=3bb0987b4e883a69d0d290dca69ceb5ce04e75f6e8ebb871
Save the script as an application bundle and then copy the icons to the Resource folder inside of the bundle. Be sure to save it as “Admins Only” or change the appropriate lines of code in the script or it will fail.
For the purpose of this example the script will open your home folder when the correct password is entered. The password is ( enter ).
This script will prompt for the password up to three times if entered incorrectly. Each time the prompt is displayed the prompt and the label for the cancel button changes. If the user fails to enter a correct password on the third attempt it displays an alert that a log of the incident has been created and saves a log file. --For the purpose of this example no log file will be created.
On successful password entry the hidden folder will open and the script bundle icon changes to that of an open lock. As of now the lock changes back to the closed lock icon after a delay of 1.5. What I would like to happen is for the icon to change back only after the folder is closed.
So what I need to do is have the script attach and enable a folder action to the hidden folder that will change the icon once the folder is closed. I have tried including an “on close” folder action script in the Resource folder and have the main script attach and enable it for the hidden folder but this attempt has failed. The second thing I would like for the “on close” script to do is either disable folder actions for the hidden folder and then delete itself from the folders actions or at the least disable folder actions for that folder. The reason for not simply adding the action script to the folder in question manually and then enabling it from the script is that I would like to automate the process for any new folders I have the script open.
Any ideas?
--Admins Only
--Created by DemonXstreeM
-- setting globals
global cnt
global trCnt
global pasKey
global trMsg
global cnMsg
global myPath
global opI
global clI
global cuI
-- end setting globals
-- setting variables
set cnt to 0
set trCnt to 1
set trMsg to {"First Attempt", "Second Attempt", "Final Attempt"} -- setting prompt list
set cnMsg to {"Cancel", "Never Mind", "I Give Up"} -- setting cancel button list
tell application "Finder" to get folder of (path to me) as Unicode text -- setting working directory and path to resources
set workingDir to POSIX path of result
set myPath to (workingDir & "Admins Only.app/Contents/Resources/") as string -- end setting working dir and path
set opI to (myPath & "openLock.icns") as string -- setting icon paths
set clI to (myPath & "closedLock.icns") as string
set cuI to (myPath & "applet.icns") as string -- end setting icon paths
-- end setting variables
on chk() -- defining the function
if (cnt < 3) then -- execute if attempts less then 3
-- prompt user for password
set pasKey to display dialog "Enter passkey " & item trCnt of trMsg default answer "******" buttons {item trCnt of cnMsg, "Ok"} default button "Ok" cancel button item trCnt of cnMsg with title "Open the Vault" with hidden answer
-- step counters
set cnt to cnt + 1
set trCnt to trCnt + 1
if (the text returned of pasKey is "enter") and (cnt ≤ 3) then -- execute if less or equal to 3 and correct password
do shell script ("cp " & "'" & opI & "'" & " " & "'" & cuI & "'") --change app icon to openLock
tell application "Finder" to update items of desktop --refresh desktop
do shell script ("open $HOME") --open folder
delay 1.5
do shell script ("cp " & "'" & clI & "'" & " " & "'" & cuI & "'") --change icon back to closedLock
tell application "Finder" to update items of desktop --refresh desktop
else if (cnt ≤ 3) then -- execute if less or equal to 3 and wrong password
say "Incorrect Password!" using "zarvox"
chk() --recall function
end if
else -- execute after final attempt to enter password fails
say "Congratulations!, You Fail!" using "zarvox"
display alert "Have a good day" message "Your actions have been logged" as warning giving up after 10
end if
end chk -- end defining function
chk() -- calling the function
After thoughts…
This example demonstrates how your script can accept a password before it will open any file(s), folder(s) or even another app all of which may be hidden.
It also demonstrates how to set maximum attempts. In this case it simply displays a message letting the user know that a log file has been created and then cancels the dialog on failure or opens the users home directory on successful entry of the password. No log entry has been created for this example but you should be able to see where it could be handled.
This could be taken a step further by having this app check with another app to see if it can open the dialog in the first place.
e.g. After three failures to enter the correct password it would reset a timer in the launch verification script; preventing the dialog from being opened until the timer has run out. Or perhaps the verification script could be set to lockdown mode which can only be released by the administrator using a hidden bash command or the like.
Any way have fun trying out different techniques and putting it to different uses.
Thx, DXS
–exitus acta probat