From ce225587dbf00e60c3953ddb9ff20cef03a7e5a2 Mon Sep 17 00:00:00 2001 From: Clark Winters Date: Thu, 20 Oct 2022 13:46:11 -0500 Subject: [PATCH 1/2] add applescript directive to indicate the provided icon path is in POSIX format --- dialog.go | 156 +++++++++++++++++++++++++++--------------------------- 1 file changed, 79 insertions(+), 77 deletions(-) diff --git a/dialog.go b/dialog.go index aabc869..8162b38 100644 --- a/dialog.go +++ b/dialog.go @@ -1,114 +1,116 @@ /* ** Mack: Alert ** Create a desktop dialog box -*/ + */ package mack import ( - "strconv" - "strings" + "strconv" + "strings" ) // Dialog triggers a desktop dialog box. Either an error is returned, or the string output from the user interaction. -// mack.Dialog("Dialog text") // Display a dialog box -// mack.Dialog("Dialog text", "My Title") // Display a dialog box with the title "My Title" -// mack.Dialog("Dialog text", "My Title", "default text") // Display a dialog box with "default text" in the input field -// mack.Dialog("Dialog text", "My Title", "default text", "5") // Display a dialog box that will disappear after 5 seconds -// mack.Dialog("Dialog text", "", "", "10") // Display a dialog box that will disappear after 10 seconds -// response, err := mack.Dialog("My dialog") // Capture the Response for the dialog box +// +// mack.Dialog("Dialog text") // Display a dialog box +// mack.Dialog("Dialog text", "My Title") // Display a dialog box with the title "My Title" +// mack.Dialog("Dialog text", "My Title", "default text") // Display a dialog box with "default text" in the input field +// mack.Dialog("Dialog text", "My Title", "default text", "5") // Display a dialog box that will disappear after 5 seconds +// mack.Dialog("Dialog text", "", "", "10") // Display a dialog box that will disappear after 10 seconds +// response, err := mack.Dialog("My dialog") // Capture the Response for the dialog box // // Parameters: // -// text string // Required - The content of the dialog box -// title string // Optional - The title of the dialog box, displayed in emphasis -// answer string // Optional - The default text in the input field -// duration string // Optional - The number of seconds to wait for a user response +// text string // Required - The content of the dialog box +// title string // Optional - The title of the dialog box, displayed in emphasis +// answer string // Optional - The default text in the input field +// duration string // Optional - The number of seconds to wait for a user response func Dialog(text string, options ...string) (Response, error) { - return runWithButtons(buildDialog(text, options)) + return runWithButtons(buildDialog(text, options)) } // Parse the dialog options and build the command func buildDialog(text string, options []string) string { - text = wrapInQuotes(text) + text = wrapInQuotes(text) - var title, answer, duration string - if len(options) > 0 && options[0] != "" { - title = "with title " + wrapInQuotes(options[0]) - } - if len(options) > 1 && options[1] != "" { - answer = "default answer " + wrapInQuotes(options[1]) - } - if len(options) > 2 && options[2] != "" { - duration = "giving up after " + options[2] - } + var title, answer, duration string + if len(options) > 0 && options[0] != "" { + title = "with title " + wrapInQuotes(options[0]) + } + if len(options) > 1 && options[1] != "" { + answer = "default answer " + wrapInQuotes(options[1]) + } + if len(options) > 2 && options[2] != "" { + duration = "giving up after " + options[2] + } - return build("display dialog", text, title, answer, duration) + return build("display dialog", text, title, answer, duration) } // DialogBox triggers a desktop dialog box with the option for custom buttons. Either an error is returned, or the string output from the user interaction. -// dialog := mack.DialogOptions{ -// Text: "Dialog text", // Required -// Title: "Dialog title", // Optional -// Answer: "Default answer", // Optional -// Duration: 5, // Optional -// HiddenAnswer: true, // Optional - If true, turns the input text to bullets -// Icon: "stop", // Optional - "stop", "note", "caution" or location of .icns file -// Buttons: "Yes, No, Don't Know", // Optional - Comma separated list, max of 3 -// DefaultButton: "Don't Know", // Optional - Ignored if no ButtonList -// } -// response, err := mack.DialogBox(dialog) // Display a dialog with the DialogBox settings, returns an error and Response +// +// dialog := mack.DialogOptions{ +// Text: "Dialog text", // Required +// Title: "Dialog title", // Optional +// Answer: "Default answer", // Optional +// Duration: 5, // Optional +// HiddenAnswer: true, // Optional - If true, turns the input text to bullets +// Icon: "stop", // Optional - "stop", "note", "caution" or location of .icns file +// Buttons: "Yes, No, Don't Know", // Optional - Comma separated list, max of 3 +// DefaultButton: "Don't Know", // Optional - Ignored if no ButtonList +// } +// response, err := mack.DialogBox(dialog) // Display a dialog with the DialogBox settings, returns an error and Response func DialogBox(dialog DialogOptions) (Response, error) { - return runWithButtons(buildDialogBox(dialog)) + return runWithButtons(buildDialogBox(dialog)) } // Parse the DialogBox options and build the command func buildDialogBox(dialog DialogOptions) string { - var title, answer, hiddenAnswer, icon, duration, buttons, defaultButton string - text := wrapInQuotes(dialog.Text) + var title, answer, hiddenAnswer, icon, duration, buttons, defaultButton string + text := wrapInQuotes(dialog.Text) - if dialog.Title != "" { - title = "with title " + wrapInQuotes(dialog.Title) - } - if dialog.Answer != "" { - answer = "default answer " + wrapInQuotes(dialog.Answer) - } - if dialog.HiddenAnswer { - hiddenAnswer = "with hidden answer" - } - if dialog.Icon != "" { - if strings.Index(dialog.Icon, ".icns") > 0 { - // found a filepath to an icon - icon = "with icon " + wrapInQuotes(dialog.Icon) - } else { - // using a system icon - icon = "with icon " + dialog.Icon - } - } - if dialog.Duration > 0 { - duration = "giving up after " + strconv.Itoa(dialog.Duration) - } - if dialog.Buttons != "" { - buttons = makeButtonList(dialog.Buttons) + if dialog.Title != "" { + title = "with title " + wrapInQuotes(dialog.Title) + } + if dialog.Answer != "" { + answer = "default answer " + wrapInQuotes(dialog.Answer) + } + if dialog.HiddenAnswer { + hiddenAnswer = "with hidden answer" + } + if dialog.Icon != "" { + if strings.Index(dialog.Icon, ".icns") > 0 { + // found a filepath to an icon + icon = "with icon POSIX file " + wrapInQuotes(dialog.Icon) + } else { + // using a system icon + icon = "with icon " + dialog.Icon + } + } + if dialog.Duration > 0 { + duration = "giving up after " + strconv.Itoa(dialog.Duration) + } + if dialog.Buttons != "" { + buttons = makeButtonList(dialog.Buttons) - if dialog.DefaultButton != "" { - defaultButton = "default button " + wrapInQuotes(dialog.DefaultButton) - } - } + if dialog.DefaultButton != "" { + defaultButton = "default button " + wrapInQuotes(dialog.DefaultButton) + } + } - return build("display dialog", text, title, answer, hiddenAnswer, icon, duration, buttons, defaultButton) + return build("display dialog", text, title, answer, hiddenAnswer, icon, duration, buttons, defaultButton) } // DialogOptions are used to generate a DialogBox type DialogOptions struct { - Text string // The content of the dialog box - Title string // The title of the dialog box, displayed in emphasis - Answer string // The default text in the input field - HiddenAnswer bool // If true, converts the answer text to bullets (like a password field) - Icon string // The path to a .icns file, or one of the following: "stop", "note", "caution" - Duration int // The number of seconds to wait for a user response + Text string // The content of the dialog box + Title string // The title of the dialog box, displayed in emphasis + Answer string // The default text in the input field + HiddenAnswer bool // If true, converts the answer text to bullets (like a password field) + Icon string // The path to a .icns file, or one of the following: "stop", "note", "caution" + Duration int // The number of seconds to wait for a user response - // Buttons - Buttons string // The list of up to 3 buttons. Must be commas separated, ex. "Yes, No, Don't Know" - DefaultButton string // The default selected button from the button list, ex. "Don't Know" + // Buttons + Buttons string // The list of up to 3 buttons. Must be commas separated, ex. "Yes, No, Don't Know" + DefaultButton string // The default selected button from the button list, ex. "Don't Know" } From ce7541f04b38b87bee371e045b675d99d119cc0d Mon Sep 17 00:00:00 2001 From: Clark Winters Date: Thu, 20 Oct 2022 14:23:54 -0500 Subject: [PATCH 2/2] update test for POSIX path support --- dialog_test.go | 260 ++++++++++++++++++++++++------------------------- 1 file changed, 130 insertions(+), 130 deletions(-) diff --git a/dialog_test.go b/dialog_test.go index 533d6d2..9eb4c11 100644 --- a/dialog_test.go +++ b/dialog_test.go @@ -1,146 +1,146 @@ /* ** Mack: Alert Test ** Test desktop dialog boxes -*/ + */ package mack import ( - "testing" + "testing" ) func TestBuildDialog(t *testing.T) { - stringAssertTests := []StringAssert{ - StringAssert{ - actual: buildDialog("text", []string{}), - expected: "display dialog \"text\"", - }, - StringAssert{ - actual: buildDialog("text", []string{"title"}), - expected: "display dialog \"text\" with title \"title\"", - }, - StringAssert{ - actual: buildDialog("text", []string{"title", "answer"}), - expected: "display dialog \"text\" with title \"title\" default answer \"answer\"", - }, - StringAssert{ - actual: buildDialog("text", []string{"title", "answer", "5"}), - expected: "display dialog \"text\" with title \"title\" default answer \"answer\" giving up after 5", - }, - StringAssert{ - actual: buildDialog("text", []string{"", "", "5"}), - expected: "display dialog \"text\" giving up after 5", - }, - } + stringAssertTests := []StringAssert{ + StringAssert{ + actual: buildDialog("text", []string{}), + expected: "display dialog \"text\"", + }, + StringAssert{ + actual: buildDialog("text", []string{"title"}), + expected: "display dialog \"text\" with title \"title\"", + }, + StringAssert{ + actual: buildDialog("text", []string{"title", "answer"}), + expected: "display dialog \"text\" with title \"title\" default answer \"answer\"", + }, + StringAssert{ + actual: buildDialog("text", []string{"title", "answer", "5"}), + expected: "display dialog \"text\" with title \"title\" default answer \"answer\" giving up after 5", + }, + StringAssert{ + actual: buildDialog("text", []string{"", "", "5"}), + expected: "display dialog \"text\" giving up after 5", + }, + } - runStringAssertTests("buildDialog", stringAssertTests, t) + runStringAssertTests("buildDialog", stringAssertTests, t) } func TestBuildDialogBox(t *testing.T) { - stringAssertTests := []StringAssert{ - StringAssert{ - actual: buildDialogBox(DialogOptions{ - Text: "text", - }), - expected: "display dialog \"text\"", - }, - StringAssert{ - actual: buildDialogBox(DialogOptions{ - Text: "text", - Title: "title", - }), - expected: "display dialog \"text\" with title \"title\"", - }, - StringAssert{ - actual: buildDialogBox(DialogOptions{ - Text: "text", - Title: "title", - Answer: "answer", - }), - expected: "display dialog \"text\" with title \"title\" default answer \"answer\"", - }, - StringAssert{ - actual: buildDialogBox(DialogOptions{ - Text: "text", - Title: "title", - Answer: "answer", - HiddenAnswer: true, - }), - expected: "display dialog \"text\" with title \"title\" default answer \"answer\" with hidden answer", - }, - StringAssert{ - actual: buildDialogBox(DialogOptions{ - Text: "text", - Title: "title", - Answer: "answer", - HiddenAnswer: false, - Icon: "my-icon.icns", - }), - expected: "display dialog \"text\" with title \"title\" default answer \"answer\" with icon \"my-icon.icns\"", - }, - StringAssert{ - actual: buildDialogBox(DialogOptions{ - Text: "text", - Title: "title", - Answer: "answer", - HiddenAnswer: false, - Icon: "0", - }), - expected: "display dialog \"text\" with title \"title\" default answer \"answer\" with icon 0", - }, - StringAssert{ - actual: buildDialogBox(DialogOptions{ - Text: "text", - Title: "title", - Answer: "answer", - HiddenAnswer: false, - Icon: "0", - Duration: 5, - }), - expected: "display dialog \"text\" with title \"title\" default answer \"answer\" with icon 0 giving up after 5", - }, - StringAssert{ - actual: buildDialogBox(DialogOptions{ - Text: "text", - Title: "title", - Answer: "answer", - HiddenAnswer: false, - Icon: "0", - Duration: 5, - Buttons: "Yes, No, Don't Know", - }), - expected: "display dialog \"text\" with title \"title\" default answer \"answer\" with icon 0 giving up after 5 " + - "buttons {\"Yes\",\"No\",\"Don't Know\"}", - }, - StringAssert{ - actual: buildDialogBox(DialogOptions{ - Text: "text", - Title: "title", - Answer: "answer", - HiddenAnswer: false, - Icon: "0", - Duration: 5, - Buttons: "Yes, No, Don't Know", - DefaultButton: "Don't Know", - }), - expected: "display dialog \"text\" with title \"title\" default answer \"answer\" with icon 0 giving up after 5 " + - "buttons {\"Yes\",\"No\",\"Don't Know\"} default button \"Don't Know\"", - }, - StringAssert{ - actual: buildDialogBox(DialogOptions{ - Text: "text", - Title: "title", - Answer: "answer", - HiddenAnswer: false, - Icon: "0", - Duration: 5, - Buttons: "Yes, No, Don't Know, One Too Many", - DefaultButton: "Don't Know", - }), - expected: "display dialog \"text\" with title \"title\" default answer \"answer\" with icon 0 giving up after 5 " + - "buttons {\"Yes\",\"No\",\"Don't Know\"} default button \"Don't Know\"", - }, - } + stringAssertTests := []StringAssert{ + StringAssert{ + actual: buildDialogBox(DialogOptions{ + Text: "text", + }), + expected: "display dialog \"text\"", + }, + StringAssert{ + actual: buildDialogBox(DialogOptions{ + Text: "text", + Title: "title", + }), + expected: "display dialog \"text\" with title \"title\"", + }, + StringAssert{ + actual: buildDialogBox(DialogOptions{ + Text: "text", + Title: "title", + Answer: "answer", + }), + expected: "display dialog \"text\" with title \"title\" default answer \"answer\"", + }, + StringAssert{ + actual: buildDialogBox(DialogOptions{ + Text: "text", + Title: "title", + Answer: "answer", + HiddenAnswer: true, + }), + expected: "display dialog \"text\" with title \"title\" default answer \"answer\" with hidden answer", + }, + StringAssert{ + actual: buildDialogBox(DialogOptions{ + Text: "text", + Title: "title", + Answer: "answer", + HiddenAnswer: false, + Icon: "my-icon.icns", + }), + expected: "display dialog \"text\" with title \"title\" default answer \"answer\" with icon POSIX file \"my-icon.icns\"", + }, + StringAssert{ + actual: buildDialogBox(DialogOptions{ + Text: "text", + Title: "title", + Answer: "answer", + HiddenAnswer: false, + Icon: "0", + }), + expected: "display dialog \"text\" with title \"title\" default answer \"answer\" with icon 0", + }, + StringAssert{ + actual: buildDialogBox(DialogOptions{ + Text: "text", + Title: "title", + Answer: "answer", + HiddenAnswer: false, + Icon: "0", + Duration: 5, + }), + expected: "display dialog \"text\" with title \"title\" default answer \"answer\" with icon 0 giving up after 5", + }, + StringAssert{ + actual: buildDialogBox(DialogOptions{ + Text: "text", + Title: "title", + Answer: "answer", + HiddenAnswer: false, + Icon: "0", + Duration: 5, + Buttons: "Yes, No, Don't Know", + }), + expected: "display dialog \"text\" with title \"title\" default answer \"answer\" with icon 0 giving up after 5 " + + "buttons {\"Yes\",\"No\",\"Don't Know\"}", + }, + StringAssert{ + actual: buildDialogBox(DialogOptions{ + Text: "text", + Title: "title", + Answer: "answer", + HiddenAnswer: false, + Icon: "0", + Duration: 5, + Buttons: "Yes, No, Don't Know", + DefaultButton: "Don't Know", + }), + expected: "display dialog \"text\" with title \"title\" default answer \"answer\" with icon 0 giving up after 5 " + + "buttons {\"Yes\",\"No\",\"Don't Know\"} default button \"Don't Know\"", + }, + StringAssert{ + actual: buildDialogBox(DialogOptions{ + Text: "text", + Title: "title", + Answer: "answer", + HiddenAnswer: false, + Icon: "0", + Duration: 5, + Buttons: "Yes, No, Don't Know, One Too Many", + DefaultButton: "Don't Know", + }), + expected: "display dialog \"text\" with title \"title\" default answer \"answer\" with icon 0 giving up after 5 " + + "buttons {\"Yes\",\"No\",\"Don't Know\"} default button \"Don't Know\"", + }, + } - runStringAssertTests("buildDialogBox", stringAssertTests, t) + runStringAssertTests("buildDialogBox", stringAssertTests, t) }