メインコンテンツへ飛ぶ

最近使用したドキュメント

概要

Windows と macOS は、それぞれジャンプリストまたは Dock メニューを介して、アプリケーションによって開かれた最近の書類のリストへのアクセスを提供します。

ジャンプリスト:

ジャンプリストの最近使った書類

アプリケーションの Dock メニュー:

macOS の Dock メニュー

サンプル

最近開いた書類の管理

const { app, BrowserWindow } = require('electron/main')
const fs = require('node:fs')
const path = require('node: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()
}
})

最近開いた書類の追加

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

Electron アプリケーションの起動後、アプリケーションのアイコンを右クリックしてみましょう。 このガイドでは、そのアイテムはプロジェクトのルートにある Markdown ファイルとなっています。 最近開いたファイルのリストに recently-used.md が追加されているはずです。

最近使った書類

最近開いた書類の消去

To clear the list of recent documents, use the app.clearRecentDocuments API. このガイドでは、すべてのウインドウを閉じた時点でその書類のリストは消去されます。

Accessing the list of recent documents

To access the list of recent documents, use the app.getRecentDocuments API.

さらなる情報

Windows での注意

Windows でこの機能を使用する際にアプリケーションが書類のファイルタイプのハンドラとして登録されていない場合、ファイルを追加してもジャンプリストに表示されません。 You can find everything on registering your application in Application Registration.

ユーザーがジャンプリストからファイルをクリックすると、アプリケーションの新しいインスタンスが、ファイルのパスがコマンドライン引数として追加されて起動されます。

macOS での注意

アプリケーションメニューに最近開いた書類を追加する

以下のコードスニペットをメニューテンプレートに追加することで、最近開いた書類にアクセスしたり消去したりするメニュー項目を追加できます。

{
"submenu":[
{
"label":"Open Recent",
"role":"recentdocuments",
"submenu":[
{
"label":"Clear Recent",
"role":"clearrecentdocuments"
}
]
}
]
}

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

const { app, Menu } = require('electron')

const template = [
// メニューテンプレートをこちらに
]
const menu = Menu.buildFromTemplate(template)

app.whenReady().then(() => {
Menu.setApplicationMenu(menu)
})

macOS 最近使った書類のメニューアイテム

最近使った書類メニューからファイルが要求されると、それに対して app モジュールの open-file イベントが発生します。