Building a daily desktop background app with node.js

Absolute programmer
5 min readFeb 6, 2022

--

Introduction

The desktop background keeps the same everyday which might make you feel boring. However, it would be fantastic when the background changes on a daily basis and with a nice looking picture. In the following paragraphs, a daily desktop background app will be built using node.js.

Structure

The desktop background app has following logic:

  1. Use the Unsplash api to fetch the random photo information
  2. Use the image-downloader module to download the photo to disk
  3. Use the wallpaper module to set the photo as the background picture
  4. Build a script to run the node.js script
  5. Enable the script to run on daily basis

Use the Unsplash api to fetch the random photo information

By CSS-Tricks

The Unsplash API is a modern JSON API which provides information about high quality photo. It provides 3 major functions on retrieving the photo you want including getting a specific photo , searching photo and listing photos. In this article, we are going to retrieve a random photo through Unsplash API . The following is the procedure to retrieve data from Unsplash API:

  1. Register in Unsplash

Proceed to https://unsplash.com/ and make a registration.

2. Create a new application in the applications section

Proceed to https://unsplash.com/oauth/applications and select New Application. Then, Enter the details required.

3. Download Unsplash module using npm

npm i --save unsplash-js

4. Import unsplash and nodeFetch module

import nodeFetch from “node-fetch”; 
import { createApi } from “unsplash-js”;

5. Create an Unsplash api instance

There are two parameters you need to pass. The first parameter is the access key retrieved from the Unsplash application created above. By clicking the application created and scrolling down, the access key will be shown.

The second parameter is nodeFetch which is imported in the begining.

const unsplash = createApi({   
accessKey: accessKeyFromUnsplash ,
fetch: nodeFetch,
});
Access Key

5. Retrieve a single random photo information by calling photos.getRandom() method

photo.getRandom retrieve a single random photo information as json structure. The random photo can be filtered by providing arguments. For example, you can have a random photo from a category such as dog by giving argument ,”query”, a string with dog. For further information, you can refer to https://github.com/unsplash/unsplash-js

In this example, we just need a single photo so we just need to input following code:

unsplash.photos.getRandom({}).then((result) => {
if (result.errors) {
// handle error here
console.log("error occurred: ", result.errors[0]);
} else {
// handle success here
const photo = result.response;
}
});

unsplash.photos.getRandom( ) is a promise. The call back function will receive a result sent from Unsplash. If the result contains no error, the response will contain a bunch of json attributes but we only care about “urls” attribute. We will use the url stored in the attribute , “regular” to download the photo :

Use the image-downloader module to download the photo to disk

In order to download the photo from the URL directly , we can using a module called “image-downloader”. First, we need to download the module using npm :

npm install --save image-downloader

and import the module:

import download from "image-downloader";

In order to download the image, we need to call the image method on the image-downloader module imported. The image method needs two parameter, url and dest . The url will be the link containing the photo which is retrieved from Unsplash api. Dest will be the path the photo will be saved in the disk which I set it to the same folder containing the node script and name the downloaded photo as photo.jpg .

download.image({
url: photo.urls.raw,
dest: "./photo.jpg",
})

Use the wallpaper module to set the photo as the background picture

Wallpaper module is a module that can get or set the desktop wallpaper but it works on macOS 10.14.4+, Linux, and Windows 10+. First, download the wallpaper module by using npm:

npm install wallpaper

and import the setWallpaper function from it:

import {setWallpaper} from "wallpaper";

The image method on the image-downloader is a promise too. A json object containing the file name just downloaded will be passed to the callback function. The filename will be provided to setWallpaper function as an argument which will then set the photo as the desktop picture.

download.image({
url: photo.urls.raw,
dest: "./photo.jpg",
}).then((file) => {
setWallpaper(file.filename);
});

Full Code

Build a script to run the node.js script

For Windows user, you simply just write a bat script and type node command to start the node script. Pause command is for debug purpose which allows the command prompt staying at the screen.

node "your node script location" 
pause

For Linux & Mac user, you can use bash script to include the node command also.

Enable the script to run on daily basis

  1. Right click your script to create a shortcut
  2. Right click the shortcut and left click properties
  3. Navigate to Shortcut tab and select advanced
  4. Tick Run as administrator
  5. Press Win + R
  6. Input shell:startup
  7. Copy the shortcut to the folder popped out

Double click the script to see if the desktop background change. Enjoy the new background.

--

--