JXA

Code

1
// Get URL of front most tab
2
Application("Google Chrome Canary").windows[0].activeTab.url();
Copied!
1
// Activate app
2
it = Application("Safari");
3
it.activate();
Copied!
1
// Minimize all Safari windows but the currently focused one
2
safari = Application("Safari");
3
​
4
// Front most window has index 0
5
for (var i = 1; i < safari.windows.length; i++) {
6
var win = safari.windows[i];
7
if (win.miniaturized() === false) win.miniaturized = true;
8
}
Copied!
1
// Get selected text in Chrome Canary
2
Application("Google Chrome Canary").windows[0].activeTab.execute({
3
javascript: "window.getSelection().toString()",
4
});
Copied!
1
// Return current active line in TaskPaper.
2
(() => {
3
"use strict";
4
​
5
const main = () => {
6
const tp3Context = (editor, options) => {
7
const main = () =>
8
unlines(
9
concatMap((x) => {
10
const txt = x[options.textProperty];
11
return options.skipBlankLines && 0 === txt.length ? [] : [txt];
12
}, editor.selection.selectedItems)
13
);
14
​
15
// GENERIC FUNCTIONS FOR TP3 CONTEXT ----------
16
// https://github.com/RobTrew/prelude-jxa
17
​
18
// concatMap :: (a -> [b]) -> [a] -> [b]
19
const concatMap = (f, xs) => xs.reduce((a, x) => a.concat(f(x)), []);
20
​
21
// unlines :: [String] -> String
22
const unlines = (xs) => xs.join("\n");
23
​
24
// TP3 MAIN ---
25
return main();
26
};
27
​
28
const ds = Application("TaskPaper").documents,
29
lrResult = bindLR(
30
ds.length > 0 ? Right(ds.at(0)) : Left("No TaskPaper documents open"),
31
(d) =>
32
Right(
33
d.evaluate({
34
script: tp3Context.toString(),
35
withOptions: {
36
skipBlankLines: true,
37
textProperty: "bodyContentString", // or 'bodyContentString'
38
},
39
})
40
)
41
);
42
return lrResult.Left || lrResult.Right;
43
};
44
​
45
// GENERIC FUNCTIONS FOR JXA CONTEXT ------------------
46
// https://github.com/RobTrew/prelude-jxa
47
​
48
// Left :: a -> Either a b
49
const Left = (x) => ({
50
type: "Either",
51
Left: x,
52
});
53
​
54
// Right :: b -> Either a b
55
const Right = (x) => ({
56
type: "Either",
57
Right: x,
58
});
59
​
60
// bindLR (>>=) :: Either a -> (a -> Either b) -> Either b
61
const bindLR = (m, mf) => (undefined !== m.Left ? m : mf(m.Right));
62
​
63
// MAIN ----
64
return main();
65
})();
Copied!

Links

Last modified 4d ago
Copy link
Contents
Code
Links