skpm/dialog

Save dialog: Giving filename but not selecting any file or folder results in error

wousser opened this issue · 1 comments

From documentation:

defaultPath String (optional) - Absolute directory path, absolute file path, or file name to use by default.

When opening the dialog with an absolute directory path or file path, the dialog opens with that folder. The file's name will be the file path's name. If the user changes the name within the dialog, the callback's filepath is the absolute file path + user's given filename. Instead of absolute directory path + user's given filename.

code:

var filePath = dialog.showSaveDialog({
    defaultPath: document.path
  })

Actual Output:
document.path: '/Users/username/Projects/project/file.sketch'
filePath: /Users/username/Projects/project/file.sketch/name given by user.txt

Expected Output:
document.path: '/Users/username/Projects/project/file.sketch'
filePath: /Users/username/Projects/project/name given by user.txt

User does not select anything, only changes the filename.
image

that's because the default path is '/Users/username/Projects/project/file.sketch'. Try:

const path = require('path')

var filePath = dialog.showSaveDialog({
    defaultPath: path.dirname(document.path)
  })