Improved file selection (macos).

This commit is contained in:
Nuno Cruces 2020-01-09 18:37:03 +00:00
parent c212fdeab7
commit 242a3aa975
2 changed files with 18 additions and 11 deletions

View file

@ -9,8 +9,8 @@ as well as a *“port”* of the `zenity` command to both Windows and macOS base
Lots of things are missing. Lots of things are missing.
For now, these are the only implemented dialogs: For now, these are the only implemented dialogs:
* message (error, info, question, warning); and * message (error, info, question, warning)
* file selection. * file selection
Behavior on Windows, macOS and other UNIXes might differ sliglty. Behavior on Windows, macOS and other UNIXes might differ sliglty.
Some of that is intended (reflecting platform differences), Some of that is intended (reflecting platform differences),
@ -28,11 +28,13 @@ Why reinvent this particular wheel?
* no main loop (or other threading requirements) * no main loop (or other threading requirements)
* no initialization * no initialization
* on Windows: * on Windows:
* Explorer shell not required (works in Server Core) * no additional dependencies
* no other dependencies * Explorer shell not required
* works in Server Core
* Unicode support * Unicode support
* on macOS: * on macOS:
* only dependency is `osascript`, JXA * only dependency is `osascript` (with [JXA](https://developer.apple.com/library/archive/releasenotes/InterapplicationCommunication/RN-JavaScriptForAutomation/Articles/Introduction.html))
* JavaScript is easier to template with `html/template`
* on other UNIXes: * on other UNIXes:
* wraps either one of `matedialog`, `qarma`, `zenity` (in that order of preference) * wraps either one of `matedialog`, `qarma`, `zenity`
* no command line support * in that order of preference, most to least specific

View file

@ -10,11 +10,12 @@ import (
func SelectFile(options ...Option) (string, error) { func SelectFile(options ...Option) (string, error) {
opts := optsParse(options) opts := optsParse(options)
dir, _ := splitDirAndName(opts.filename)
out, err := osa.Run("file", osa.File{ out, err := osa.Run("file", osa.File{
Operation: "chooseFile", Operation: "chooseFile",
Prompt: opts.title, Prompt: opts.title,
Location: opts.filename,
Type: appleFilters(opts.filters), Type: appleFilters(opts.filters),
Location: dir,
}) })
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return "", nil return "", nil
@ -30,13 +31,14 @@ func SelectFile(options ...Option) (string, error) {
func SelectFileMutiple(options ...Option) ([]string, error) { func SelectFileMutiple(options ...Option) ([]string, error) {
opts := optsParse(options) opts := optsParse(options)
dir, _ := splitDirAndName(opts.filename)
out, err := osa.Run("file", osa.File{ out, err := osa.Run("file", osa.File{
Operation: "chooseFile", Operation: "chooseFile",
Multiple: true, Multiple: true,
Prompt: opts.title, Prompt: opts.title,
Location: opts.filename,
Separator: cmd.Separator, Separator: cmd.Separator,
Type: appleFilters(opts.filters), Type: appleFilters(opts.filters),
Location: dir,
}) })
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return nil, nil return nil, nil
@ -55,10 +57,12 @@ func SelectFileMutiple(options ...Option) ([]string, error) {
func SelectFileSave(options ...Option) (string, error) { func SelectFileSave(options ...Option) (string, error) {
opts := optsParse(options) opts := optsParse(options)
dir, name := splitDirAndName(opts.filename)
out, err := osa.Run("file", osa.File{ out, err := osa.Run("file", osa.File{
Operation: "chooseFileName", Operation: "chooseFileName",
Prompt: opts.title, Prompt: opts.title,
Location: opts.filename, Location: dir,
Name: name,
}) })
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return "", nil return "", nil
@ -74,10 +78,11 @@ func SelectFileSave(options ...Option) (string, error) {
func SelectDirectory(options ...Option) (string, error) { func SelectDirectory(options ...Option) (string, error) {
opts := optsParse(options) opts := optsParse(options)
dir, _ := splitDirAndName(opts.filename)
out, err := osa.Run("file", osa.File{ out, err := osa.Run("file", osa.File{
Operation: "chooseFolder", Operation: "chooseFolder",
Prompt: opts.title, Prompt: opts.title,
Location: opts.filename, Location: dir,
}) })
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 { if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return "", nil return "", nil