Skip to content

Commit 212f424

Browse files
0xganzbencevans
authored andcommitted
feat(osx): support multi screenshot for multi screens on mac (#101)
* screenshot support multi screens * ignore files * link and fix delete file error * examples * remove unused * lint
1 parent 68eee48 commit 212f424

File tree

5 files changed

+71
-22
lines changed

5 files changed

+71
-22
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ node_modules
22
out.jpg
33
*.exe
44
*.log
5+
*.png
6+
package-lock.json

.vscode/launch.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
// 使用 IntelliSense 了解相关属性。
3+
// 悬停以查看现有属性的描述。
4+
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "debug one File",
11+
"args": [
12+
// "--enable-logging"
13+
],
14+
"console": "integratedTerminal",
15+
"program": "${file}",
16+
"protocol": "inspector"
17+
}
18+
]
19+
}

example.js renamed to examples/example.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ screenshot().then((img) => {
66
if (err) {
77
throw err
88
}
9-
109
console.log('written to out.jpg')
1110
})
11+
}).catch((err) => {
12+
throw err
1213
})
13-
.catch((err) => {
14-
throw err
15-
})

examples/multiScreens.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const path = require('path')
2+
const screenshot = require('..')
3+
4+
screenshot.listDisplays()
5+
.then((displays) => {
6+
console.log(displays)
7+
for (let index = 0; index < displays.length; index++) {
8+
const display = displays[index]
9+
const imgpath = path.join(__dirname, Date.now() + '_' + index + '.png')
10+
screenshot({ screen: display.id, filename: imgpath }).then((imgpath) => {
11+
console.log(imgpath)
12+
}).catch(err => {
13+
console.error(err)
14+
})
15+
}
16+
})
17+
18+
screenshot.listDisplays()
19+
.then((displays) => {
20+
console.log(displays)
21+
for (let index = 0; index < displays.length; index++) {
22+
const display = displays[index]
23+
screenshot({ screen: display.id }).then((imgbuf) => {
24+
console.log(imgbuf)
25+
}).catch(err => {
26+
console.error(err)
27+
})
28+
}
29+
})

lib/darwin/index.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,18 @@ function darwinSnapshot (options = {}) {
2626
.fill(null)
2727
.map(() => temp.path({ suffix }))
2828

29-
let pathsToDelete = tmpPaths
29+
let pathsToDelete = []
3030
if (options.filename) {
3131
tmpPaths[displayId] = filename
32-
pathsToDelete = tmpPaths.slice(0, -1)
3332
}
33+
pathsToDelete = tmpPaths.slice(displayId)
3434

35-
exec('screencapture -x -t ' + suffix.slice(1) + ' ' + tmpPaths.join(' '),
35+
exec('screencapture' + ' -D ' + (displayId + 1) + ' ' + ' -x -t ' + suffix.slice(1) + ' ' + tmpPaths.slice(displayId).join(' '),
3636
function (err, stdOut) {
3737
if (err) {
3838
return reject(err)
3939
} else if (options.filename) {
40-
Promise.all(pathsToDelete.map(unlinkP))
41-
.then(() => resolve(path.resolve(options.filename)))
42-
.catch((err) => reject(err))
40+
resolve(path.resolve(options.filename))
4341
} else {
4442
fs.readFile(tmpPaths[displayId], function (err, img) {
4543
if (err) {
@@ -141,23 +139,26 @@ function parseDisplaysOutput (output) {
141139
return []
142140
}
143141

144-
const firstGpuKey = Object.keys(tree['Graphics/Displays'])[0]
145-
if (!firstGpuKey) {
142+
const firstGpuKeys = Object.keys(tree['Graphics/Displays'])
143+
if (!firstGpuKeys || firstGpuKeys.length <= 0) {
146144
return []
147145
}
148146

149-
const firstGpu = tree['Graphics/Displays'][firstGpuKey]
150-
if (!firstGpu) {
151-
return []
152-
}
147+
let displayinfos = []
153148

154-
const displays = Object.entries(firstGpu['Displays'])
155-
.map(([name, props]) => {
156-
const primary = props['Main Display'] === 'Yes'
157-
return { name, primary }
158-
})
149+
firstGpuKeys.forEach(gpukey => {
150+
const gpu = tree['Graphics/Displays'][gpukey]
151+
if (gpu.Displays) {
152+
const temp = Object.entries(gpu.Displays)
153+
.map(([name, props]) => {
154+
const primary = props['Main Display'] === 'Yes'
155+
return { name, primary }
156+
})
157+
displayinfos = displayinfos.concat(temp)
158+
}
159+
})
159160

160-
return addId(movePrimaryToHead(displays))
161+
return addId(movePrimaryToHead(displayinfos))
161162
}
162163

163164
function listDisplays () {

0 commit comments

Comments
 (0)