Created
June 16, 2023 19:20
-
-
Save jamiebuilds/dbb1d7464e944a1b275df642cadd3aa1 to your computer and use it in GitHub Desktop.
dialog.showSaveDialog force normalized extension
This file contains 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
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}` | |
}) | |
} | |
}) |
This file contains 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
{ | |
"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