Well, in the meantime I was fiddling with some Meta Bind buttons which open links: performs searches…
Then I thought, while I’m at it, I am going to upgrade the snippet above:
Ver.2 of the script shared above
<%*
// Get the active workspace
const workspace = app.workspace;
// Get all open leaves (tabs)
const leaves = workspace.getLeavesOfType("markdown");
// Define the list of filenames to exclude
const excludeFilenames = [
"Collections Query",
"fifty latest notes",
"quicksearch"
];
// Extract filenames, filter out excluded filenames
const filenames = leaves
.map(leaf => leaf.view.file.basename)
.filter(filename => !excludeFilenames.includes(filename));
// Format the filenames for search modal
let formatted = `file: /^(${filenames.join("|")})\\.md/`;
// URL-encode only the `file:` part of the query
const encodedFileQuery = encodeURIComponent(formatted);
// Use Templater's system prompt to get the main query from the user
const userQuery = await tp.system.prompt("Enter search term:");
// Append the user's search term to the file search query if provided
if (userQuery && userQuery.trim()) {
formatted += ` ${userQuery.trim()}`;
}
// URL-encode the complete query
const encodedQuery = encodeURIComponent(formatted);
// Construct the Obsidian URI and open the search automatically
const uri = `obsidian://search?query=${encodedQuery}`;
const link = document.createElement('a');
link.href = uri;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// Copy the result and the URL-encoded `file:` query to the clipboard as an afterthought
setTimeout(() => {
const combinedClipboardText = `${formatted}\n\nEncoded file query:\n${encodedFileQuery}`;
navigator.clipboard.writeText(combinedClipboardText).then(() => {
// Show a notice to the user
new Notice(`Copied to clipboard:\n${formatted}\n\nEncoded file query:\n${encodedFileQuery}`);
}).catch(err => {
console.error('Could not copy text: ', err);
});
}, 100);
_%>
When you execute the script, you are asked to enter your search term (utensil
or /cut.*?cucumber/
from my examples above – so normal or regex with /syntax/
searches are both supported) and this will be appended to the file:
query (which part is handled by the script).
Search in the dedicated core search modal pane will be automatically initiated so no need to paste anything in the search bar with this upgrade of the script.
Didn’t test (the DOM element click simulation method, preferred over window.location.href
method) on mobile but it should work there too.
If you are reluctant to touch (edit) the script, you can leave the list of filenames to exclude as it is. It’s not going to make a difference. Script works as it is.
Again, for those who don’t know what’s going on:
Executing the script enables you to limit your search to content in markdown files that are currently open.
Faster than copying and pasting search term after clicking on each tab one by one…
Upgrade 2B
Use case 2 - copy url-encoded versions of files currently open
I kept ‘the add to clipboard’ part and the script even adds the URL-encoded versions of the files currently open. Which is handy if you want to re-use these for Obsidian URI or Advance URI functionality, for example for a Meta Bind button. An example of such button:
```meta-bind-button
style: primary
label: Translations
id: trigger-translations
action:
type: open
link: obsidian://search?&query=file%3A%20%2F%5E(Translations%20%E2%80%93%20same%20translations%20in%20multiple%20places)%5C.md%2F
```
- You add the copied line after the
obsidian://search?&query=
part.