I will be demonstrating how to write a program that periodically takes a screenshot of your PC or Mac. The program works even if you have multiple screens. You will be able to start, stop and reset the timer. We will be using the Electron framework to make this work.


Create an empty project in Electron and install screenshot-desktop from npm.

$ npm install --save screenshot-desktop

Add the package to your package.json file under the dependencies section

 "dependencies": {

    "screenshot-desktop": "^1.12.7"

  }

Add the command to take a screenshot in the main function and save screenshots from all your screens to a local file


app.whenReady().then(() => {
screenshot.all({format: 'png'}).then((imgs) => {
imgs.forEach((img, index) => {
screenshot_filename = path.join(appDatatDirPath,(from_timestamp*1000)+"_"+index+".png");
fs.writeFile(screenshot_filename, img, (err) => {
if (err) {
console.log("Error saving a screenshot!");
} else {
console.log("Saved screenshots");
}
});

})
}).catch((err) => {
if (debugMode) {
console.log("Error taking a screenshot!");
}
})
})

Run your program. A screenshot will be taken when the app is ready, yeah!

Cool but not useful, the program will only take one screenshot, and most probably of your IDE. Let's make it take screenshots on a regular basis. We will use the setInterval function and call it every 5 minutes

function takeScreenshot() {
setInterval(screenshotInterval, 5 * 60 * 1000);
}

and change the whenReady() call to

function screenshotInterval() {

creenshot.all({format: 'png'}).then((imgs) => {
imgs.forEach((img, index) => {
screenshot_filename = path.join(appDatatDirPath,(from_timestamp*1000)+"_"+index+".png");
fs.writeFile(screenshot_filename, img, (err) => {
if (err) {
console.log("Error saving a screenshot!");
} else {
console.log("Saved screenshots");
}
});

})
}).catch((err) => {
if (debugMode) {
console.log("Error taking a screenshot!");
}
})
}

  and

app.on('ready', takeScreenshot);


Run the app and it should take a screenshot and save the files to your Electron working directory every 5 minutes. Yeah!

Cooler but still not great, we want to save the files online, I'm a fan of AWS S3, so let's replace the write to file function by write to file and upload to S3. We will need the aws-sdk package for Electron. Install and add it to your dependencies list under package.json

$ npm install --save aws-sdk

Add the package to your package.json file under the dependencies section

 "dependencies": {

    "aws-sdk": "^2.1165.0"

  }

Setup your AWS credentials at the beginning of the program

const ID = 'MY_AWS_ID';
const SECRET = 'MY_AWS_SECRET';
const BUCKET_NAME = 'my-bucket-name';

const s3 = new AWS.S3({
accessKeyId: ID,
secretAccessKey: SECRET
});


Add the following right after saving to a local file, this will upload the file to the AWS bucket

const fileContent = fs.readFileSync(file_name);

const params = {
Bucket: BUCKET_NAME,
Key: file_name,
Body: fileContent
};
var putObjectPromise = s3.putObject(params).promise();
putObjectPromise.then(function(data) {
fs.unlink(file_name, (err) => {
if (err) throw err //handle your error the way you want to;

});
}).catch(function(err) {
console.log(err);
});

Et voilà! We are now getting our files saved from our PC/Mac on the AWS folder. Keep in mind that files uploaded with this method are, by default, accessible to the admin only. So, depending on your AWS settings, you might need to change the permissions to 'READ' for connected or all users.

Similar Stories


Enterprise

Automatic timesheets in Cosmolex

To sync phone records with Chrometa time entries, first export from T-Mobile, then import back into Chrometa. Read More

Enterprise

5 Resources to Boost Your Freelance Productivity

The modern freelancer has a lot of plates to spin on a daily basis in order to succeed – and there never seems to be enough hours in the day. Those that use their limited time most efficiently will blow past the competition and make an impact in their chosen market. . Read More

Enterprise

6 Tips to Maintain a Healthy Work-Life Balance during COVID

Confinement, lockdown, quarantine, shelter-in-place… .... Read More