Last active
May 4, 2021 06:49
-
-
Save ngehlert/74d5a26990811eed59c635e49134d669 to your computer and use it in GitHub Desktop.
Show open dialog with security scoped bookmarks and access them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const settings = require('electron-settings'); | |
dialog | |
.showOpenDialog(mainWindow, { | |
properties: ['openDirectory'], | |
securityScopedBookmarks: true, | |
}) | |
.then(({ canceled, filePaths, bookmarks }) => { | |
// If the dialog was canceled or no directory/file selected we don't want to do anything | |
if (canceled || filePaths.length === 0) { | |
return; | |
} | |
if (bookmarks && bookmarks.length) { | |
// store the bookmark key | |
settings.set('security-scoped-bookmark', bookmarks[0]); | |
} | |
// do something with the file path, e.g. send to your application | |
const [directory] = filePaths; | |
mainWindow.webContents.send('new-directory', directory); | |
}) | |
/////////////////// | |
// access security scope bookmark | |
const { app } = require('electron'); | |
const settings = require('electron-settings'); | |
const securityBookmark = settings.get('security-scoped-bookmark'); | |
if (securityBookmark) { | |
app.startAccessingSecurityScopedResource(securityBookmark); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment