Question

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 
        });

Mail.outgoingMessages.push(msg);

Mail.activate();

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

Result

Can anyone please point out where I am going wrong?

Was it helpful?

Solution

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'

with

+ '     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.)

Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top