我在OSX 10.6.7下的Xcode V3.2.6中构建了一个基于AppleScript的应用程序。 我想为我的应用程序添加其他代码,因此在系统启动时,它会将我在应用程序中设置的日期与系统日期进行比较。 如果指定范围内的日期,请继续。如果日期检查超出范围,则立即终止程序。
目前代码看起来像这样:
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
Chuck发布的这篇文章中,有一位非常优秀的用户发布了一些内容。该代码在Apple脚本编写器下工作得很好,但在我粘贴到Xcode时却没有。 谁能告诉我我做错了什么?
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
非常感谢!
答案 0 :(得分:1)
自从我使用以前称为AppleScript Studio的技术以来,已经有很长一段时间了。 :)但是,我刚刚创建了一个Cocoa-AppleScript应用程序,我发现默认情况下文件UntitledAppDelegate.applescript
具有以下内容:
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
注意on applicationWillFinishLaunching_
处理程序和Xcode放置的注释。这是您希望放置将在程序启动时执行的代码的位置。例如,我在那里放置了一个beep
语句,并且应用程序在启动时出现了问题。所以我猜你可能会有这样的事情:
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_