0

我是 AppleScript 的新手,我似乎找不到任何好的资源来帮助我解决我的问题。给定一个包含日期和描述的文件:

  September 5, 2013
  Event 1.

  September 8, 2013
  Event 2.

  etc.

我想解析日期和事件信息的文件,然后在 Mac 日历应用程序中创建这些天的事件(这些描述和事件标题)。但是,我坚持使用以下代码:

tell application "Finder"
    set Names to paragraphs of (read (choose file with prompt "Pick text file containing track names"))
    repeat with nextLine in Names
        set x to "Thursday, " & nextLine & " 12:00:00 AM"
        if nextLine starts with "Sept" then
            tell application "Calendar"
                tell calendar "My Calendar"
                    make new event with properties {description:"Event Description", summary:"Event Name", location:"Event Location", start date:date x, allday event:true}
                end tell
            end tell
        end if
    end repeat
end tell

该代码还不起作用,因为它抱怨日期格式不正确,最重要的是,我不知道如何让第二行与日期行一起读取。任何帮助将不胜感激,谢谢。

4

1 回答 1

2

我通过将外部文件格式更改为:

5 September 2013

这可能是本地系统日期格式设置,因为这就是我在语言和文本 - 系统偏好设置中的定义。也许首先探索您的设置。

我在下面稍微修改了您的脚本,以向您展示如何通过将循环更改为基于索引的循环来获取事件名称。

此外,我已经删除了告诉“Finder”块,因为它不需要,在这种情况下,您没有使用 Finder 中的任何命令。

我还将您的一些变量重命名为更清晰(主观),脚本中注释了其他调整。

set cal_data to the paragraphs of (read (choose file with prompt "Pick text file containing track names"))
set c to the count of cal_data

repeat with i from 1 to c
    set currentLine to item i of cal_data
    if currentLine contains "September" then
        set dt to date (currentLine) -- Dropped the time as an all day event does not need this and the date parse will auto set it to 12:00am
        set ev_name to item (i + 1) of cal_data -- The next item in cal_data  should be the event name      
        tell application "Calendar" 
            tell calendar "My Calendar"
                make new event with properties {description:"Event Description", summary:ev_name, location:"Event Location", start date:dt, allday event:true}
            end tell
        end tell
    end if

end repeat

希望这能让您进入下一步。

于 2013-09-06T07:57:27.247 回答