@mac-bug-screenshot/native-host 1.0.9 → 1.0.11

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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/host.js +96 -5
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -22,7 +22,7 @@ mac-bug-screenshot-install <확장_프로그램_ID>
22
22
  ## 게시 정보
23
23
 
24
24
  - npm 패키지: https://www.npmjs.com/package/@mac-bug-screenshot/native-host
25
- - 현재 버전: 1.0.5
25
+ - 현재 버전: 1.0.10
26
26
 
27
27
  ## 문제 해결
28
28
 
package/host.js CHANGED
@@ -28,19 +28,110 @@ 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
+
43
+ function runCommandOutput(command, args) {
44
+ return new Promise((resolve, reject) => {
45
+ execFile(command, args, (error, stdout) => {
46
+ if (error) {
47
+ reject(error);
48
+ return;
49
+ }
50
+ resolve(stdout);
51
+ });
52
+ });
53
+ }
54
+
55
+ function sleep(ms) {
56
+ return new Promise((resolve) => setTimeout(resolve, ms));
57
+ }
58
+
59
+ function runAppleScript(script) {
60
+ return new Promise((resolve, reject) => {
61
+ execFile("osascript", ["-e", script], (error) => {
62
+ if (error) {
63
+ reject(error);
64
+ return;
65
+ }
66
+ resolve();
67
+ });
68
+ });
69
+ }
70
+
71
+ async function setPreviewActualSize() {
72
+ const script = [
73
+ 'tell application "Preview"',
74
+ "activate",
75
+ "delay 0.2",
76
+ "tell application \"System Events\" to tell process \"Preview\"",
77
+ "if exists (menu bar 1) then",
78
+ "click menu item \"Actual Size\" of menu 1 of menu item \"Zoom\" of menu 1 of menu bar 1",
79
+ "end if",
80
+ "end tell",
81
+ "end tell"
82
+ ].join("\n");
83
+
84
+ try {
85
+ await runAppleScript(script);
86
+ } catch (error) {
87
+ // Ignore AppleScript failures (permission or menu layout).
88
+ }
89
+ }
90
+
91
+ async function readImageSize(filePath) {
92
+ try {
93
+ const output = await runCommandOutput("sips", ["-g", "pixelWidth", "-g", "pixelHeight", filePath]);
94
+ const widthMatch = output.match(/pixelWidth:\s+(\d+)/);
95
+ const heightMatch = output.match(/pixelHeight:\s+(\d+)/);
96
+ if (!widthMatch || !heightMatch) {
97
+ return null;
98
+ }
99
+ return {
100
+ width: Number(widthMatch[1]),
101
+ height: Number(heightMatch[1])
102
+ };
103
+ } catch (error) {
104
+ return null;
105
+ }
106
+ }
107
+
31
108
  async function capture(outputDir) {
32
109
  const resolvedDir = expandHome(outputDir || DEFAULT_OUTPUT_DIR);
33
110
  const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
34
111
  const outputPath = path.join(resolvedDir, `screenshot-${timestamp}.png`);
35
112
 
36
113
  await fs.promises.mkdir(resolvedDir, { recursive: true });
37
- await runCommand("screencapture", ["-i", "-s", "-t", "png", outputPath]);
114
+ await sleep(300);
115
+ try {
116
+ await runCommandDetailed("screencapture", ["-i", "-s", "-t", "png", outputPath]);
117
+ } catch (error) {
118
+ await sleep(300);
119
+ await runCommandDetailed("screencapture", ["-i", "-s", "-t", "png", outputPath]);
120
+ }
38
121
  if (!fs.existsSync(outputPath)) {
39
122
  throw new Error("캡처가 취소되었습니다.");
40
123
  }
41
- await runCommand("open", ["-a", "Preview", outputPath]);
124
+ const dimensions = await readImageSize(outputPath);
125
+ try {
126
+ await runCommandDetailed("open", ["-a", "Preview", outputPath]);
127
+ } catch (error) {
128
+ await sleep(300);
129
+ await runCommandDetailed("open", ["-a", "Preview", outputPath]);
130
+ }
131
+ await sleep(200);
132
+ await setPreviewActualSize();
42
133
 
43
- return outputPath;
134
+ return { outputPath, dimensions };
44
135
  }
45
136
 
46
137
  function writeMessage(message) {
@@ -88,8 +179,8 @@ async function main() {
88
179
  return;
89
180
  }
90
181
 
91
- const outputPath = await capture(message.outputDir);
92
- writeMessage({ ok: true, outputPath });
182
+ const result = await capture(message.outputDir);
183
+ writeMessage({ ok: true, outputPath: result.outputPath, dimensions: result.dimensions });
93
184
  } catch (error) {
94
185
  writeMessage({ ok: false, error: error.message });
95
186
  } finally {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mac-bug-screenshot/native-host",
3
- "version": "1.0.9",
3
+ "version": "1.0.11",
4
4
  "description": "Native messaging host for Mac Bug Screenshot.",
5
5
  "bin": {
6
6
  "mac-bug-screenshot-install": "bin/install.js"