Run script inside a note to transform content

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):

Name1 Lastname1 <[email protected]>; Name2 Lastname2 <[email protected]>; Name3 Lastname3 <[email protected]>; Name4 Lastname4 <[email protected]>; Name5 Lastname5 <[email protected]>; Name6 Lastname6 <[email protected]>; Name7 Lastname7 <[email protected]>; Name8 Lastname8 <[email protected]>; Name9 Lastname9 <[email protected]>;

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:

People:: [[Name1 Lastname1]] - [[Name2 Lastname2]] - [[Name3 Lastname3]] - [[Name4 Lastname4]] - [[Name5 Lastname5]] - [[Name6 Lastname6]] - [[Name7 Lastname7]] - [[Name8 Lastname8]] - [[Name9 Lastname9]]

Things I have tried

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 :+1:

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:

People:: <% tp.user.formatPeopleList(tp.file.selection()) %> 

(below is the paste list of attendees)
Bob Marley <[email protected]>; Jane X Doe <jane[email protected]>; John A Smith <[email protected]>

The I simply select the list of attendees and run Templater to replace the script in the note. The result is this:

People:: [[Bob Marley]] - [[Jane X Doe]] - [[John A Smith]] 

(below is the paste list of attendees)
Bob Marley <[email protected]>; Jane X Doe <jane[email protected]>; John A Smith <[email protected]>

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:

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.

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]]

- Obsidian Templater plugin

- 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()) %>`

- 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}]]`;

  // 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:

<% tp.user.formatPeopleList(tp.file.selection()) %>

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.

Glad you got it figured out

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.