You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript buttons returned from display dialog

Hi!

I have been learning Applescript for about a month and found the following issue which I thought was an easy implementation.

On the "text returned" I get the error condition described. Do not understand!


Any help would be appreciated.


display dialog ("ENTER COMPANY NAME") with title ("TOTAL SHARE SPECIFICATION")

buttons {"Cancel", "Company"} default button "Company" default answer ""

set BUTTON_Returned to button returned of the result

set CompanyName to text returned of the result



error "Can’t get text returned of \"Company\"." number -1728 from text returned of "Company"


MacBook 6.1 OS X Yosemite Vn 10.10.1

Script Editor Vn. 2.7 (176)

Applescript 2.4


Regards

Posted on Dec 18, 2014 5:05 AM

Reply
Question marked as Top-ranking reply

Posted on Dec 18, 2014 5:55 AM

se

set the_results to (display dialog ("ENTER COMPANY NAME") with title ("TOTAL SHARE SPECIFICATION") buttons {"Cancel", "Company"} default button "Company" default answer "")
set BUTTON_Returned to button returned of the_results
set CompanyName to text returned of the_results
9 replies
Question marked as Top-ranking reply

Dec 18, 2014 5:55 AM in response to tybach

se

set the_results to (display dialog ("ENTER COMPANY NAME") with title ("TOTAL SHARE SPECIFICATION") buttons {"Cancel", "Company"} default button "Company" default answer "")
set BUTTON_Returned to button returned of the_results
set CompanyName to text returned of the_results

Dec 18, 2014 5:58 AM in response to tybach

The short answer is because you are using result which always refersto the result returned by the last statement. So in this case the result in the line

setCompanyNametotext returnedoftheresult

is not referring to the return value of the display dialog statement but to the value returned by the

setBUTTON_Returnedtobutton returnedoftheresult

statement.


To fix this set a variable to the value returned by the display dialog statement and use that.


set valueReturned to display dialog ("ENTER COMPANY NAME") with title ("TOTAL SHARE SPECIFICATION") buttons {"Cancel", "Company"} default button "Company" default answer ""


set BUTTON_Returned to button returned of valueReturned
set CompanyName to text returned of valueReturned

Dec 18, 2014 8:31 AM in response to tybach

No question that Applescript can be frustrating to learn. Many things about it have to be figured out by trial and error and when dealing with scripting applications all bets are off. The application designer can pretty much do as they want so what works in one app might not work in another even though you are using the same and correct applescript 😕


I find Introduction to AppleScript Language Guide by Apple to be a good source of answers and usually always have it open when I'm scripting for quick reference.


Spend some time getting familiar with the script editor. It is intelligent and can usually steer you in the right direction when it comes to debugging. Especially get use to and pay attention to the colors it assigns to items in your script.


For example in your original script notice the colors of Button_Returned and CompanyName (your variables) and compare those to result and button returned. Also notice how text returned is a different color from button returned even though both are essentially the same thing. This is an indication that something is not right and it is where the error message occurred. (for a list of the default colors used open the Script Editors preferences and look at the Formatting tab).


Notice also how the editor formats your scripts, again the way the script gets formatted can tell you a lot if there is an error.


Finally the error messages while cryptic can offer some clues. In your case the error message was


error "Can’t get text returned of \"Company\"." number -1728 fromtext returnedof "Company"

generated by the line

setCompanyNametotext returnedoftheresult

The last part of the error message is essentially the line that caused the error after evaluation. So here result evaluated to “Company” which doesn't seem right, display dialog returns a record.


Finally there is the log statement which will print its value to the log window

User uploaded file

Hope this helps


regards

Dec 18, 2014 11:54 AM in response to tybach

Another handy trick is to use a variable to hold all the responses from the dialog:


set the_response to {text returned, button returned} of (display dialog "Feed me some info..." buttons {"1", "2", "3"} default answer "Some info")

item 1 of the_response -- the text

item 2 of the_response -- the button pressed


These can be recalled long after the dialog has been dismissed.

Dec 18, 2014 12:54 PM in response to HD

As display dialog returns a record you can take that one step farther and get


display dialog "Hello" default answer "" buttons {"1", "2", "3"} giving up after 5


set {ans, button, giveUp} to {text returned, button returned, gave up} of result


display dialog "Ans: " & ans & return & "Button: " & button & return & "Give Up: " & giveUp

Then the values returned are stored in their own variable and there is no need to access them from the list.

Dec 19, 2014 2:02 AM in response to Frank Caggiano

Hi! Tony T1, Frank and HD


Many thanks Guys for your great input during my skill development re Applescript. I started a little while ago to learn it because I felt that my use of Numbers could be enhanced in initially developing and maintaining an Equities Database. I know now that my belief was correct - but I now also am beginning to understand that Applescript is much more of course.


Many thanks again for your ready responses and perhaps I might mention Frank with his remarkable exposition of the context of the issues discussed.

I am reminded of the old proverb,

"give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime."

Many thanks again - I doubt that will be the last time I raise my hand.


Warm Regards

Applescript buttons returned from display dialog

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.