Representing Files in a BrowserWindow (macOS)

Overview

On macOS, you can set a represented file for any window in your application. The represented file’s icon will be shown in the title bar, and when users Command-Click or Control-Click, a popup with a path to the file will be shown.

Represented File

NOTE: The screenshot above is an example where this feature is used to indicate the currently opened file in the Atom text editor.

You can also set the edited state for a window so that the file icon can indicate whether the document in this window has been modified.

To set the represented file of window, you can use the BrowserWindow.setRepresentedFilename and BrowserWindow.setDocumentEdited APIs.

Example

  1. const { app, BrowserWindow } = require('electron')
  2. const os = require('os');
  3. const createWindow = () => {
  4. const win = new BrowserWindow({
  5. width: 800,
  6. height: 600
  7. })
  8. }
  9. app.whenReady().then(() => {
  10. const win = new BrowserWindow()
  11. win.setRepresentedFilename(os.homedir())
  12. win.setDocumentEdited(true)
  13. })
  14. app.on('window-all-closed', () => {
  15. if (process.platform !== 'darwin') {
  16. app.quit()
  17. }
  18. })
  19. app.on('activate', () => {
  20. if (BrowserWindow.getAllWindows().length === 0) {
  21. createWindow()
  22. }
  23. })

After launching the Electron application, click on the title with Command or Control key pressed. You should see a popup with the represented file at the top. In this guide, this is the current user’s home directory:

Represented file