Jump to content

Open Obsidian note


Recommended Posts

I have all of my bookmarks in Obsidian now and I am slowly working on a workflow to make it easier to post the links in the appropriate note. The first part was to make a script to list the notes in the vault and then open one

 

CleanShot2023-04-01at20_53.31@2x.thumb.jpg.3b94d1a670158d55e1a0a303ae7d3228.jpg

 

So a super simple Python script

 

import os
import json
import copy
from pathlib import Path
import urllib.parse

vaultPathData = os.getenv("obVaultPath") 
vaultName = os.getenv("obVaultName") 

vaultPath = Path(vaultPathData)
vaultPath = vaultPath.resolve()

fileList = []

#first get the root level files
rootFiles = vaultPath.glob('*.md')

for mdFile in rootFiles:
    fileList.append(['', mdFile.name, mdFile.stem])

#now the files in any directories
directoryFiles = vaultPath.rglob('*.md')

for mdFile in directoryFiles:
    fileList.append([mdFile.parts[-2], mdFile.name, mdFile.stem])

#now build the JSON data
# default item in JSON/dict format
fileItem = {
        "type": "file",
        "title": "",
        "subtitle": "",
        "arg": "",
        "icon": {
            "type": "fileicon",
            "path": "./MarkdownFileIcon.jpg"
        }
    }

# the JSON wrapper that we will be putting items into
jsonFileList = {"items": [] }

for file in fileList:
    thisJSONItem = copy.deepcopy(fileItem)
    thisJSONItem["title"] = file[2]
    thisJSONItem["subtitle"] = file[1]
    thisJSONItem["arg"] = "obsidian://advanced-uri?filepath=" + urllib.parse.quote(file[2], safe='')

    jsonFileList["items"].append(thisJSONItem)

# print out the JSON data        
print(json.dumps(jsonFileList))

 

The arg is the Advanced URI to the specific note. The Script Filter makes a list of all of the .md files at the root of the Vault and then in each directory. It presents a list and when one is selected it is opened. 

 

So now I can quickly open the Note that stores the type of URL I want to save. 

Link to comment
  • 2 weeks later...

@pixelgeek I’ve found no link to the ready-made workflow on your GitHub or the blog post. Care to do so and submit for the Gallery?

 

There’s also this bit in the blog post:

 

Quote

Alfred stores file and directory paths starting with a tilde

 

~/Documents/Obsidian Vaults/Note and Links

 

Python is not always that jazzed about using these types of paths and so when the user supplied path is read from Alfred it has to be resolved into a posix path.

 

/Users/Zac/Documents/Obsidian Vaults/Note and Links

 

That’s not quite right. The tilde is a Unix convention for the home folder. Alfred usually shows that in the path, but when it knows something is a path it will pass on the expanded text for the user’s machine (i.e. /Users/Zac for you). For example, when a path starting with a tilde is used in a File Picker configuration, your script won’t see the tilde when you call the variable, it will get the correct path outright. In other words, no need to have Python do that part.

Link to comment
15 hours ago, vitor said:

For example, when a path starting with a tilde is used in a File Picker configuration, your script won’t see the tilde when you call the variable, it will get the correct path outright. In other words, no need to have Python do that part.

 

I had to add that to the code because I was getting an error in Python and the path that was being supplied to my Python script had a tilde in it. I will go back and see if I can generate the error again.

 

I will need to go back to this and re-examine it as I added some features to it. 

 

Oddly, I thought that I had posted the workflow but clearly I didn't. Weird. 

Link to comment
3 hours ago, pixelgeek said:

I had to add that to the code because I was getting an error in Python and the path that was being supplied to my Python script had a tilde in it. I will go back and see if I can generate the error again.

 

Well whatever the heck was causing the problem isn't happening now so I am not sure what I was doing. 🙂

 

I've updated the code to remove that parse() function call.

Link to comment

The configuration options are missing labels. Those are necessary to identify options, especially because some are required. Note the orange message at the bottom.

 

bJ2bstr.png

Since there’s configuration, a configurable keyword would be nice too.

 

The instructions direct users to install Python from the website but that’s unnecessary and can be confusing. A default installation of Python 3 has a python3 binary, not python (that’s v2), so people running python --version will erroneously think they don’t have it. Using /usr/bin/python3 in the language dropdown is enough. macOS will check for it and if a user does not have it the Developer Tools dialog will show up and help them install it. Getting Python from their website is not needed and won’t do anything in this case. It may even lead to breakage due to conflicting versions and needing to install certificates.


It’s not clear why a vault’s name is required when it doesn’t seem to be used in the code. It’s set in a variable but then never acted upon. Perhaps it was forgotten from a previous approach?

 

On the Script Filter every item is show twice. As in, the full list is added twice to the results. It’s also quite atypical to override the argument to serve as a placeholder (“Select a Note”), that’s not what the Inbound Configuration is meant for. Instead, if you tick the Alfred filters results box (and set the Keyword to Argument Optional) you’ll make it way more useful by still showing every note but allowing users to type to filter for the one they’re looking for instead of having to manually search for it by pressing the arrow keys.

 

Another tip on that regard is to have the Script Filter be first. There’s no need to copy the URL immediately, so by having the Script Filter first you don’t even need the Keyword, you can go straight to the note selection and copy the browser URL after pressing ↩.

 

Other than that it seems to work as intended. It was a tad confusing when Obsidian was opening with the note but then I realised it both opens the note and copies the link to it. That is intentional, isn’t it?

 

Edited by vitor
Link to comment
On 4/18/2023 at 1:44 PM, vitor said:

On the Script Filter every item is show twice.

 

Really? I don't have that issue here. Can you post a tree of the directory structure you were using?

 

On 4/18/2023 at 1:44 PM, vitor said:

It was a tad confusing when Obsidian was opening with the note but then I realised it both opens the note and copies the link to it. That is intentional, isn’t it?

 

It shouldn't copy the link. I didn't do anything to get the URL to paste. 

 

I'll look into the other issues you mentioned.

Link to comment
19 hours ago, pixelgeek said:

Can you post a tree of the directory structure you were using?

 

It’s a direct copy of the Sandbox Vault:

 

.
├── Adventurer
│   ├── From plain-text note-taking.md
│   ├── From standard note-taking.md
│   └── No prior experience.md
├── Formatting
│   ├── Blockquote.md
│   ├── Callout.md
│   ├── Code block.md
│   ├── Comment.md
│   ├── Diagram.md
│   ├── Embeds.md
│   ├── Emphasis.md
│   ├── Footnote.md
│   ├── Format your notes.md
│   ├── Heading.md
│   ├── Highlighting.md
│   ├── Horizontal divider.md
│   ├── Images.md
│   ├── Inline code.md
│   ├── Internal link.md
│   ├── Links.md
│   ├── Lists.md
│   ├── Math.md
│   ├── Strikethrough.md
│   ├── Table.md
│   └── Task.md
├── Guides
│   ├── Create a vault.md
│   ├── Create your first note.md
│   ├── Get started with Obsidian.md
│   └── Link notes.md
├── Plugins make Obsidian special for you.md
├── Start Here.md
└── Vault is just a local folder.md

 

With this configuration:

 

5tslEfi.png

I see:

 

mUkcvPK.png

19 hours ago, pixelgeek said:

It shouldn't copy the link. I didn't do anything to get the URL to paste. 

 

Ah yes, it’s not the link to the note, but the link to the webpage so you can then paste in the open note. I see.

Link to comment
  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...