Action

Migrate Journal Items tb12

Posted by FlohGro, Last update about 1 year ago - Unlisted

UPDATES

about 1 year ago

Fix busycal link
Remove lines when migrated

Process each line of the draft. By the default settings file:

  • Lines starting with “-“ are collected and sent to Day One as a journal entry.
  • Lines starting with “@“ are sent to Busycal.
  • Lines startting with “&” are sent to Obsidian Daily Note via the Obsidian Advanced URI

Everything else is ignored.

Based on the works of Kirk Strauser and inspired by his craftsmanship.

adapted by @FlohGro

Steps

  • script

    draft.content = draft.content.replace(/- \[\ \]/gi, "*");
    draft.update();
    script.complete();
  • script (disabled)

    // move "@done" to bottom of draft
    
    var d = draft.content;
    var lines = d.split("\n");
    var begin = '';
    var end = '';
    
    for (var line of lines) {
      var check = line.includes("- [x]");
      //if line contains "- [x]"… 
      if (check) {
      //add it to var end
        end += "- " + line + "\n";
      }
      else {
        begin += line + "\n";
      }
    }
    begin = begin.slice(0,-1);
    end = end.slice(0,-1);
    editor.setText(begin + "\n" + end);
    d.update;
    editor.focus(d);
    script.complete();
  • script (disabled)

    var settingsTitle = "# Quick Journaling settings"
    var tag = "settings"
    
    var settingsDrafts = Draft.query(settingsTitle, "all", [tag]);
    
    if (settingsDrafts.length > 1) {
    	app.displayErrorMessage("Found more than one settings file named '" + settingsTitle + "'");
      exit();
    }
    
    if (settingsDrafts.length == 0) {
    	var ourSettings = Draft.create();
    	ourSettings.content = settingsTitle + "\n" + `
    // These lines determine which action is taken for each line in your Today's Journal.
    
    @ Fantastical
    * Things
    - Day One
    
    // Line format: "<trigger character><space>Name Of App".
    
    // Lines that start with "//" or "#", or which are blank, are ignored. Feel free to add new mappings (e.g. "& GoodTask" if that's your thing) or delete ones you never want to use.
    
    // Supported apps:
    //
    //   Things
    //   Day One
    //   Fantastical
    //   GoodTask
    //   OmniFocus
    `;
    	ourSettings.addTag(tag);
    	ourSettings.update();
    	app.displayInfoMessage("Created a new settings file: " + settingsTitle);
    }
    else
    {
    	var ourSettings = settingsDrafts[0];
    }
    
    var lineProcessors = {}
    
    for (var line of ourSettings.content.split("\n")) {
    	line = line.trim();
    	if (
    		line.length == 0 ||
    		line[0] == "#" || 
    		line.substr(0, 2) == "//"
    	) { continue; }
    	
    	first = line[0];
    	rest = line.substr(1).trim();
    	if (rest.length == 0) { continue; }
    	
    	lineProcessors[first] = rest;
    }
    script.complete();
  • script

    const vaultName = "FlohGro"
    const sectionName = "Test Heading 1"
    
    // NO CHANGES BELOW
    const draftLines = draft.content.split("\n");
    const tags = draft.tags;
    const dayoneURL = "dayone2://post"
    //const fantasticalURL = "fantastical2://x-callback-url/parse";
    //const goodtaskURL = "goodtask3://x-callback-url/add";
    //const omnifocusURL = "omnifocus://x-callback-url/add";
    //const thingsURL = "things:///add";
    let dayOneLines = [];
    let obsidianLines = [];
    let linesToDelete = [];
    let lineProcessors = {"-":"Day One",
    "&": "Obsidian",
    "@": "BusyCal"
    }
    
    let textReplacements = {
    "#t": "#today",
    "#q": "#quick",
    "#u": "#urgent"
    }
    // Actions for sending journal entries to various apps
    
    function collectForDayOne(entry) {
    	dayOneLines.push(entry);
    }
    
    function collectForObsidianDaily(entry) {
    	obsidianLines.push(entry);
    }
    /*
    function sendToFantastical(entry) {
    	var cb = CallbackURL.create();
    	cb.baseURL = fantasticalURL;
    	cb.addParameter("sentence", entry);
    	cb.open();
    }
    
    function sendToGoodTask(entry) {
    	var cb = CallbackURL.create();
    	cb.baseURL = goodtaskURL;
    	cb.addParameter("title", entry);
    	cb.open();
    }
    
    function sendToOmniFocus(entry) {
    	var cb = CallbackURL.create();
    	cb.baseURL = omnifocusURL;
    	cb.addParameter("name", entry);
    	cb.open();
    }
    
    function sendToThings(entry) {
    	var cb = CallbackURL.create();
    	cb.baseURL = thingsURL;
    	cb.addParameter("title", entry);
    	cb.addParameter("tags", tags);
    	cb.open();
    }
    
    function sendToTodoist(entry) {
    		let success = todoist.quickAdd(line);
    		if (success) {
    			console.log("Todoist task created: " + line);
    		}
    		else {
    			ctErrors++;
    			console.log("Todoist error: " + todoist.lastError);
    		}
    }
    */
    
    function sendToBusyCal(entry){
    	app.openURL("busycalevent://new/" + entry)
    }
    
    // Resolve actions defined in the settings file to the actual
    // functions to call.
    
    var actionMap = {
    	"Day One": collectForDayOne,
    	"Obsidian": collectForObsidianDaily,
    //	"Fantastical": sendToFantastical,
    //	"GoodTask": sendToGoodTask,
    //	"OmniFocus": sendToOmniFocus,
    //	"Things": sendToThings,
    //	"Todoist": sendToTodoist,
    	"BusyCal": sendToBusyCal
    }
    
    var actions = {}
    for (var key in lineProcessors) {
    	var actionName = lineProcessors[key];
    	actions[key] = actionMap[actionName];
    }
    
    // Process all the draftLines.
    
    for (var line of draftLines) {
    	line = line.trim();
    	if (line.length == 0) { continue; }
    
    	first = line[0];
    	rest = line.substr(1).trim();
    	if (rest.length == 0) { continue; }
    
    	action = actions[first];
    	if (action === undefined) { continue; }
    
    	action(rest);
    	linesToDelete.push(line);
    }
    
    // Some actions are processed in a batch after all their entries
    // are collected. This is where that happens.
    
    if (dayOneLines.length > 0) {
    	var body = dayOneLines.join("\n\n");
    	var cb = CallbackURL.create();
    	cb.baseURL = dayoneURL;
    	cb.addParameter("entry", body);
    	cb.addParameter("tags", tags);
    	cb.waitForResponse = false;
    	cb.open();
    }
    
    if (obsidianLines.length > 0){
    	let linesToAdd = obsidianLines.map((line) => {return "- [ ] " + line})
    	let textToAdd = linesToAdd.join("\n")
    
    for (let key in textReplacements) {
      textToAdd = textToAdd.replaceAll(key, textReplacements[key]);
    }
    
    	let cb = CallbackURL.create();
    	
    	cb.baseURL = "obsidian://advanced-uri?" + vaultName + "&daily=true";
    		
    	if(sectionName != ""){
    	cb.addParameter("heading", sectionName)
    	}
    	alert(cb.url)
    	cb.addParameter("mode", "append");
    	cb.addParameter("data", textToAdd)
    	cb.waitForResponse = false
    	cb.open();
    
    
    }
    
    //remove all lines from the that where processed 
    
    let newContent = draftLines.filter((line) => !linesToDelete.includes(line))
    
    draft.content = newContent.join("\n")
    draft.update()

Options

  • After Success Archive , Tags: migrated
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.