Skip to content

Instantly share code, notes, and snippets.

@kherge
Last active November 9, 2025 02:54
Show Gist options
  • Select an option

  • Save kherge/49dbe4eda4d66ed8949602ad761a911c to your computer and use it in GitHub Desktop.

Select an option

Save kherge/49dbe4eda4d66ed8949602ad761a911c to your computer and use it in GitHub Desktop.
Obsidian: Get and Set Parent

Get and Set Parent

These scripts are useful when you want to create a QuickAdd macro to remember the active note as the parent, create a new note, and then set a note property with a link to the parent. The QuickAdd macro would look like the following:

  1. Get Parent
  2. <Template>
  3. Set Parent

Make sure the Template step opens the new note.

const SETTING = {
ALIAS: "Use Alias",
};
const VARIABLE = "kherge:parent";
async function getParent({ app, variables }, settings) {
delete variables[VARIABLE];
const file = app.workspace.getActiveFile();
if (!file) {
throw new Error("No active file found.");
}
if (settings[SETTING.ALIAS]) {
const cache = app.metadataCache.getFileCache(file);
if (cache?.frontmatter?.aliases) {
variables[VARIABLE] = `[[${file.basename}|${cache.frontmatter.aliases[0]}]]`;
}
}
if (!variables[VARIABLE]) {
variables[VARIABLE] = `[[${file.basename}]]`;
}
}
module.exports = {
entry: getParent,
settings: {
name: "Get Parent",
author: "Kevin Herrera",
options: {
[SETTING.ALIAS]: {
defaultValue: true,
description: "Use the first alias of the parent note, if it exists?",
type: "checkbox",
}
}
}
};
const SETTING = {
PROPERTY: "Parent Property",
};
const VARIABLE = "kherge:parent";
async function setParent({ app, variables }, settings) {
if (!variables[VARIABLE]) {
throw new Error("No parent has been selected.");
}
const file = app.workspace.getActiveFile();
if (!file) {
throw new Error("No active file found.");
}
await app.fileManager.processFrontMatter(
file,
(frontmatter) => {
frontmatter[settings[SETTING.PROPERTY]] = variables[VARIABLE];
}
);
delete variables[VARIABLE];
}
module.exports = {
entry: setParent,
settings: {
name: "Set Parent",
author: "Kevin Herrera",
options: {
[SETTING.PROPERTY]: {
defaultValue: "parent",
description: "What is the name of the parent property in the child note?",
type: "text",
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment