Skip to content

Instantly share code, notes, and snippets.

@jamiebuilds
Created June 16, 2023 19:20
Show Gist options
  • Save jamiebuilds/dbb1d7464e944a1b275df642cadd3aa1 to your computer and use it in GitHub Desktop.
Save jamiebuilds/dbb1d7464e944a1b275df642cadd3aa1 to your computer and use it in GitHub Desktop.
dialog.showSaveDialog force normalized extension
let { app, dialog } = require('electron')
let path = require("path")
let pathCompleteExtname = require("path-complete-extname")
app.whenReady().then(async () => {
for (let defaultPath of ['change-me.txt', 'change-me.tar.gz', 'change-me.jpeg']) {
let defaultExtname = pathCompleteExtname(defaultPath)
let defaultBasename = path.basename(defaultPath, defaultExtname)
let { canceled, filePath: selectedPath } = await dialog.showSaveDialog({
defaultPath: defaultBasename,
})
if (canceled) return
let selectedDirname = path.dirname(selectedPath)
let selectedExtname = pathCompleteExtname(selectedPath)
let selectedBaseName = path.basename(selectedPath, selectedExtname)
let finalPath = path.format({
dir: selectedDirname,
name: selectedBaseName,
ext: defaultExtname,
})
await dialog.showMessageBox({
message: `Selected file path (normalized - dont let user change extension): ${finalPath}`
})
}
})
{
"name": "wacky-notebook-innovate-xddrf",
"productName": "wacky-notebook-innovate-xddrf",
"description": "My Electron application description",
"keywords": [],
"main": "./main.js",
"version": "1.0.0",
"author": "Jamie",
"scripts": {
"start": "electron ."
},
"dependencies": {
"path-complete-extname": "1.0.0"
},
"devDependencies": {
"electron": "25.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment