@mac-bug-screenshot/native-host 1.0.10 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/host.js +53 -4
- package/package.json +1 -1
package/README.md
CHANGED
package/host.js
CHANGED
|
@@ -28,6 +28,18 @@ function runCommand(command, args) {
|
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
function runCommandDetailed(command, args) {
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
execFile(command, args, { encoding: "utf8" }, (error, stdout, stderr) => {
|
|
34
|
+
if (error) {
|
|
35
|
+
reject(new Error(stderr || error.message));
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
resolve({ stdout, stderr });
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
31
43
|
function runCommandOutput(command, args) {
|
|
32
44
|
return new Promise((resolve, reject) => {
|
|
33
45
|
execFile(command, args, (error, stdout) => {
|
|
@@ -40,6 +52,10 @@ function runCommandOutput(command, args) {
|
|
|
40
52
|
});
|
|
41
53
|
}
|
|
42
54
|
|
|
55
|
+
function sleep(ms) {
|
|
56
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
57
|
+
}
|
|
58
|
+
|
|
43
59
|
function runAppleScript(script) {
|
|
44
60
|
return new Promise((resolve, reject) => {
|
|
45
61
|
execFile("osascript", ["-e", script], (error) => {
|
|
@@ -52,6 +68,29 @@ function runAppleScript(script) {
|
|
|
52
68
|
});
|
|
53
69
|
}
|
|
54
70
|
|
|
71
|
+
async function openInPreview(filePath) {
|
|
72
|
+
const script = [
|
|
73
|
+
'tell application "Preview"',
|
|
74
|
+
"activate",
|
|
75
|
+
`open POSIX file "${filePath}"`,
|
|
76
|
+
"end tell"
|
|
77
|
+
].join("\n");
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
await runAppleScript(script);
|
|
81
|
+
return;
|
|
82
|
+
} catch (error) {
|
|
83
|
+
// fallback to open command below
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
await runCommandDetailed("open", ["-a", "Preview", filePath]);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
await sleep(300);
|
|
90
|
+
await runCommandDetailed("open", ["-n", "-a", "Preview", filePath]);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
55
94
|
async function setPreviewActualSize() {
|
|
56
95
|
const script = [
|
|
57
96
|
'tell application "Preview"',
|
|
@@ -89,18 +128,28 @@ async function readImageSize(filePath) {
|
|
|
89
128
|
}
|
|
90
129
|
}
|
|
91
130
|
|
|
92
|
-
async function capture(outputDir) {
|
|
131
|
+
async function capture(outputDir, mode = "region") {
|
|
93
132
|
const resolvedDir = expandHome(outputDir || DEFAULT_OUTPUT_DIR);
|
|
94
133
|
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
95
134
|
const outputPath = path.join(resolvedDir, `screenshot-${timestamp}.png`);
|
|
96
135
|
|
|
97
136
|
await fs.promises.mkdir(resolvedDir, { recursive: true });
|
|
98
|
-
await
|
|
137
|
+
await sleep(300);
|
|
138
|
+
const captureArgs = mode === "full"
|
|
139
|
+
? ["-x", "-t", "png", outputPath]
|
|
140
|
+
: ["-i", "-s", "-t", "png", outputPath];
|
|
141
|
+
try {
|
|
142
|
+
await runCommandDetailed("screencapture", captureArgs);
|
|
143
|
+
} catch (error) {
|
|
144
|
+
await sleep(300);
|
|
145
|
+
await runCommandDetailed("screencapture", captureArgs);
|
|
146
|
+
}
|
|
99
147
|
if (!fs.existsSync(outputPath)) {
|
|
100
148
|
throw new Error("캡처가 취소되었습니다.");
|
|
101
149
|
}
|
|
102
150
|
const dimensions = await readImageSize(outputPath);
|
|
103
|
-
await
|
|
151
|
+
await openInPreview(outputPath);
|
|
152
|
+
await sleep(200);
|
|
104
153
|
await setPreviewActualSize();
|
|
105
154
|
|
|
106
155
|
return { outputPath, dimensions };
|
|
@@ -151,7 +200,7 @@ async function main() {
|
|
|
151
200
|
return;
|
|
152
201
|
}
|
|
153
202
|
|
|
154
|
-
const result = await capture(message.outputDir);
|
|
203
|
+
const result = await capture(message.outputDir, message.mode);
|
|
155
204
|
writeMessage({ ok: true, outputPath: result.outputPath, dimensions: result.dimensions });
|
|
156
205
|
} catch (error) {
|
|
157
206
|
writeMessage({ ok: false, error: error.message });
|