UNPKG

@ayonli/jsext

Version:

A JavaScript extension package for building strong and modern applications.

119 lines (115 loc) 3.97 kB
'use strict'; var string = require('../../../string.js'); var cli = require('../../../cli.js'); var filetype = require('../../../filetype.js'); var dialog_cli_util = require('../util.js'); function htmlAcceptToAppleType(accept) { return accept.split(/\s*,\s*/).map(filetype.getUTI).filter(Boolean).map(t => `"${t}"`).join(", "); } function createAppleScript(mode, title = "", options = {}) { const { type, forSave, defaultName } = options; if (mode === "file") { if (forSave) { return string.dedent ` tell application (path to frontmost application as text) set myFile to choose file name${title ? ` with prompt "${dialog_cli_util.escape(title)}"` : ""}\ ${defaultName ? ` default name "${dialog_cli_util.escape(defaultName)}"` : ""} POSIX path of myFile end `; } else { const _type = type ? htmlAcceptToAppleType(type) : ""; return string.dedent ` tell application (path to frontmost application as text) set myFile to choose file${title ? ` with prompt "${dialog_cli_util.escape(title)}"` : ""}\ ${_type ? ` of type {${_type}}` : ""} invisibles false POSIX path of myFile end `; } } else if (mode === "files") { const _type = type ? htmlAcceptToAppleType(type) : ""; return string.dedent ` tell application (path to frontmost application as text) set myFiles to choose file${title ? ` with prompt "${dialog_cli_util.escape(title)}"` : ""}\ ${_type ? ` of type {${_type}}` : ""} invisibles false\ multiple selections allowed true set theList to {} repeat with aItem in myFiles set end of theList to POSIX path of aItem end repeat set my text item delimiters to "\\n" return theList as text end `; } else { return string.dedent ` tell application (path to frontmost application as text) set myFolder to choose folder${title ? ` with prompt "${dialog_cli_util.escape(title)}"` : ""}\ invisibles false POSIX path of myFolder end `; } } async function macPickFile(title = "", options = {}) { const { code, stdout, stderr } = await cli.run("osascript", [ "-e", createAppleScript("file", title, options) ]); if (!code) { const path = stdout.trim(); return path || null; } else { if (stderr.includes("User canceled")) { return null; } else { throw new Error(stderr.trim()); } } } async function macPickFiles(title = "", type = "") { const { code, stdout, stderr } = await cli.run("osascript", [ "-e", createAppleScript("files", title, { type }) ]); if (!code) { const output = stdout.trim(); return output ? string.lines(stdout.trim()) : []; } else { if (stderr.includes("User canceled")) { return []; } else { throw new Error(stderr.trim()); } } } async function macPickFolder(title = "") { const { code, stdout, stderr } = await cli.run("osascript", [ "-e", createAppleScript("folder", title) ]); if (!code) { const dir = stdout.trim(); return dir || null; } else { if (stderr.includes("User canceled")) { return null; } else { throw new Error(stderr.trim()); } } } exports.macPickFile = macPickFile; exports.macPickFiles = macPickFiles; exports.macPickFolder = macPickFolder; //# sourceMappingURL=mac.js.map