System date validate upon application launch
I have built an applescript based app in Xcode V3.2.6 under OSX 10.6.7. I want to have additional code to my application so when the system launched, it will compare the date I set in the application with the system date. If date within range specified, proceed. If date check is out of range then terminate program immediately.
currently the code looks something like this:
on clicked the Object
if name of theObject = "One" then
try
display dialog "ONE"
end try
else开发者_运维知识库 if name of theObject = "two" then
try
display dialog "TWO"
end try
end if
end clicked
on action theObject
end action
One of the very nice users in this fourm post Chuck has posted something. The code works great under apple scripter but not when I pasted into the Xcode. Can any one tell me what I am doing wrong?
posted by Chuck
set range to 3 * days
set targetDate to (date "Saturday, March 19, 2011 12:00:00 AM")
set currDate to current date
if abs(targetDate - currDate) > range then
display dialog "quit"
else
display dialog "continue"
end if
on abs(n)
if n < 0 then
return -n
else
return n
end if
end abs
Thanks so much!
It's been a long time since I worked with the technology formerly known as AppleScript Studio. :) However, I just created a Cocoa-AppleScript application and I see that the file UntitledAppDelegate.applescript
has the following by default:
script UntitledAppDelegate
property parent : class "NSObject"
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
end applicationWillFinishLaunching_
on applicationShouldTerminate_(sender)
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate_
end script
Note the on applicationWillFinishLaunching_
handler and the comment placed there by Xcode. This is where you want to place code that will execute when the program launches. For example, I placed a beep
statement in there and the application beeped when it launched. So I'm guessing you could have something like this:
on applicationWillFinishLaunching_(aNotification)
-- Insert code here to initialize your application before any files are opened
set range to 3 * days
set targetDate to (date "Saturday, March 19, 2011 12:00:00 AM")
set currDate to current date
if (currDate - targetDate) > range then
display dialog "quit"
else
display dialog "continue"
end if
end applicationWillFinishLaunching_
精彩评论