Skip to content

Instantly share code, notes, and snippets.

@heroheman
Last active August 29, 2023 06:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heroheman/1fb19d6fd9ea05a848ecf1fa89ffb8fe to your computer and use it in GitHub Desktop.
Save heroheman/1fb19d6fd9ea05a848ecf1fa89ffb8fe to your computer and use it in GitHub Desktop.
Obsidian Templater Script - add task metadata to current line #obsidian #obsidian-md #obsidian-templater #templater #templater-script #tasks #obsidian-task

Add task metadata to current task - Obsidian Templater Script

This script asks the user which type of metadata they want to insert, then prompts them for the appropriate input based on their choice. It handles all the types of metadata mentioned here and appends the corresponding tag to the current line of the document.

Installation

  • You will need the Templater Plugin
  • Add file to your Templates Folder where Templater is looking for your Templates.
  • In your documents, preferable in with the cursor in a task, press CMD+e (or whatever you call to call the templater template modal) and select the script.

Usage

  • It first asks for what you want to add

  • Priority lets you choose a priority

  • everything date related, you have several options

    • add a YYYY-MM-DD format
    • leave empty - defaults to today
    • write: tomorrow, today or in [N] days, where n stands for the amount of days in the future
  • In your documents, preferable in with the cursor in a task, press CMD+e (or whatever you call to call the templater template modal) and select the script.

Options

It defaults to the Emoji Format. You can change the format variable in the beginning to something else to use the DataView Format ... and yes, this should be a boolean. Don't @mention me. 🤷‍♂️

Screenshots

Choose Metadata Type Choose Priority Repeat Frequency Date Input Example Output in Task

#obsidian #obsidian-md #obsidian-templater #templater #templater-script #tasks #obsidian-task

<%*
const format = 'blub'; // default format
// let metadataType = await tp.system.prompt('Enter the type of metadata (Priority, Repeat, Start, Scheduled, Due, Created, Completion)');
const metadataTypes = ['priority', 'repeat', 'start', 'scheduled', 'due', 'created', 'completion'];
const priorityTypes = ['highest', 'high', 'medium', 'low', 'due', 'lowest'];
const metadataType = await tp.system.suggester(metadataTypes, metadataTypes, true, 'Select the type of metadata');
let metadata;
switch (metadataType.toLowerCase()) {
case 'priority':
let priorityValue = await tp.system.suggester(priorityTypes, priorityTypes, true, 'Enter your priority');
switch (priorityValue) {
case 'highest':
metadata = format == 'emoji' ? '🔺' : '[priority:: highest]';
break;
case 'high':
metadata = format == 'emoji' ? '⏫' : '[priority:: high]';
break;
case 'medium':
metadata = format == 'emoji' ? '🔼' : '[priority:: medium]';
break;
case 'low':
metadata = format == 'emoji' ? '🔽' : '[priority:: low]';
break;
case 'lowest':
metadata = format == 'emoji' ? '⏬' : '[priority:: lowest]';
break;
default:
metadata = 'Invalid input';
}
break;
case 'repeat':
let repeatValue = await tp.system.prompt('Enter your repeat frequency');
metadata = format == 'emoji' ? `🔁 ${repeatValue}` : `[repeat:: ${repeatValue}]`;
break;
case 'start':
case 'scheduled':
case 'due':
case 'created':
case 'completion':
// let dateValue = await tp.system.prompt(`Enter your date for ${metadataType}`);
let dateValue = await tp.system.prompt(`Enter your date for ${metadataType}. You can also write 'today', 'tomorrow' or 'in n days'. Leave empty for today`);
if (dateValue === '') {
dateValue = tp.date.now('YYYY-MM-DD'); // use today's date if input is empty
} else if (dateValue.toLowerCase() === 'today') {
dateValue = tp.date.now('YYYY-MM-DD'); // use today's date if input is "today"
} else if (dateValue.toLowerCase() === 'tomorrow') {
dateValue = tp.date.now('YYYY-MM-DD', 1); // use tomorrow's date if input is "tomorrow"
} else if (dateValue.toLowerCase().startsWith('in ') && dateValue.toLowerCase().endsWith(' days')) {
let days = Number(dateValue.split(' ')[1]);
if (!isNaN(days)) {
dateValue = tp.date.now('YYYY-MM-DD', days); // use the date in X days if input is "in X days"
}
}
if (format == 'emoji') {
switch (metadataType.toLowerCase()) {
case 'start':
metadata = `🛫 ${dateValue} `;
break;
case 'scheduled':
metadata = `⏳ ${dateValue} `;
break;
case 'due':
metadata = `📅 ${dateValue} `;
break;
case 'created':
metadata = `➕ ${dateValue} `;
break;
case 'completion':
metadata = `✅ ${dateValue} `;
break;
}
} else {
metadata = `[${metadataType}:: ${dateValue}] `;
}
break;
default:
metadata = ' ⚠️ Invalid input ';
}
tR += metadata + ' ';
-%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment