-
Notifications
You must be signed in to change notification settings - Fork 0
/
screenshot.js
executable file
·127 lines (113 loc) · 3.32 KB
/
screenshot.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env osascript -l JavaScript
function run(argv) {
const rectangleId = "com.knollsoft.Rectangle";
const defaultSize = getDefaultSize(rectangleId);
const { dock, menuBar } = currentDockAndMenuBar();
const command = argv[0];
const targetSize = {
width: parseInt(argv[1]) || defaultSize.width,
height: parseInt(argv[2]) || defaultSize.height,
};
switch (command) {
case "pose":
moveToCenter(defaultSize, targetSize, rectangleId);
break;
case "screenshot":
try {
hidedockAndMenuBar();
hideOthers();
moveToCenter(defaultSize, targetSize, rectangleId);
screenshot(getRect(targetSize));
} finally {
setDockAndMenuBar(dock, menuBar);
}
break;
default:
break;
}
}
function currentDockAndMenuBar() {
const dockPreferences = new Application("System Events").dockPreferences();
return {
dock: dockPreferences.autohide(),
menuBar: dockPreferences.autohideMenuBar(),
};
}
function setDockAndMenuBar(hideDock, hideMenuBar) {
const dockPreferences = new Application("System Events").dockPreferences();
dockPreferences.autohide = hideDock;
dockPreferences.autohideMenuBar = hideMenuBar;
}
function hidedockAndMenuBar() {
setDockAndMenuBar(true, true);
}
function hideOthers() {
new Application("System Events")
.processes()
.filter((process) => process.visible() && !process.frontmost())
.forEach((process) => (process.visible = false));
}
function moveToCenter(defaultSize, targetSize, rectangleId) {
const rectangle = new Application("Rectangle");
if (JSON.stringify(defaultSize) !== JSON.stringify(targetSize)) {
const { width, height } = targetSize;
const write = `defaults write ${rectangleId}`;
rectangle.quit();
runCommand(`${write} specifiedWidth -float ${width}`);
runCommand(`${write} specifiedHeight -float ${height}`);
}
if (!rectangle.running()) {
runCommand(`open -b ${rectangleId}`);
}
runCommand('open -g "rectangle://execute-action?name=specified"');
}
function screenshot(rect) {
runCommand(
[
"screencapture",
"-m",
"-T 1",
`-R ${rect.x},${rect.y},${rect.width},${rect.height}`,
"${HOME}/Desktop/$(date +%Y%m%d-%H%M%S).png",
].join(" ")
);
}
function runCommand(command) {
try {
const app = Application.currentApplication();
app.includeStandardAdditions = true;
return app.doShellScript(command);
} catch {
return "";
}
}
function getScreenSize() {
const json = runCommand("displays");
const displays = JSON.parse(json);
const display = displays.find((display) => display.isMain);
if (display === undefined) {
return;
}
return {
width: display.resolution.width,
height: display.resolution.height,
topInset: display.safeAreaInsets.top,
};
}
function getRect(targetSize) {
const padding = 160;
const size = getScreenSize();
return {
x: (size.width - targetSize.width - padding) / 2,
y: (size.height - targetSize.height + size.topInset - padding) / 2,
width: targetSize.width + padding,
height: targetSize.height + padding,
};
}
function getDefaultSize(rectangleId) {
const read = `defaults read ${rectangleId}`;
return {
width: parseInt(runCommand(`${read} specifiedWidth`)) || 800,
height: parseInt(runCommand(`${read} specifiedHeight`)) || 600,
};
}