Recent Documents

Overview

Windows and macOS provide access to a list of recent documents opened by the application via JumpList or dock menu, respectively.

JumpList:

JumpList Recent Files

Application dock menu:

macOS Dock Menu

Example

Managing recent documents

docs/fiddles/features/recent-documents (27.0.1)Open in Fiddle

  • main.js
  • index.html
  1. const { app, BrowserWindow } = require('electron')
  2. const fs = require('node:fs')
  3. const path = require('node:path')
  4. function createWindow () {
  5. const win = new BrowserWindow({
  6. width: 800,
  7. height: 600
  8. })
  9. win.loadFile('index.html')
  10. }
  11. const fileName = 'recently-used.md'
  12. fs.writeFile(fileName, 'Lorem Ipsum', () => {
  13. app.addRecentDocument(path.join(__dirname, fileName))
  14. })
  15. app.whenReady().then(createWindow)
  16. app.on('window-all-closed', () => {
  17. app.clearRecentDocuments()
  18. if (process.platform !== 'darwin') {
  19. app.quit()
  20. }
  21. })
  22. app.on('activate', () => {
  23. if (BrowserWindow.getAllWindows().length === 0) {
  24. createWindow()
  25. }
  26. })
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Recent Documents</title>
  6. <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
  7. </head>
  8. <body>
  9. <h1>Recent Documents</h1>
  10. <p>
  11. Right click on the app icon to see recent documents.
  12. You should see `recently-used.md` added to the list of recent files
  13. </p>
  14. </body>
  15. </html>

Adding a recent document

To add a file to recent documents, use the app.addRecentDocument API.

After launching the Electron application, right click the application icon. In this guide, the item is a Markdown file located in the root of the project. You should see recently-used.md added to the list of recent files:

Recent document

Clearing the list of recent documents

To clear the list of recent documents, use the app.clearRecentDocuments API. In this guide, the list of documents is cleared once all windows have been closed.

Additional information

Windows Notes

To use this feature on Windows, your application has to be registered as a handler of the file type of the document, otherwise the file won’t appear in JumpList even after you have added it. You can find everything on registering your application in Application Registration.

When a user clicks a file from the JumpList, a new instance of your application will be started with the path of the file added as a command line argument.

macOS Notes

Add the Recent Documents list to the application menu

You can add menu items to access and clear recent documents by adding the following code snippet to your menu template:

  1. {
  2. "submenu":[
  3. {
  4. "label":"Open Recent",
  5. "role":"recentdocuments",
  6. "submenu":[
  7. {
  8. "label":"Clear Recent",
  9. "role":"clearrecentdocuments"
  10. }
  11. ]
  12. }
  13. ]
  14. }

Make sure the application menu is added after the ‘ready’ event and not before, or the menu item will be disabled:

  1. const { app, Menu } = require('electron')
  2. const template = [
  3. // Menu template here
  4. ]
  5. const menu = Menu.buildFromTemplate(template)
  6. app.whenReady().then(() => {
  7. Menu.setApplicationMenu(menu)
  8. })

macOS Recent Documents menu item

When a file is requested from the recent documents menu, the open-file event of app module will be emitted for it.