forked from chrisgrieser/shimmering-obsidian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault-switcher.js
executable file
·82 lines (71 loc) · 2.96 KB
/
vault-switcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env osascript -l JavaScript
ObjC.import("stdlib");
const app = Application.currentApplication();
app.includeStandardAdditions = true;
//──────────────────────────────────────────────────────────────────────────────
/** @param {string} path */
function readFile(path) {
const data = $.NSFileManager.defaultManager.contentsAtPath(path);
const str = $.NSString.alloc.initWithDataEncoding(data, $.NSUTF8StringEncoding);
return ObjC.unwrap(str);
}
/** @param {string} str */
function camelCaseMatch(str) {
const subwords = str.replace(/[-_./]/g, " ");
const fullword = str.replace(/[-_./]/g, "");
const camelCaseSeparated = str.replace(/([A-Z])/g, " $1");
return [subwords, camelCaseSeparated, fullword, str].join(" ") + " ";
}
//──────────────────────────────────────────────────────────────────────────────
/** @type {AlfredRun} */
// biome-ignore lint/correctness/noUnusedVariables: Alfred run
function run() {
const currentVault = $.getenv("vault_path");
const vaultNameEnc = encodeURIComponent(currentVault.replace(/.*\//, ""));
// get vault paths
const vaultListJson =
app.pathTo("home folder") + "/Library/Application Support/obsidian/obsidian.json";
const vaultList = JSON.parse(readFile(vaultListJson)).vaults;
const vaultPaths = [];
for (const hash in vaultList) vaultPaths.push(vaultList[hash].path);
/** @type {AlfredItem[]} */
const vaults = vaultPaths.map((vaultPath) => {
const vaultName = vaultPath.replace(/.*\//, "");
const vaultURI = "obsidian://open?vault=" + encodeURIComponent(vaultName);
// visual: icons & shorter path
let currentIcon = "";
if (currentVault === vaultPath) currentIcon = "✅ ";
if (vaultName === "Obsidian Sandbox") currentIcon += "🏖 ";
const tildePath = vaultPath.replace(/\/Users\/[^/]*/, "~");
const shortParentPath = tildePath.slice(0, -(vaultName.length + 1));
const shiftArg =
currentVault === vaultPath
? { valid: false, subtitle: "⛔️ Already controlling this vault." }
: { arg: tildePath };
return {
title: currentIcon + vaultName,
subtitle: shortParentPath,
arg: vaultURI,
match: camelCaseMatch(vaultName),
mods: {
alt: { arg: vaultPath },
ctrl: { arg: vaultPath },
shift: shiftArg,
},
uid: vaultURI,
};
});
vaults.push({
title: "Vault Menu",
subtitle: "Create or delete vaults",
arg: `obsidian://advanced-uri?vault=${vaultNameEnc}&commandid=app%253Aopen-vault`,
icon: { path: "icons/settings.png" },
mods: {
alt: { valid: false, subtitle: "⛔️" },
ctrl: { valid: false, subtitle: "⛔️" },
shift: { valid: false, subtitle: "⛔️" },
},
// no UID, so it's always at the bottom
});
return JSON.stringify({ items: vaults });
}