Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 36 ยท Native APIs and Files

Clipboard, Notifications and the Shell Module

Use Electron's clipboard, Notification and shell modules to copy text and images, show system notifications, open links safely and reveal files in the file manager.

Small touches make a desktop app feel native: a "Copy link" button that really fills the system clipboard, a notification when a long export finishes, a click that opens a document in the user's file manager. Electron wraps all of these in three tiny modules: clipboard, Notification and shell.

None of them can run inside a web page sandbox, so we show the real code read-only and build stand-ins to practise the logic: formatting what goes on the clipboard, queuing notifications, and deciding which URLs deserve to be opened at all.

The clipboard module

clipboard reads and writes the system clipboard. It supports several formats at once, so a paste target can pick the richest one it understands.

JavaScript
import { clipboard, nativeImage } from 'electron';

clipboard.writeText('https://example.com/invoice/42');
console.log(clipboard.readText());

clipboard.write({
  text: 'Quarterly report',
  html: '<b>Quarterly report</b>',
});

const png = nativeImage.createFromPath('chart.png');
clipboard.writeImage(png);

Facts worth knowing:

  • clipboard.write({ text, html, image, rtf, bookmark }) puts several representations on the clipboard in one call.
  • readText() returns '' when the clipboard has no text, never null.
  • clipboard.availableFormats() lists MIME types such as text/plain and text/html.
  • In a sandboxed renderer the module is not available. Use the browser's navigator.clipboard.writeText() (needs a user gesture) or expose a preload function that asks the main process.

Reading the clipboard silently is a privacy risk. A clipboard manager does it on purpose, but a normal app should read it only when the user clicks Paste.

A stand-in clipboard

This example fakes the clipboard as an object holding several formats, then writes a "rich copy" and a pasting function that chooses the best format available. It shows why multi-format writes exist.

Node.js Multi-format clipboard and paste targets

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

A rich editor takes the HTML, a terminal takes the text. Writing both costs you nothing and makes the copy useful everywhere.

System notifications

Electron has two notification paths. In the renderer you can use the web Notification API. In the main process you can use Electron's own Notification class, which works even when no window is open.

JavaScript
import { Notification } from 'electron';

if (Notification.isSupported()) {
  const n = new Notification({
    title: 'Export finished',
    body: 'report.pdf is ready in Downloads.',
    silent: false,
  });
  n.on('click', () => mainWindow.show());
  n.show();
}

On Windows, notifications only show properly when the app has an AppUserModelID; call app.setAppUserModelId('com.example.myapp') early, and keep it identical to the installer's ID. On macOS the user must allow notifications for your app in System Settings, and unsigned dev builds may be blocked silently.

Good notification manners:

  • Notify about things the user asked for (a finished export, a message), never marketing.
  • Give the click a purpose: focus the window and navigate to the item.
  • Do not stack twenty notifications; group or summarise them.

A notification queue stand-in

The runnable example models a small notifier that collapses bursts: if five downloads finish within a second it shows one summary rather than five pop-ups.

Node.js Collapsing bursts of notifications

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The shell module

shell connects your app to the desktop environment.

JavaScript
import { shell } from 'electron';

await shell.openExternal('https://example.com/help');   // default browser
await shell.openPath('/Users/ana/report.pdf');            // default app for that file
shell.showItemInFolder('/Users/ana/report.pdf');         // reveal in Finder or Explorer
await shell.trashItem('/Users/ana/old.txt');              // move to trash
shell.beep();

openExternal is the dangerous one. It hands a string to the operating system, which may happily launch file:, smb: or custom protocol handlers. Never pass it a URL that came from page content without checking the scheme.

JavaScript
function openIfSafe(url) {
  const { protocol } = new URL(url);
  if (protocol === 'https:' || protocol === 'mailto:') {
    return shell.openExternal(url);
  }
}

This is why navigation handlers route every outside link through an allowlist.

A scheme allowlist you can test

Node.js Which URLs may be opened externally?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Note that plain http: is blocked here on purpose. Decide your own policy, but make it an explicit list, never "everything except a few bad ones".

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does clipboard.write accept text and html together?

    A rich editor pastes the HTML, a terminal pastes the plain text. Providing both makes one copy useful in many apps.
  2. 2. A page link is passed straight to shell.openExternal(url). What is the main risk?

    openExternal asks the operating system to open any URL. Without a scheme allowlist, hostile content can trigger local files or protocol handlers.
  3. 3. Which call reveals a file in Finder or Explorer?

    showItemInFolder opens the containing folder with the file selected. openPath would launch the file in its default application.
  4. 4. On Windows, what must be set for notifications to display reliably?

    Windows ties notifications to an AppUserModelID. Setting one matching your installer makes them appear and group correctly.

Summary

  • clipboard.write can store text, HTML and images together; read the clipboard only on user action.
  • Notification works from the main process; set an AppUserModelID on Windows and collapse bursts.
  • shell opens links, files and folders, and moves items to the trash.
  • Allowlist URL schemes before shell.openExternal.

Next: App Data and Settings