I am trying to get on the Javascript for Automation bandwagon. I wrote a quick script that automates my travel request email, with a few dialog boxes.

Here is my code:

Mail = Application("Mail");

Mail.includeStandardAdditions = true

Origin = Mail.displayDialog(
                'Where is the trip starting from?',
                { defaultAnswer: 'Shanghai',
                  buttons:["Cancel", "Continue"],
                  defaultButton: "Continue"

Destination = Mail.displayDialog(
                'Where is the trip to?',
                { defaultAnswer: "",
                  buttons:["Cancel", "Continue"],
                  defaultButton: "Continue"

StartDate = Mail.displayDialog(
                'When are you leaving?',
                { title: "Onward Flight Date",
                  defaultAnswer: "",
                  buttons:["Cancel", "Continue"],
                  defaultButton: "Continue"

RtnDate = Mail.displayDialog(
                'When are you returning?',
                { title: "Return Flight Date",
                  defaultAnswer: "",
                  buttons:["Cancel", "Continue"],
                  defaultButton: "Continue"

content = 'Hi \n\n'

            + 'I am currently trying to arrange travel for an upcoming trip.\n\n' 

            + 'Could you please help me in sorting out the flights, hotels and airport transfers?\n\n'

            + 'Please find below the details of the trip:\n\n'

            + 'FLIGHTS\n\n'

            + 'Onward Journey:\n'+ '\n'

            + '     From \t\t\t\t- ' + Origin.text +'\n'
            + '     To \t\t\t\t\t- ' + Destination + '\n'
            + '     Date of Departure \t- ' + StartDate + '\n' + '\n'

            + 'Return Journey:\n'+ '\n'

            + '     From \t\t\t\t- ' + Destination + '\n'
            + '     To \t\t\t\t\t- ' + Origin + '\n'
            + '     Date of Departure \t- ' +RtnDate+ '\n' + '\n'

            + 'Preferred Airline \t\t\t\t- Star Alliance \n'
            + 'Meal Preference \t\t\t\t- Vegetarian\n'+ '\n'
            + 'Loyalty Program\t\t\t\t- United:\n'

            + '\n ________________________________________ \n'+ '\n'

            + 'HOTEL\n\n'
            + '     CheckIn Date \t\t- ' + StartDate + '\n'
            + '     CheckOut Date \t\t- ' + RtnDate + '\n' + '\n'

            + 'Preferred Hotels \t\t\t\t- SPG\n'

            + 'Loyalty Program \t\t\t\t- SPG:\n'

            + '\n ________________________________________ \n'+ '\n'

            + 'Thank you\n'

msg = Mail.OutgoingMessage({
        subject: 'Itinerary Request - ',
        content: content,
        visible: true 



But when I run the script, the places where I have used the variables seem to come as [object Object]. (Shown below)


Can anyone please point out where I am going wrong?

  • 1
    Attention: Close Voters - JavaScript for Automation (JXA) is on-topic since scripting is specifically listed as being on-topic. For those who do not know, JXA is an Apple technology that provides users with the ability to use JavaScript for interapplication communication between apps in macOS. – Monomeeth Jun 15 '17 at 7:42
  • Oops! Did my javascript tag cause some issues? So Sorry! I wasn't allowed to tag "javascript-automation" – Vinny Jun 15 '17 at 7:47
  • No worries. I've created the javascript-automation tag as I've seen a few of these questions now. – Monomeeth Jun 15 '17 at 7:57

displayDialog returns an object, not a string. When printing an object as a string, it is shown as [object Object]. The object returned from displayDialog is as follows:

{"buttonReturned":"Continue", "textReturned":"your text here"}

Therefore, you need to use the textReturned property of the object. You seemed to attempt this when you accessed the text property of Origin, but since the property is called ‘textReturned’, this didn't work either. For each of the variables where you are printing it in a string, replace

+ '     From \t\t\t\t- ' + Origin.text +'\n'
+ '     To \t\t\t\t\t- ' + Destination + '\n'


+ '     From \t\t\t\t- ' + Origin.textReturned +'\n'
+ '     To \t\t\t\t\t- ' + Destination.textReturned + '\n'

In the future when attempting to debug something like this, create a minimal, complete and verifiable example. In this case, just use one displayDialog and observe the value of the variable. (MCVE is something generally required in Stack Exchange questions, and I think this question was pushing that limit providing an entire script.)

  • That makes a lot of sense. And I shall keep MCV in mind for the next time ! Thank you so much. – Vinny Jun 15 '17 at 8:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .