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 thenis 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
|