Re: Using results of dialogs?
Re: Using results of dialogs?
- Subject: Re: Using results of dialogs?
- From: Paul Berkowitz <email@hidden>
- Date: Mon, 19 May 2014 11:17:46 -0700
- Thread-topic: Using results of dialogs?
Title: Re: Using results of dialogs?
'result' is not a variable. It doesn't have to be set explicitly. It's a special AppleScript term that can be used in this context if you don't want to be bothered with a variable and, yes, it loses its context after one line of code. It's ephemeral. It's simpler to in fact set a genuine variable to the display dialog, which will be preserved as long as you may need it.
set theResult to display dialog "Enter the name of the folder to be searched. Capitalization does matter: " default answer "Sent" with title "Folder to Search" buttons btnList_nextCancel default button 1 cancel button 2
if button returned of theResult = btn_cancel then
return false
end if
set targetMailbox to text returned of theResult as text
You see that AppleScript Editor now shows theResult in green, its color for variable names, not purple, its color for 'properties'. If you look up 'display dialog' in Standard Additions dictionary, you'll see that this is the format given, with no mention of 'result' anywhere. Using 'result' is a mini-shortcut that saves a few characters of typing if you never need to refer to it again. Otherwise, don't use it. You might find it easier to never use it.
--
Paul Berkowitz
From: Alex Hall <email@hidden>
Date: Fri, 16 May 2014 19:02:33 -0400
To: "koenig.yvan" <email@hidden>
Cc: AppleScript-Users <email@hidden>
Subject: Re: Using results of dialogs?
Thanks, I think it is all working now. I am still not quite clear on why the result variable goes away, but it makes a whole lot more sense to assign the "display dialog" to a variable so it sticks around.
On May 16, 2014, at 4:48 PM, koenig.yvan <email@hidden> wrote:
Le 16/05/2014 à 22:27, Alex Hall <email@hidden> a écrit :
Hi all,
I'm writing a script for a friend. It first prompts for a string, then for a selection from a list, then another string. On each dialog I have a check to be sure the user didn't cancel, which i just added. Now that I have that, though, the line after the check, where I grab the result, gives me an error that "result" is undefined. Do the results of these prompts go away after one use?
Here's my code. I've placed a double percent sign on the line that throws the error.
set btn_ok to "OK"
set btn_next to "Next"
set btn_cancel to "Cancel"
set btnList_nextCancel to {btn_next, btn_cancel}
display dialog "Enter the name of the folder to be searched. Capitalization does matter: " default answer "Sent" with title "Folder to Search" buttons btnList_nextCancel default button 1 cancel button 2
if button returned of result = btn_cancel then
return false
end if
%% set targetMailbox to text returned of result as text
When the instruction if button returned of result = btn_cancel then
is executed, the variable result no longer contain your dialog answer, it contains the result of the test.
In fact, the test upon Cancel is useless.
As you defined the cancel button in your instruction (cancel button 2), the cancel choice will never be reported in your code.
Given that, you may use :
set btn_ok to "OK"
set btn_next to "Next"
set btn_cancel to "Cancel"
set btnList_nextCancel to {btn_next, btn_cancel}
display dialog "Enter the name of the folder to be searched. Capitalization does matter: " default answer "Sent" with title "Folder to Search" buttons btnList_nextCancel default button 1 cancel button 2
set targetMailbox to text returned of result as text
If the test was really useful, you would have to code :
set btn_ok to "OK"
set btn_next to "Next"
set btn_cancel to "Cancel"
set btnList_nextCancel to {btn_next, btn_cancel}
set myAnswer to display dialog "Enter the name of the folder to be searched. Capitalization does matter: " default answer "Sent" with title "Folder to Search" buttons btnList_nextCancel default button 1 cancel button 2
if button returned of myAnswer = btn_cancel then
return false
end if
set targetMailbox to text returned of myAnswer as text
Run this late version from the script editor and look at the Events log issued when you hit Cancel (or press Escape).
You will get :
tell application "AppleScript Editor"
display dialog "Enter the name of the folder to be searched. Capitalization does matter: " default answer "Sent" with title "Folder to Search" buttons {"Next", "Cancel"} default button 1 cancel button 2
--> error number -128
Résultat :
error "Annulé par l’utilisateur." number -128
which is exactly what is also done by the "stripped" version.
Yvan KOENIG (VALLAURIS, France) vendredi 16 mai 2014 22:48:23
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden
--
Have a great day,
Alex Hall
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden