无法使用ApplescriptObjC创建事件(但代码适用于直接的Applescript)

时间:2014-09-08 18:35:35

标签: date calendar applescript applescript-objc

我已经坚持了好几天。我试图从用户输入中检索日期并使用它在日历上创建一个事件条目。这适用于Applescript:

tell application "Calendar"
  tell calendar "Home"
    set theDate to date loadInDate
    make new event at end with properties {description:job_name, summary:title_,   location:location_, start date:theDate}
 end tell
end tell

其中loadInDate是一个文本字符串,形成为(userMonth&“/”& userDay&“/”& userYear&“& userTime)

但是当我将此代码粘贴到ApplescriptObjC并从用户输入字符串中检索loadInDate时,它会出错“无法从类型字符串中获取日期”。如果我用字符串“09/08/2014 15:00”替换变量“theDate”,它运行正常。

有什么想法吗?

谢谢, Ĵ

script AppDelegate
property parent : class "NSObject"

-- IBOutlets
property window : missing value
property liString : ""



on buttonClicked_(sender)

   set loadInDate to liString as text
    tell application "Calendar"
        tell calendar "Bills"
            make event at end with properties {start date: date loadInDate, summary:loadInDate, location:"Here"}
        end tell
    end tell

end buttonClicked_

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

1 个答案:

答案 0 :(得分:1)

如果loadInDate变量的类是NSString,请将其转换为AppleScript字符串

set theDate to date (loadInDate as text)

-

<强>更新

我测试了,当日期是tell calendar "x"块中的变量时,我发现了“日历”应用程序的错误。

在Xcode中: AppDelegate buttonClicked:]:«class wres»票据的日期和时间日期10/10/2014无效。 (错误-30720)

在ApplesScript编辑器中:错误“日历帐单的日期和时间日期10/10/2014无效。”编号-30720

这有效(我在NSTextField中使用“ 10/10/2014 ”进行了测试):

 on buttonClicked_(sender)
        set loadInDate to (tFld's stringValue()) as text
        tell application "Calendar"
            make new event at end of calendar "Bills" with properties {start date:date loadInDate ,summary:loadInDate, location:"Here"}
        end tell
 end buttonClicked_

在脚本中用 liString 替换(tFld的stringValue())