PDA

View Full Version : AppleScript, tell how long since last opening?




kyle1320
Jun 17, 2011, 10:27 PM
Hey guys, I'm an AppleScript newb, and I'm wondering what the best way of doing this is. I'm trying to create a program where if you enter the password correctly, a dialog box pops up, but if you enter the wrong password 3 times you get kicked out and can't try again for 5 minutes. I am saving the time using
do shell script "date '+%M%S'" , and everything is working so far, but I realized with the way I have it set up, if you try again an hour later, it will still say you must wait 5 minutes. I could add the year, month, and day, but then if you came back a year later it would do the same. (EDIT: realized you'd have to come back 100 years later, hmmm) :P Is there a better way of doing this? Here's my code:


set finishTime to do shell script "awk '{ print }' /Users/kyle1320/Desktop/save.txt"
set userMustWait to 0
set startTime to do shell script "date '+%M%S'"

if startTime - finishTime < 500 then
if startTime - finishTime > 0 then
display dialog "You must wait 5 minutes before trying again."
set userMustWait to 1
end if
if startTime - finishTime ≤ 0 then
if finishTime - startTime > 5500 then
display dialog "You must wait 5 minutes before trying again."
set userMustWait to 1
end if
end if

end if

set unlocked to 0
set saveFile to "/Users/kyle1320/Desktop/save.txt"
set pass to "ilikecherrypie"
set numTries to 0
set triesTillKickedOut to 3
repeat triesTillKickedOut times

if userMustWait is 1 then exit repeat

display dialog triesTillKickedOut - numTries default answer ""
set userPassword to text returned of result
if userPassword is pass then set unlocked to 1
set numTries to numTries + 1
if unlocked is 1 then

display dialog "You win! Now get out." buttons ("OK") default button 1

exit repeat
end if

end repeat

if unlocked is 0 then

if userMustWait is 0 then

display dialog "Maximum attempts exceeded."
set finishTime to do shell script "date '+%M%S'"
display dialog finishTime default answer ""
if text returned of result is "resetTime" then set finishTime to 0
-- if user enters "resetTime" into text field, they will not have to wait 5 minutes next time

set theText to finishTime
set theFilePath to (path to desktop as string) & "save.txt" as string
set theFileReference to open for access theFilePath with write permission
write theText to theFileReference
close access theFileReference
end if
end if


This is my first code with AppleScript, so there are probably a lot of inefficiencies. The only other thing I have a question about is in
display dialog triesTillKickedOut - numTries default answer ""
set userPassword to text returned of result
How would I make it say "You have (triesTillKickedOut - numTries) tries left"? Thanks :D



jiminaus
Jun 18, 2011, 12:10 AM
display dialog "You have " & (triesTillKickedOut - numTries) & " tries left" default answer ""


Use the AppleScript date object instead of the Unix date program, and store both the date and time in the file.

Load with


set saveFilePosixPath to POSIX path of (path to desktop) & "save.txt"

set saveFileExists to false
try
info for (POSIX file saveFilePosixPath)
set saveFileExists to true
end try

set userMustWait to 0
if saveFileExists then
set finishTimeString to do shell script "awk '{ print }' " & saveFilePosixPath
set waitMinutes to ((date finishTimeString) - (current date) + (6 * 60)) div 60
if waitMinutes > 0 then
display alert "You must wait " & waitMinutes & " minutes before trying again." giving up after 10
set userMustWait to 1
end if
end if


Save with:

set finishTime to current date
set finishTimeString to (short date string of finishTime) & " " & (time string of finishTime)
display dialog finishTimeString default answer ""
-- if user enters "resetTime" into text field, they will not have to wait 5 minutes next time
if text returned of result is "resetTime" then
set year of finishTime to 2000
set finishTimeString to (short date string of finishTime) & " " & (time string of finishTime)
end if
set theText to finishTimeString
set theFileReference to open for access POSIX file saveFilePosixPath with write permission
write theText to theFileReference
close access theFileReference

kyle1320
Jun 18, 2011, 09:35 AM
Thanks, I need a default time and date to put in the text file though or else it gives me an error while trying to read it, since it's not in the proper format

jiminaus
Jun 18, 2011, 06:56 PM
Thanks, I need a default time and date to put in the text file though or else it gives me an error while trying to read it, since it's not in the proper format

The loading code I gave above checks that file exists before trying to read it. If the file doesn't exist, the user is allowed to attempt a login.

I've quoted the code below and highlighted the file detection logic in green.



set saveFilePosixPath to POSIX path of (path to desktop) & "save.txt"

set saveFileExists to false
try
info for (POSIX file saveFilePosixPath)
set saveFileExists to true
end try

set userMustWait to 0
if saveFileExists then
set finishTimeString to do shell script "awk '{ print }' " & saveFilePosixPath
set waitMinutes to ((date finishTimeString) - (current date) + (6 * 60)) div 60
if waitMinutes > 0 then
display alert "You must wait " & waitMinutes & " minutes before trying again." giving up after 10
set userMustWait to 1
end if
end if