I have some Templater scripts which use tp.system.suggester() and I’m wondering if there is a way I can change this to open the Omnisearch window and grab whatever file you pick out from there instead. This would be extremely useful for me and would massively improve my workflow were I able to do this.
I’m assuming this is possible, I feel like I just need a little bit of direction.
I’ve seen people make use of app.plugins.plugins to call functions from other plugins. This might be one path you could try. I have no personal experience with trying to use it in Templater scripts.
Thanks, that’s the route I was probably going to go down next; have a look at how to call functions from other plugins. I’m hoping there’s something in the Omnisearch code I can use.
I feel like I just need an await to open the modal and then a callback to actually use the file that’s been selected.
The suggester is for filenames so it would do the same as the QuickSwitcher: take filenames from the cache.
Omnisearch’s strength is indexed file content.
So I don’t see why you would want to hook into Omnisearch? Would you want to do what? Create files based on search string results? There is not much to gain when you copy the string to the clipboard and create the new file.
I have a template which allows you to select a note and then insert the link to the active note into that note. It will then go into my inbox note and remove the link to this note.
For context, I have a system revolving around links parenting notes into other notes, rather than using folders, as I find links are a lot more flexible in being able to parent things in multiple places as well as the improved ability to organise the files in what’s a sort of open canvas.
This is that code (yay, free code!):
<%*
let last_open_files_paths = await app.workspace.getLastOpenFiles();
function sort_func(a, b) {
let a_last_open = last_open_files_paths.contains(a.path);
let b_last_open = last_open_files_paths.contains(b.path);
if (a_last_open && !b_last_open) {
return -1;
}
if (!a_last_open && b_last_open) {
return 1;
}
if ((a_last_open && b_last_open)) {
let a_index = last_open_files_paths.indexOf(a.path);
let b_index = last_open_files_paths.indexOf(b.path);
if (a_index < b_index) {
return -1;
}
if (a_index > b_index) {
return 1;
}
return 0;
}
if (a.basename < b.basename) {
return -1;
}
if (a.basename > b.basename) {
return 1;
}
return 0;
}
let all_notes = app.vault.getAllLoadedFiles()
.filter(x => x.name.endsWith(".md") && x.path.startsWith("90_Notes"))
.sort(sort_func);
all_notes.unshift("/");
let note_to_add_link_to = await tp.system.suggester(function(f) {
if (typeof f === "string") {
return f;
}
return f.path.slice(0, -3);
}, all_notes, false, "", 10);
if (note_to_add_link_to != null) {
let tfile = app.workspace.getActiveFile();
if (tfile.basename == "202412140120 Top Inbox Note") {
// get first inbox note link instead
const inbox = app.vault.getAbstractFileByPath("90_Notes/202411142100 Active Inbox-MOC.md");
const contents = await this.app.vault.read(inbox);
tfile = tp.user.get_first_outlink_page(contents);
}
if (note_to_add_link_to != "/") {
const my_title = tfile.basename;
// add note link to note_to_add_link_to
const note_link = "- [[" + my_title + "]].";
const notes_tfolder = app.vault.getAbstractFileByPath("90_Notes");
app.workspace.activeLeaf.openFile(note_to_add_link_to).then(function() {
app.vault.append(note_to_add_link_to, "\n" + note_link).then(function() {
new Notice('Successfully moved ' + my_title + ' to ' + note_to_add_link_to.name, 3600);
})
});
}
if (note_to_add_link_to == "/" || note_to_add_link_to.basename != "202411142100 Active Inbox-MOC") {
tp.user.remove_from_inbox(tfile);
}
}
%>
The issue is, I have 88k notes in my vault now, and using tp.system.suggester() is so slow while typing to filter these search results. Omnisearch is so much faster. I was hoping I could just slot it in here somehow and get that performance boost.
If you can help me understand a way to improve the performance of this without using Omnisearch that could also work (and I would be incredibly grateful!); that’s really what I’m after.
I’ll have a play with the suggester() method and see if I can get it faster.
It may be faster retrieval if I insist it just deals with strings rather than files and then just grab the file afterwards instead.
I also swapped getLoadedFiles() for getMarkdownFiles() as per your suggestion. I imagine getMarkdownFiles() is faster as I’m not having to pre-filter for markdown files, though that was never the issue before and through testing already it hasn’t really noticeably made a difference. I still appreciate it though, it’s probably better for the purpose I need it for.
Yeahh, somehow through experimentation I actually managed to make the template slower.
I don’t think messing with suggester() works because the way it handles typing is that it iterates through all 88k files every time you type a character to check whether there’s a name match.
I think the way Omnisearch works is probably pre-caching search results, so having all “a” results cached, “ab” results cached, etc. There is no good way to do that with the suggester() method.
This is the slower method I used if you’re interested:
let all_notes = app.vault.getMarkdownFiles()
.filter(x => x.path.startsWith("90_Notes"))
.sort(sort_func);
let suggestions = ["/"];
let result_dict = {};
for (let i = 0; i < all_notes.length; i++) {
let key = all_notes[i].path.slice(0, -3);
suggestions.push(key);
result_dict[key] = all_notes[i];
}
let chosen_selection = await tp.system.suggester(suggestions, suggestions, false, "", 10);
I am not at a PC now to be able to try but you can you try and add the full alias retrieval implementation of gino_m and AlanG from that thread because I remember trying that and it was fast for me with…13k or so files.
You can also get help from Claude.ai (free with a mobile number signup) to comb the necessary parts together, if needed.
It’s OK, figured out a workaround by just reducing the amount of suggester results to only those with certain name suffixes (“-MOC”, “-Complete”, “-Archived”, etc.).
I did a bunch of research on the Omnisearch GitHub source code, so I could probably give some limited direction if anyone else is trying to implement this.