I had a breakthrough! And it doesn’t involve any extra code in Obsidian so far!
- open
~/Library/Application Support/obsidian/obsidian.json
- look at
last_open
to see what vault was last viewed, and make note of it - look at
vaults
to find that vault - make note of the vault’s path
- open
path/to/vault/.obsidian/workspace
- look at the first item in
lastOpenFiles
to get the relative path - generate a obsidian with the vault and file path:
obsidian://open?vault=<vault id>&file=<relative path>
It doesn’t look like the obsidian URI supports creating new files yet. It could be done by using a similar approach, and then creating a file in the vault’s path.
That said, I got it working Here are Hook scripts for Obsidian
Open Hook -> Preferences -> Scripts -> “+” and then browse select Obsidian
For “Get Name”:
//JavaScript
(() => {
'use strict';
const main = () => {
const app = Application.currentApplication()
app.includeStandardAdditions = true
const homeDirectory = app.pathTo("home folder").toString()
const obsidianJSONPath = `${homeDirectory}/Library/Application Support/obsidian/obsidian.json`
const obsidianData = JSON.parse(app.read(Path(obsidianJSONPath)))
const vaultId = obsidianData["last_open"]
const vault = obsidianData["vaults"][vaultId]
const vaultPath = vault["path"]
const workspaceJSONPath = `${vaultPath}/.obsidian/workspace`
const workspaceData = JSON.parse(app.read(Path(workspaceJSONPath)))
const currentDocument = workspaceData["lastOpenFiles"][0]
const slashParts = currentDocument.split("/")
const basename = slashParts[slashParts.length - 1]
if (basename) {
const title = basename.replace(/\.md$/, '')
return title
}
}
return main();
})()
For “Get Address”:
//JavaScript
(() => {
'use strict';
const main = () => {
const app = Application.currentApplication()
app.includeStandardAdditions = true
const homeDirectory = app.pathTo("home folder").toString()
const obsidianJSONPath = `${homeDirectory}/Library/Application Support/obsidian/obsidian.json`
const obsidianData = JSON.parse(app.read(Path(obsidianJSONPath)))
const vaultId = obsidianData["last_open"]
const vault = obsidianData["vaults"][vaultId]
const vaultPath = vault["path"]
const workspaceJSONPath = `${vaultPath}/.obsidian/workspace`
const workspaceData = JSON.parse(app.read(Path(workspaceJSONPath)))
const currentDocument = workspaceData["lastOpenFiles"][0]
const uri = `obsidian://open?vault=${vaultId}&file=${encodeURIComponent(currentDocument)}`
return uri;
}
return main();
})()