You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to make my first script that every time I insert a new attachment (image) is renamed with the title of the note and the name of the heading on which it is located
Example
Title: Heart Attack
Heading: # Treatment
New attachment title: Heart attack treatment
Context:
Obsidian Version 1.6.7
MacOS 15.0
Templater Version: 2.7.3
This is the .js I have (not functional)
Templates/Templater/Scripts
`
// El nuevo nombre se basa en el título de la nota y el encabezado donde se inserta la imagen.
module.exports = async (tp) => {
try {
// Verificar si tp está definido
if (!tp) {
console.error("Error: tp no está definido.");
return;
}
// Obtener el contenido del archivo
const content = await tp.file.content();
// Lógica para obtener el título de la nota
const noteTitle = tp.file.title;
// Lógica para obtener el encabezado donde se está insertando la imagen
const cursorPos = tp.app.workspace.activeLeaf.view.editor.getCursor();
const lines = content.split('\n');
let currentHeading = "sin_encabezado";
for (let i = cursorPos.line; i >= 0; i--) {
const line = lines[i];
if (line.match(/^#+\s/)) {
currentHeading = line.replace(/^#+\s*/, '').trim();
break;
}
}
// Nombre del archivo con el título de la nota y el encabezado
const newFilename = `${noteTitle} ${currentHeading}.jpg`;
// Supongamos que la imagen tiene un nombre predeterminado al ser adjuntada
const imagePath = tp.file.folder(true) + "/nombre_imagen_predeterminado.jpg";
// Renombrar la imagen
const newImagePath = tp.file.folder(true) + "/" + newFilename;
await tp.app.vault.rename(tp.app.vault.getAbstractFileByPath(imagePath), newImagePath);
} catch (error) {
console.error("Error ejecutando el script de Templater: ", error);
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm trying to make my first script that every time I insert a new attachment (image) is renamed with the title of the note and the name of the heading on which it is located
Example
Title: Heart Attack
Heading: # Treatment
New attachment title: Heart attack treatment
Context:
Obsidian Version 1.6.7
MacOS 15.0
Templater Version: 2.7.3
This is the .js I have (not functional)
Templates/Templater/Scripts
`
// El nuevo nombre se basa en el título de la nota y el encabezado donde se inserta la imagen.
module.exports = async (tp) => {
try {
// Verificar si tp está definido
if (!tp) {
console.error("Error: tp no está definido.");
return;
}
};`
Templates/Templater/Templates
<%* await tp.user.rename_attachment() %>
The error I get is always "tp is not defined"
Thanks for your time!
Beta Was this translation helpful? Give feedback.
All reactions