Skip to content

Instantly share code, notes, and snippets.

@ngehlert
Last active May 4, 2021 06:49
Show Gist options
  • Save ngehlert/74d5a26990811eed59c635e49134d669 to your computer and use it in GitHub Desktop.
Save ngehlert/74d5a26990811eed59c635e49134d669 to your computer and use it in GitHub Desktop.
Show open dialog with security scoped bookmarks and access them
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