系统日期在应用程序启动时验证

发布于 2024-11-14 06:23:10 字数 988 浏览 3 评论 0原文

我在 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 scripter 下工作得很好,但当我粘贴到 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 

非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洒一地阳光 2024-11-21 06:23:10

我已经很长时间没有使用以前称为 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_

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_
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文