forked from chrisgrieser/shimmering-obsidian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy-note-link.js
executable file
·41 lines (33 loc) · 1.84 KB
/
copy-note-link.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
#!/usr/bin/env osascript -l JavaScript
ObjC.import("stdlib");
const app = Application.currentApplication();
app.includeStandardAdditions = true;
//───────────────────────────────────────────────────────────────────────────
/** @type {AlfredRun} */
// biome-ignore lint/correctness/noUnusedVariables: Alfred run
function run(argv) {
const vaultPath = $.getenv("vault_path");
const vaultNameEnc = encodeURIComponent(vaultPath.replace(/.*\//, ""));
// import variables
let [relativePath, heading] = argv[0].split("#");
relativeEncodedPath = encodeURIComponent(relativePath);
heading = encodeURIComponent(heading);
const linkType = $.getenv("link_type_to_copy");
//───────────────────────────────────────────────────────────────────────────
let filenameNoExt = relativePath.split("/").pop().replace(/\.\w+$/, "");
let toCopy;
if (linkType === "Markdown Link" && heading && heading != 'undefined' ) {
const urlScheme = `obsidian://advanced-uri?vault=${vaultNameEnc}&filepath=${relativeEncodedPath}&heading=${heading}`;
filenameNoExt += "|" + heading;
toCopy = `[${filenameNoExt}](${urlScheme})`;
} else if (linkType === "Markdown Link") {
const urlScheme = `obsidian://open?vault=${vaultNameEnc}&file=${relativeEncodedPath}`;
toCopy = `[${filenameNoExt}](${urlScheme})`;
} else if (linkType === "Wikilink" && heading && heading != 'undefined' ) {
toCopy = `[[${filenameNoExt}#${heading}]]`;
} else if (linkType === "Wikilink") {
toCopy = `[[${filenameNoExt}]]`;
}
app.setTheClipboardTo(toCopy);
return toCopy; // also return for the notification
}