Is there no way to apply a template on a file in the flie explorer?

What I’m trying to do

I have a template, and idealy i woul prefer if i didn’t need to open file first, and could
apply it on a file using a right click menu.

 <%* tR = "" -%><%*
const allowedVaults = ["Storage", "Flower Tree", "Abitest", "Archivist", "Attributes", "Akashi"];
const LEVELS_DOWN = tp.frontmatter.LEVELS_DOWN ?? 1; // Change this to move N levels down
const TERMINATE_IF_NOT_ENOUGH = false; // true = stop if not enough levels, false = go as far as possible

const fullPath = tp.file.path(); // full path including all folders
const parts = fullPath.split("/");

// Skip the first folder (current vault)
const foldersAfterCurrent = parts.slice(1);

// Find all valid vaults after current vault
const validVaults = foldersAfterCurrent.filter(f => allowedVaults.includes(f));

let targetVault, filePath;

if (validVaults.length < LEVELS_DOWN) {
    if (TERMINATE_IF_NOT_ENOUGH) {
        new Notice(`Not enough valid vaults to move ${LEVELS_DOWN} levels down.`);
        return;
    } else if (validVaults.length === 0) {
        new Notice("No valid vaults found in path. Cannot move down.");
        return;
    } else {
        targetVault = validVaults[validVaults.length - 1];
    }
} else {
    targetVault = validVaults[LEVELS_DOWN - 1];
}

// Everything after the target vault in the path
const targetIndex = foldersAfterCurrent.indexOf(targetVault);
filePath = foldersAfterCurrent.slice(targetIndex + 1).join("/");

// Open note in target vault
const uri = `obsidian://open?vault=${encodeURIComponent(targetVault)}&file=${encodeURIComponent(filePath)}`;
window.open(uri);
%>


Template, even if you probably don’t need it.

Things I have tried

I have looked into (With varius detai):
Quick add, note toolbar, Advanced URI, Commander, URI commands, Custom commands
and searches for “Menu” “Command” “URI” “Template” in the plugin marketplace
As well as ask AI with more or less, zero results.

I am starting to feel there is no, sensible way of doing it, but maybe i am wrong?

There is file menu event and {{event_file_path}} variable when you use Shell commands plugin.


https://publish.obsidian.md/shellcommands/Events/File+menu

That shell command (in red) can access file specific information using the {{event_file_path}} variable.

The last part of executing Templater using command line is still missing though. You could instruct Templater using Advanced URI but this requires simulating the file opening and then closing it. There is openmode=silent but I’m not sure does it work (https://publish.obsidian.md/advanced-uri-doc/Concepts/Navigation+Parameters).

Here is the “replace templates in the active file” command uri:

obsidian://adv-uri?commandid=templater-obsidian%3Areplace-in-file-templater

Then use filepath={{event_file_path}} i.e.

obsidian://adv-uri?filepath={{event_file_path}}&commandid=templater-obsidian%3Areplace-in-file-templater

and openmode=silent:

obsidian://adv-uri?filepath={{event_file_path}}&commandid=templater-obsidian%3Areplace-in-file-templater&openmode=silent

(I haven’t tested above)

 

The exact shell command is system specific:


https://publish.obsidian.md/advanced-uri-doc/Getting+started

1 Like

Or i can rewrite my template as a shell script and be done with it.
the important part was to how get the file path from the menu

powershell -NoProfile -Command "$allowedVaultsStr='{{_allowedVault}}'; $allowedVaults=$allowedVaultsStr -split ',' | ForEach-Object { $_.Trim() }; $LEVELS_DOWN=[int]'{{_Levels_down}}'; $TERMINATE_IF_NOT_ENOUGH=[bool]::Parse('{{_blink}}'); $limit='{{_limit}}'; $fullPath='{{event_file_path:absolute}}'; $relativePath='{{event_file_path:relative}}'; if([string]::IsNullOrWhiteSpace($fullPath)){Write-Host 'No file path provided'; exit 1}; $fullPath=$fullPath -replace '\\\\','/'; $relativePath=$relativePath -replace '\\\\','/'; $limitIndex=$fullPath.IndexOf($limit); if($limitIndex -eq -1){Write-Host 'Limit folder not found in path'; exit 1}; $pathAfterLimit=$fullPath.Substring($limitIndex); $relativeIndex=$pathAfterLimit.IndexOf($relativePath); if($relativeIndex -eq -1){Write-Host 'Relative path not found in path after limit'; exit 1}; $pathBeforeRelative=$pathAfterLimit.Substring(0, $relativeIndex - 1); $relativePathParts=$relativePath -split '[/\\\\]'; $relativePathParts=$relativePathParts | Where-Object { $_ -ne '' }; $validVaults=@(); foreach($folder in $relativePathParts){if($allowedVaults -contains $folder){$validVaults+=$folder}}; if($validVaults.Count -eq 0){Write-Host 'No valid vaults found in path. Cannot move down.'; exit 1}; if($validVaults.Count -lt $LEVELS_DOWN){if($TERMINATE_IF_NOT_ENOUGH){Write-Host \"Not enough valid vaults to move $LEVELS_DOWN levels down.\"; exit 1} else {$targetVault=$validVaults[-1]}} else {$targetVault=$validVaults[$LEVELS_DOWN-1]}; $targetIndex=[Array]::IndexOf($relativePathParts,$targetVault); $rest=if($targetIndex+1 -le ($relativePathParts.Count-1)){$relativePathParts[($targetIndex+1)..($relativePathParts.Count-1)]} else {@()}; $filePath=$rest -join '/'; $uri='obsidian://open?vault='+[System.Uri]::EscapeDataString($targetVault)+'&file='+[System.Uri]::EscapeDataString($filePath); Start $uri"
powershell -NoProfile -Command "$allowedVaultsStr='{{_allowedVault}}'; $allowedVaults=$allowedVaultsStr -split ',' | ForEach-Object { $_.Trim() }; $LEVELS_UP=[int]'{{_Levels_up}}'; $TERMINATE_IF_NOT_ENOUGH=[bool]::Parse('{{_blink}}'); $limit='{{_limit}}'; $fullPath='{{event_file_path:absolute}}'; $relativePath='{{event_file_path:relative}}'; if([string]::IsNullOrWhiteSpace($fullPath)){Write-Host 'No file path provided'; exit 1}; $fullPath=$fullPath -replace '\\\\','/'; $relativePath=$relativePath -replace '\\\\','/'; $limitIndex=$fullPath.IndexOf($limit); if($limitIndex -eq -1){Write-Host 'Limit folder not found in path'; exit 1}; $pathAfterLimit=$fullPath.Substring($limitIndex); $relativeIndex=$pathAfterLimit.IndexOf($relativePath); if($relativeIndex -eq -1){Write-Host 'Relative path not found in path after limit'; exit 1}; $pathBeforeRelative=$pathAfterLimit.Substring(0, $relativeIndex - 1); $pathAfterLimitParts=$pathAfterLimit -split '[/\\\\]'; $pathAfterLimitParts=$pathAfterLimitParts | Where-Object { $_ -ne '' }; $relativePathParts=$relativePath -split '[/\\\\]'; $relativePathParts=$relativePathParts | Where-Object { $_ -ne '' }; $currentVaultIndex=[Array]::IndexOf($pathAfterLimitParts, $relativePathParts[0]) - 1; if($currentVaultIndex -lt 0){Write-Host 'Cannot determine current vault position'; exit 1}; $validVaultsBeforeCurrent=@(); for($i=$currentVaultIndex-1; $i -ge 0; $i--){if($allowedVaults -contains $pathAfterLimitParts[$i]){$validVaultsBeforeCurrent+=$pathAfterLimitParts[$i]}}; if($validVaultsBeforeCurrent.Count -eq 0){Write-Host 'No valid vaults found to move up to.'; exit 1}; if($validVaultsBeforeCurrent.Count -lt $LEVELS_UP){if($TERMINATE_IF_NOT_ENOUGH){Write-Host \"Not enough valid vaults to move $LEVELS_UP levels up.\"; exit 1} else {$targetVault=$validVaultsBeforeCurrent[-1]}} else {$targetVault=$validVaultsBeforeCurrent[$LEVELS_UP-1]}; $targetIndex=[Array]::IndexOf($pathAfterLimitParts,$targetVault); $rest=if($targetIndex+1 -le ($pathAfterLimitParts.Count-1)){$pathAfterLimitParts[($targetIndex+1)..($pathAfterLimitParts.Count-1)]} else {@()}; $filePath=$rest -join '/'; $uri='obsidian://open?vault='+[System.Uri]::EscapeDataString($targetVault)+'&file='+[System.Uri]::EscapeDataString($filePath); Start $uri"

Well, it works now!
thank you a lot.

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