public String getName() {
return "Apple iCal";
}
public boolean exportPrograms(Program[] programs, CalendarExportSettings settings, AbstractPluginProgramFormating formatting) {
AppleScriptRunner runner = new AppleScriptRunner();
StringBuilder script = new StringBuilder();
SimpleDateFormat formatDay = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat formatHour = new SimpleDateFormat("HH:mm");
String calTitle = settings.getExporterProperty(AppleiCalExporter.PROPERTY_CALENDAR_NAME);
if (StringUtils.isBlank(calTitle)) {
calTitle = "TV-Browser";
}
script.append("property myTVCalendar : \"" + calTitle + "\"\n");
script.append("on stringToList from theString for myDelimiters\n" +
"\ttell AppleScript\n" +
"\t\tset theSavedDelimiters to AppleScript's text item delimiters\n" +
"\t\tset text item delimiters to myDelimiters\n" +
"\t\t\n" +
"\t\tset outList to text items of theString\n" +
"\t\tset text item delimiters to theSavedDelimiters\n" +
"\t\t\n" +
"\t\treturn outList\n" +
"\tend tell\n" +
"end stringToList\n" +
"\n" +
"\n" +
"on getDateForISOdate(theISODate, theISOTime)\n" +
"\tlocal myDate\n" +
"\t-- converts an ISO format (YYYY-MM-DD) and time to a date object\n" +
"\tset monthConstants to {January, February, March, April, May, June, July, August, September, October, November, December}\n" +
"\t\n" +
"\tset theISODate to (stringToList from (theISODate) for \"-\")\n" +
"\tset theISOTime to (stringToList from (theISOTime) for \":\")\n" +
"\t\n" +
"\tset myDate to current date\n" +
"\tset month of myDate to 1\n" +
"\t\n" +
"\ttell theISODate\n" +
"\t\tset year of myDate to item 1\n" +
"\t\tset day of myDate to item 3\n" +
"\t\tset month of myDate to item (item 2) of monthConstants\n" +
"\tend tell\n" +
"\ttell theISOTime\n" +
"\t\tset hours of myDate to item 1\n" +
"\t\tset minutes of myDate to item 2\n" +
"\t\tset seconds of myDate to 0\n" +
"\tend tell\n" +
"\t\n" +
"\treturn myDate\n" +
"end getDateForISOdate\n" +
"\n");
script.append("\n");
script.append("tell application \"iCal\"\n");
script.append(" if (exists (calendars whose title is myTVCalendar)) then\n");
script.append(" set TVBrowserCalendar to first item of (calendars whose title is myTVCalendar)\n");
script.append(" else\n");
script.append(" set TVBrowserCalendar to make new calendar with properties {title:myTVCalendar}\n");
script.append(" end if\n");
script.append("\n");
for (Program program : programs) {
final Calendar start = CalendarToolbox.getStartAsCalendar(program);
final Calendar end = CalendarToolbox.getEndAsCalendar(program);
script.append(" set startDate to my getDateForISOdate(\"").append(formatDay.format(start.getTime())).append("\", \"").append(formatHour.format(start.getTime())).append("\")\n");
script.append(" set endDate to my getDateForISOdate(\"").append(formatDay.format(end.getTime())).append("\", \"").append(formatHour.format(end.getTime())).append("\")\n");
script.append("\n");
script.append(" set props to {start date:startDate, end date:endDate, summary:\"");
ParamParser parser = new ParamParser();
String title = parser.analyse(formatting.getTitleValue(), program);
script.append(title);
script.append("\", description:\"");
String desc = parser.analyse(formatting.getContentValue(), program);
script.append(desc.replaceAll("\"", "\\\\\"").replace('\n', ' '));
script.append("\"}\n");
script.append(" set theEvent to make new event at end of (events of TVBrowserCalendar) with properties props\n");
if (settings.getUseAlarm()) {
script.append(" make new display alarm at beginning of theEvent with properties {trigger interval:-");
script.append(settings.getAlarmMinutes());
script.append("}\n");
}
}
script.append("end tell\n");
try {
runner.executeScript(script.toString());
} catch (IOException e) {
e.printStackTrace();
ErrorHandler.handle("Error during execution of the applescript", e);
}