-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·68 lines (62 loc) · 2.29 KB
/
index.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
"strict mode";
/**
* @param {string} filename File Name (current dir) OR Full Path with file name
* @param {string} fformat File Format Like png , jpg , jpeg etc
*/
async function ss(filename, fformat) {
return new Promise((resolve, reject) => {
const dfname = filename || process.cwd() + "/" + Date.now();
const dformat = fformat || "jpg";
if (process.platform === "darwin") {
const applescript = require("applescript");
var ext = dfname + "." + dformat;
var script =
'do shell script "screencapture -x -t ' + dformat + " " + ext + '"';
applescript.execString(script, function (err, rtn) {
if (err) {
reject(err);
} else {
resolve(`${ext}`);
}
});
} else if (process.platform === "win32") {
const PowerShell = require("powershell");
var tt = dfname.replace(/\\/g, "/");
function capitalize(s) {
if (s.toLowerCase() == "jpg" || s.toLowerCase() == "jpeg") {
return "JPEG";
} else {
return s.toLowerCase();
}
}
var ext = capitalize(dformat);
var script = `Add-Type -AssemblyName System.Windows.Forms
$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$image = New-Object System.Drawing.Bitmap($screen.Width, $screen.Height)
$graphic = [System.Drawing.Graphics]::FromImage($image)
$point = New-Object System.Drawing.Point(0, 0)
$graphic.CopyFromScreen($point, $point, $image.Size);
$cursorBounds = New-Object System.Drawing.Rectangle([System.Windows.Forms.Cursor]::Position, [System.Windows.Forms.Cursor]::Current.Size)
[System.Windows.Forms.Cursors]::Default.Draw($graphic, $cursorBounds)
$screen_file = "${tt}.${dformat}"
$image.Save($screen_file, [System.Drawing.Imaging.ImageFormat]::${ext})`;
let ps = new PowerShell(script);
ps.on("error", (err) => {
reject(err);
});
ps.on("output", (data) => {
resolve(`${tt}.${dformat}`);
});
ps.on("error-output", (data) => {
reject(data);
});
} else {
module.exports = function unSupported() {
reject(
new Error("Currently unsupported platform. Pull requests welcome!")
);
};
}
});
}
module.exports = ss;