使用AppleScript解析日历(ICS)

时间:2011-11-05 23:59:12

标签: parsing applescript icalendar geektool

我很难解决这个问题。我已经在AppleScript中读到了一个ICS文件,我需要解析它。这是我的代码

set calURL to "https://url.to.ics"
set calenderData to do shell script "curl " & calURL

数据读得很好,但我需要解析它。我希望脚本在每个事件的BEGIN:VEVENT和END:VEVENT之间读取数据。我不知道怎么做,但是使用foreach循环还是什么?

以下是包含我的考试的样本日历。

BEGIN:VCALENDAR
VERSION:2.0
X-WR-CALNAME:Eksamener
X-WR-CALDESC:
X-APPLE-CALENDAR-COLOR:#CC2B23FF
BEGIN:VEVENT
UID:C96EF18C-5D17-4170-8C67-832B7F90B464
SUMMARY:TK 1100 Eksamen
SEQUENCE:0
LOCATION:
DTSTART;VALUE=DATE:20111213
DTEND;VALUE=DATE:20111214
END:VEVENT
BEGIN:VEVENT
UID:BDBE0B4B-FF3D-4245-B23F-F1BF25F65998
SUMMARY:DB 1100 Eksamen
SEQUENCE:0
LOCATION:
DTSTART;VALUE=DATE:20111006
DTEND;VALUE=DATE:20111007
END:VEVENT
BEGIN:VEVENT
UID:21261AF0-FDA7-432B-B513-CC3A9C04393D
SUMMARY:PG 1100 Eksamen
SEQUENCE:0
LOCATION:
DTSTART;VALUE=DATE:20111216
DTEND;VALUE=DATE:20111217
END:VEVENT
END:VCALENDAR

这与学校无关,我只是想解析数据,以便在我的桌面(GeekTool)上查看。

1 个答案:

答案 0 :(得分:0)

您需要使用文本项分隔符。试试这个,看看你是否能理解它是如何运作的。

set theData to "BEGIN:VCALENDAR
VERSION:2.0
X-WR-CALNAME:Eksamener
X-WR-CALDESC:
X-APPLE-CALENDAR-COLOR:#CC2B23FF
BEGIN:VEVENT
UID:C96EF18C-5D17-4170-8C67-832B7F90B464
SUMMARY:TK 1100 Eksamen
SEQUENCE:0
LOCATION:
DTSTART;VALUE=DATE:20111213
DTEND;VALUE=DATE:20111214
END:VEVENT
BEGIN:VEVENT
UID:BDBE0B4B-FF3D-4245-B23F-F1BF25F65998
SUMMARY:DB 1100 Eksamen
SEQUENCE:0
LOCATION:
DTSTART;VALUE=DATE:20111006
DTEND;VALUE=DATE:20111007
END:VEVENT
BEGIN:VEVENT
UID:21261AF0-FDA7-432B-B513-CC3A9C04393D
SUMMARY:PG 1100 Eksamen
SEQUENCE:0
LOCATION:
DTSTART;VALUE=DATE:20111216
DTEND;VALUE=DATE:20111217
END:VEVENT
END:VCALENDAR"

set text item delimiters to "BEGIN:VEVENT"
set theEvents to text items of theData
set text item delimiters to "END:VEVENT"

set allEvents to {}
repeat with i from 2 to count of theEvents
    set theEvent to item i of theEvents
    set trimmedEvent to text item 1 of theEvent
    set end of allEvents to text 2 thru -2 of trimmedEvent -- you can do something here instead of collecting them in the list
end repeat
set text item delimiters to ""
return allEvents