First, search the help docs and this forum. Maybe your question has been answered! The debugging steps can help, too. Still stuck? Delete this line and proceed.
What I’m trying to do
When I take notes of work meetings, I want to keep document who participated. From Outlook I can get a list like this (see below):
I would like to past it as it is in the note, maybe I need to add a special begin and end to identify the input data (the list of people paste from Outlook), and run a script inside Obsidian that will convert the strings above into this:
I have read how to call JavaScripts using Templater, but I do not know how to consume data from the active note, process it, and write the results in the active note. I am not a developer, I can follow up code and make changes here and there, but I really need help with this one.
Many thanks! The tool and this community are awesome
I would recommend using templater’s tp.file.selection() function. After you paste the list, just select it and run whatever templater script that will consume the data and spit out the formatted list.
Thank you very much @mr_abomination ! You gave me the right tip on how to use content in the note as the input to the script.
This is the final solution I implemented. Imagine that I have this meeting note, and I copy-pasted the emails from Outlook. Note the Templater code in the note:
Unfortunately, I have still not figured out a way to delete the selected text. If anybody knows how, please let me know.
Here is the Javascript:
/*
MOTIVATION:
In Obsidian, I associate people I work with to notes, specially meeting notes. It is easy for me to get the list of attendees from Outlook (copy&paste) into the note, in the field Dataview `People::`, but the format is not OK, my vault requires Obsidian links with the name of person as the name of the note.
FUNCTIONALITY:
The script looks for the field `People::` and replaces a list of people's names and emails separated with a semicolon into a dash-separate list of Obsidian links with the persons names.
- Input --> People:: Bob Marley <[email protected]>; James 007 Bond <[email protected]>;
- Output --> People:: [[Bob Marley]] - [[James 007 Bond]]
REQUIRES:
- Obsidian Templater plugin
PREPARATION:
- In my vault there is the folder `Aux - Scripts` for my scripts (not just for Templater)
- Save this script in `your-obsidian-vault/Aux - Scripts`
- In the Templater plugin settings, choose `Aux - Scripts` as the folder for user scripts
- Optionally, you can create a note template for meetings with the line `People:: <% tp.user.test2(tp.file.selection()) %>`
USAGE:
- Open or create a note containing the line `People:: <% tp.user.test2(tp.file.selection()) %>`
- Paste somewhere in the body of the note, the list of attendees gathered from Outlook
- Select the full list of names and emails gathered from Outlook; keep that text selected
- Bring up the Command pane (CMD+P) and select `Templater: Replace templates in the active file`
*/
function formatPeopleList(input) {
// Split the input string into individual person strings
const people = input.split(';');
// Initialize an empty array to store the formatted names and last names
const attendeesLinks = [];
// Iterate over each person string
for (let i = 0; i < people.length; i++) {
// Next, the regex pattern ([^<]+)<([^>]+)> is designed to match and capture two parts of the input string: the name part before < and the email part between < and >. The captured matches are stored in the matches array.
const matches = people[i].match(/([^<]+)<([^>]+)>/);
// If the pattern matches, the match() method returns an array where the first element (index 0) is the entire matched string, and the subsequent elements correspond to the captured groups: name (index 1) and email (index 2)
if (matches && matches.length === 3) {
const namesAndLastNames = matches[1].trim();
const personLink = `[[${namesAndLastNames}]]`;
attendeesLinks.push(personLink);
}
}
// Join the personLinks with dashes and return the result
const peopleObsidianFriendly = attendeesLinks.join(' - ');
return peopleObsidianFriendly;
}
module.exports = formatPeopleList;
Hello again, I would like to mention that I have optimize my workflow and resolved the need to later delete the pasted list of attendees (gathered from Outlook).
I create a template called meeting_attendees containing just this:
So then in the note where I track meeting stuff, in the People:: field, I paste the list of attendees (gathered from Outlook). I select them all and then have a hotkey that triggers the action “Templater: Open Insert Template modal”. I then type “meeting”, select the meeting_attendees template, hit enter, and voilà, the list of names is nicely formatted as Obsidian links.