@skpm/dialog v0.4.2
Sketch dialogs
A Sketch module for displaying native system dialogs for opening and saving files, alerting, etc. The API is the mimicking the Electron dialog API.
Installation
To use this module in your Sketch plugin you need a bundler utility like skpm and add it as a dependency:
npm i -S @skpm/dialogUsage
An example of showing a dialog to select multiple files and directories:
import dialog from '@skpm/dialog'
console.log(
dialog.showOpenDialogSync({
properties: ['openFile', 'openDirectory', 'multiSelections']
})
)Methods
The dialog module has the following methods:
showOpenDialogshowOpenDialogSyncshowSaveDialogshowSaveDialogSyncshowMessageBoxshowMessageBoxSync
dialog.showOpenDialogSync([document, ]options)
documentDocument (optional)optionsObjecttitleString (optional)defaultPathString (optional)buttonLabelString (optional) - Custom label for the confirmation button, when left empty the default label will be used.filtersFileFilter[] (optional)propertiesString[] (optional) - Contains which features the dialog should use. The following values are supported:openFile- Allow files to be selected.openDirectory- Allow directories to be selected.multiSelections- Allow multiple paths to be selected.showHiddenFiles- Show hidden files in dialog.createDirectory- Allow creating new directories from dialog.noResolveAliases- Disable the automatic alias (symlink) path resolution. Selected aliases will now return the alias path instead of their target path.treatPackageAsDirectory- Treat packages, such as.appfolders, as a directory instead of a file.
messageString (optional) - Message to display above input boxes.
Returns String[], an array of file paths chosen by the user, if the callback
is provided it returns an empty array.
The document argument allows the dialog to attach itself to a Sketch document
window, making it a sheet.
The filters specifies an array of file types that can be displayed or selected
when you want to limit the user to a specific type. For example:
{
filters: [
{ name: 'Images', extensions: ['jpg', 'png', 'gif'] },
{ name: 'Movies', extensions: ['mkv', 'avi', 'mp4'] },
{ name: 'Custom File Type', extensions: ['as'] }
]
}The extensions array should contain extensions without wildcards or dots (e.g.
'png' is good but '.png' and '*.png' are bad).
dialog.showOpenDialog([document, ]options)
documentDocument (optional)optionsObjecttitleString (optional)defaultPathString (optional)buttonLabelString (optional) - Custom label for the confirmation button, when left empty the default label will be used.filtersFileFilter[] (optional)propertiesString[] (optional) - Contains which features the dialog should use. The following values are supported:openFile- Allow files to be selected.openDirectory- Allow directories to be selected.multiSelections- Allow multiple paths to be selected.showHiddenFiles- Show hidden files in dialog.createDirectory- Allow creating new directories from dialog.noResolveAliases- Disable the automatic alias (symlink) path resolution. Selected aliases will now return the alias path instead of their target path.treatPackageAsDirectory- Treat packages, such as.appfolders, as a directory instead of a file.
messageString (optional) - Message to display above input boxes.
Returns Promise<Object> - Resolve with an object containing the following:
canceledBoolean - whether or not the dialog was canceled.filePathsString[] - An array of file paths chosen by the user. If the dialog is cancelled this will be an empty array.
Returns String[], an array of file paths chosen by the user, if the callback
is provided it returns undefined.
The document argument allows the dialog to attach itself to a Sketch document
window, making it a sheet.
The filters specifies an array of file types that can be displayed or selected
when you want to limit the user to a specific type. For example:
{
filters: [
{ name: 'Images', extensions: ['jpg', 'png', 'gif'] },
{ name: 'Movies', extensions: ['mkv', 'avi', 'mp4'] },
{ name: 'Custom File Type', extensions: ['as'] }
]
}The extensions array should contain extensions without wildcards or dots (e.g.
'png' is good but '.png' and '*.png' are bad).
dialog.showSaveDialogSync([document, ]options)
documentDocument (optional)optionsObjecttitleString (optional)defaultPathString (optional) - Absolute directory path, absolute file path, or file name to use by default.buttonLabelString (optional) - Custom label for the confirmation button, when left empty the default label will be used.filtersFileFilter[] (optional)messageString (optional) - Message to display above text fields.nameFieldLabelString (optional) - Custom label for the text displayed in front of the filename text field.showsTagFieldBoolean (optional) - Show the tags input box, defaults totrue.
Returns String, the path of the file chosen by the user, if a callback is
provided it returns undefined.
The document argument allows the dialog to attach itself to a Sketch document
window, making it a sheet.
The filters specifies an array of file types that can be displayed, see
dialog.showOpenDialog for an example.
dialog.showSaveDialog([document, ]options)
documentDocument (optional)optionsObjecttitleString (optional)defaultPathString (optional) - Absolute directory path, absolute file path, or file name to use by default.buttonLabelString (optional) - Custom label for the confirmation button, when left empty the default label will be used.filtersFileFilter[] (optional)messageString (optional) - Message to display above text fields.nameFieldLabelString (optional) - Custom label for the text displayed in front of the filename text field.showsTagFieldBoolean (optional) - Show the tags input box, defaults totrue.
Returns Promise<Object> - Resolve with an object containing the following:
canceledBoolean - whether or not the dialog was canceled.filePathString (optional) - If the dialog is canceled, this will be undefined.
The document argument allows the dialog to attach itself to a Sketch document
window, making it a sheet.
The filters specifies an array of file types that can be displayed, see
dialog.showOpenDialog for an example.
Note: Using the asynchronous version is recommended to avoid issues when expanding and collapsing the dialog.
dialog.showMessageBox([document, ]options)
documentDocument (optional)optionsObjecttypeString (optional) - Can be"none","info","error","question"or"warning". Both"warning"and"error"display the same warning icon.buttonsString[] (optional) - Array of texts for buttons.defaultIdInteger (optional) - Index of the button in the buttons array which will be selected by default when the message box opens.titleString (optional) - Title of the message box, some platforms will not show it.messageString - Content of the message box.detailString (optional) - Extra information of the message.checkboxLabelString (optional) - If provided, the message box will include a checkbox with the given label.checkboxCheckedBoolean (optional) - Initial checked state of the checkbox.falseby default.iconString (optional) - path to the image (if you useskpm, you can justrequire('./path/to/my/icon.png'))
Returns Integer - the index of the clicked button.
Shows a message box, it will block the process until the message box is closed.
The document argument allows the dialog to attach itself to a Sketch document
window, making it a sheet.
dialog.showMessageBoxSync([document, ]options)
documentDocument (optional)optionsObjecttypeString (optional) - Can be"none","info","error","question"or"warning". Both"warning"and"error"display the same warning icon.buttonsString[] (optional) - Array of texts for buttons.defaultIdInteger (optional) - Index of the button in the buttons array which will be selected by default when the message box opens.titleString (optional) - Title of the message box, some platforms will not show it.messageString - Content of the message box.detailString (optional) - Extra information of the message.checkboxLabelString (optional) - If provided, the message box will include a checkbox with the given label.checkboxCheckedBoolean (optional) - Initial checked state of the checkbox.falseby default.iconString (optional) - path to the image (if you useskpm, you can justrequire('./path/to/my/icon.png'))
Returns Promise<Object> - resolves with a promise containing the following properties:
responseNumber - The index of the clicked button.checkboxCheckedBoolean - The checked state of the checkbox if checkboxLabel was set. Otherwise false.
Shows a message box, it will block the process until the message box is closed.
The document argument allows the dialog to attach itself to a Sketch document
window, making it a sheet.
Sheets
Dialogs are presented as sheets attached to a window if you provide a document
reference in the document parameter, or modals if no document is provided.