Automate finding a bookmark string enclosed in a comment in the current Obsidian note.
Things I have tried
Advance warning: I nerd out in this request—please read the entire post before offering a solution.
I’ve figured out how to do most of what I want using Obsidian plugins:
The comment string is %%§: bookmark%%
I invoke the in-note search function with the Advanced URI plugin, using this URI: obsidian://adv-uri?search=%%§:%20bookmark%% (Note: the space in the bookmark string needs to be encoded as %20 in an Obsidian link but enclosed in quotes when opened from the zsh shell.)
I open the URI from within Obsidian either by entering as a link in a note or using the Shell Commands plugin to open it using the MacOS shell command: open -a obsidian 'obsidian://adv-uri?search=%%§: bookmark%%'
I call the shell command from a button on a toolbar using the Editing Toolbar plugin.
open -an obsidian 'obsidian://adv-uri?search=%%§: bookmark%%'
It’s a bit convoluted, but it works, and it’s the only way I’ve found to automate an in-document search in Obsidian. I use regex search and replace frequently, but those don’t go to the find string in the document. Even with this process, I still need to click in the document to move the cursor to that point.
The last bit I haven’t yet figured out is how to close the Obsidian search pane automatically. It’s not a huge hassle, but my tidy soul wants to tie this up with a bow.
tl;dr
Is there a way to close the Obsidian in-document search pane with a scriptable command: URI, slash command, or other mechanism?
Is there a way to move the cursor to the location of a search string?
If there is not a command to close the search pane, can a search be invoked without opening it, preferably using a native URI or the Advanced URI plugin?
I don’t think there is a way to do what you are asking with 3rd party plugins or core plugins, although I do remember using a plugin of my own to set certain sidebar tabs active (if you know the existing leaf ID from the workspace.json, you can do it but don’t go down this road), but this is not interesting anymore as Obsidian handles deferred tabs well now, thankfully.
Actually, reading your lines, I’ve had two separate ideas:
I used to use embedded searches with a Templater (or other plugin) script: paste them in below YAML with a shortcut and have another (non-Templater) script remove the query on a leaf-change/close (just so I don’t need to remember removing them manually). You can add that line in your note creation template as well, and if you comment them out, your readers won’t see the query even if you publish your notes.
I developed my own plugin to bookmark important lines in workspaces. The “remember-cursor-position-by-workspace” is not as exact as produced from idea 1, but I find the embedded searches a bit awkward or I’d have to use too many commented-out stuff, etc. So I use this second method now, for other reasons. I am afraid the link to the plugin is not be shown publicly, as you need to be in the community plugin list, etc.
@Sunnaq445, thanks for the ideas. I may be making this too hard.
I realized that calling a shell command probably wouldn’t work on mobile (where I do most of my reading), and yup, the plugin doesn’t even run on my iPad.
It occurred to me that I could use Templater or other scripting tool to execute a Javascript regex search and maybe place the cursor. I use this command in another script: <% tp.file.cursor(0) %><%* app.workspace.activeLeaf.view.editor?.focus(); %>
I already use a couple of plugins to show highlights and comments in a sidebar, and it would be simple to use them to find my bookmark. I use that particular string because I previously used the Advanced Annotation plugin to color comments with that syntax blue. Sadly, that doesn’t run on mobile.
It’s fairly simple to find the bookmark, and my interest at this point is probably more about having fun making computers do what I want—hence, the over-engineered solution.
It’s good flexing one’s muscles on plugins helping with automation, upkeep, etc.
Speaking of regex, I use Dynamic Highlights to mark various texts in my documents, but they don’t help with cursor position. (There are another two plugins with the same functionality; a fork of this and one made from scratch (Regex Mark).
There may be so many plugins out there I cannot keep track. Those brought up by you were new to me too.
I know CawlinTeffid would simply use Embedded Searches for this use case, but I also like to tinker. Well, that is at least true in the past tense.
@Sunnaq445 Thanks—just seeing this. I’m familiar with Dynamic Highlights and Regex Mark or at least plugins that are similar. I haven’t had time recently to play with this, but I’ll probably circle back to it at some point. Currently, I have too much actual work to do, and this isn’t a big deal. Mostly, it seems like it should be possible, and I’m a little obsessive.