Drafts Action Directory

Import Calendar

Posted by agiletortoise, Last update about 1 month ago

Prompt to select a calendar and date range, then import all events in that calendar and date range as a Markdown list in a new draft.

Install

Steps

  • script

    let calendars = Calendar.getAllCalendars();
    let calendarNames = calendars.map(x => x.title);
    
    let p = Prompt.create();
    p.title = "Import Calendar";
    p.message = "Select calendar and date range to import events from the calendar as a Mardown lst";
    
    p.addSelect("calendar", "Calendar", calendarNames, [], false);
    
    p.addDatePicker("start","Start date", (1).years().ago(), {"mode":"date"});
    p.addDatePicker("end","End date", Date.today(), {"mode":"date"});
    p.addButton("Import");
    
    if (p.show()) {
    	let selected = p.fieldValues["calendar"];
    	let ix = calendarNames.findIndex(x => x == selected);
    	let cal = calendars[ix];
    	let start = p.fieldValues["start"];
    	let end = p.fieldValues["end"];
    	if (cal) {
    		if (!importCal(cal, start, end)) {
    			context.fail();
    		}
    	}
    }
    else {
    	context.cancel();
    }
    
    function importCal(cal, start, end) {
    	let events = cal.events(start, end);
    	if (!events || events.length == 0) {
    		return false;
    	}
    	let t = []
    	for(let event of events) {
    		t.push("- *" + event.title + "*\n");
    		if (event.isAllDay) {
    			t.push("    - All Day: " + event.startDate + " - " + event.endDate + "\n");
    		}
    		else {
    			t.push("    - Start: " + event.startDate + ", End: " + event.endDate + "\n");
    		}
    		if (event.notes.length > 0) {
    			t.push("    - " + event.notes + "\n");
    		}
    		t.push("\n");
    	}
    
    	let d = Draft.create();
    	d.content = t.join("");
    	d.update();
    	editor.load(d);
    	return true;
    }

Options

  • After Success Default
    Notification Info
    Log Level Info

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.