2018-02-19 23:09:58 +00:00
|
|
|
# Recent Documents (Windows & macOS)
|
|
|
|
|
2020-10-20 01:26:24 +00:00
|
|
|
## Overview
|
|
|
|
|
2018-05-07 15:46:14 +00:00
|
|
|
Windows and macOS provide access to a list of recent documents opened by
|
2018-02-19 23:09:58 +00:00
|
|
|
the application via JumpList or dock menu, respectively.
|
|
|
|
|
|
|
|
__JumpList:__
|
|
|
|
|
|
|
|
![JumpList Recent Files][jumplist-image]
|
|
|
|
|
|
|
|
__Application dock menu:__
|
|
|
|
|
|
|
|
![macOS Dock Menu][dock-menu-image]
|
|
|
|
|
2020-10-20 01:26:24 +00:00
|
|
|
## Example
|
|
|
|
|
2021-06-07 01:43:24 +00:00
|
|
|
### Managing recent documents
|
2018-02-19 23:09:58 +00:00
|
|
|
|
2020-11-30 07:48:39 +00:00
|
|
|
```javascript fiddle='docs/fiddles/features/recent-documents'
|
2021-06-07 01:43:24 +00:00
|
|
|
const { app, BrowserWindow } = require('electron')
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
|
|
|
|
function createWindow () {
|
|
|
|
const win = new BrowserWindow({
|
|
|
|
width: 800,
|
|
|
|
height: 600
|
|
|
|
})
|
|
|
|
|
|
|
|
win.loadFile('index.html')
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileName = 'recently-used.md'
|
|
|
|
fs.writeFile(fileName, 'Lorem Ipsum', () => {
|
|
|
|
app.addRecentDocument(path.join(__dirname, fileName))
|
|
|
|
})
|
|
|
|
|
|
|
|
app.whenReady().then(createWindow)
|
|
|
|
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
app.clearRecentDocuments()
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
|
|
createWindow()
|
|
|
|
}
|
|
|
|
})
|
2018-02-19 23:09:58 +00:00
|
|
|
```
|
|
|
|
|
2021-06-07 01:43:24 +00:00
|
|
|
#### Adding a recent document
|
2020-10-20 01:26:24 +00:00
|
|
|
|
2021-06-07 01:43:24 +00:00
|
|
|
To add a file to recent documents, use the
|
|
|
|
[app.addRecentDocument][addrecentdocument] API.
|
2020-10-20 01:26:24 +00:00
|
|
|
|
2021-06-07 01:43:24 +00:00
|
|
|
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:
|
2020-10-20 01:26:24 +00:00
|
|
|
|
2021-06-07 01:43:24 +00:00
|
|
|
![Recent document](../images/recent-documents.png)
|
2018-02-19 23:09:58 +00:00
|
|
|
|
2021-06-07 01:43:24 +00:00
|
|
|
#### Clearing the list of recent documents
|
2020-10-20 01:26:24 +00:00
|
|
|
|
2021-06-07 01:43:24 +00:00
|
|
|
To clear the list of recent documents, use the
|
|
|
|
[app.clearRecentDocuments][clearrecentdocuments] API.
|
|
|
|
In this guide, the list of documents is cleared once all windows have been
|
|
|
|
closed.
|
2018-02-19 23:09:58 +00:00
|
|
|
|
2020-10-20 01:26:24 +00:00
|
|
|
## Additional information
|
2018-02-19 23:09:58 +00:00
|
|
|
|
2020-10-20 01:26:24 +00:00
|
|
|
### 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
|
2018-02-19 23:09:58 +00:00
|
|
|
on registering your application in [Application Registration][app-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.
|
|
|
|
|
2020-10-20 01:26:24 +00:00
|
|
|
### macOS Notes
|
2018-02-19 23:09:58 +00:00
|
|
|
|
2020-10-20 01:26:24 +00:00
|
|
|
#### Add the Recent Documents list to the application menu
|
2020-07-23 02:22:01 +00:00
|
|
|
|
2020-10-20 01:26:24 +00:00
|
|
|
You can add menu items to access and clear recent documents by adding the
|
|
|
|
following code snippet to your menu template:
|
2020-07-23 02:22:01 +00:00
|
|
|
|
2020-07-23 19:27:00 +00:00
|
|
|
```json
|
2020-07-23 02:22:01 +00:00
|
|
|
{
|
2020-07-23 19:27:00 +00:00
|
|
|
"submenu":[
|
|
|
|
{
|
|
|
|
"label":"Open Recent",
|
|
|
|
"role":"recentdocuments",
|
|
|
|
"submenu":[
|
|
|
|
{
|
|
|
|
"label":"Clear Recent",
|
|
|
|
"role":"clearrecentdocuments"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2020-07-23 02:22:01 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-05-06 08:19:06 +00:00
|
|
|
Make sure the application menu is added after the [`'ready'`](../api/app.md#event-ready)
|
|
|
|
event and not before, or the menu item will be disabled:
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
const { app, Menu } = require('electron')
|
|
|
|
|
|
|
|
const template = [
|
|
|
|
// Menu template here
|
|
|
|
]
|
|
|
|
const menu = Menu.buildFromTemplate(template)
|
|
|
|
|
|
|
|
app.whenReady().then(() => {
|
|
|
|
Menu.setApplicationMenu(menu)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
2020-10-20 01:26:24 +00:00
|
|
|
![macOS Recent Documents menu item][menu-item-image]
|
|
|
|
|
2018-02-19 23:09:58 +00:00
|
|
|
When a file is requested from the recent documents menu, the `open-file` event
|
|
|
|
of `app` module will be emitted for it.
|
|
|
|
|
|
|
|
[jumplist-image]: https://cloud.githubusercontent.com/assets/2289/23446924/11a27b98-fdfc-11e6-8485-cc3b1e86b80a.png
|
|
|
|
[dock-menu-image]: https://cloud.githubusercontent.com/assets/639601/5069610/2aa80758-6e97-11e4-8cfb-c1a414a10774.png
|
2018-02-20 00:20:35 +00:00
|
|
|
[addrecentdocument]: ../api/app.md#appaddrecentdocumentpath-macos-windows
|
|
|
|
[clearrecentdocuments]: ../api/app.md#appclearrecentdocuments-macos-windows
|
2018-03-13 23:07:36 +00:00
|
|
|
[app-registration]: https://msdn.microsoft.com/en-us/library/cc144104(VS.85).aspx
|
2020-07-23 02:22:01 +00:00
|
|
|
[menu-item-image]: https://user-images.githubusercontent.com/3168941/33003655-ea601c3a-cd70-11e7-97fa-7c062149cfb1.png
|