Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Applescript - selected list item in a dialog box??

Applescript - selected list item in a dialog box??
Thread Tools
Dedicated MacNNer
Join Date: Feb 2001
Location: Huddersfield, UK
Status: Offline
Reply With Quote
Jun 7, 2001, 06:57 PM
 
(Sorry for the long post!...)

To 'introduce' myself to the Applescript language, I am writing a script that will create a web page based on answers to questions asked by the script.

In setting the background color, I would like the user to be able to select a color from a list presented to them.

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier><font color = green>if</font> the button_pressed is <font color = red>"Colour picker"</font> then choose from list {<font color = red>"Red"</font>, <font color = red>"Green"</font>, <font color = red>"Blue"</font>, <font color = red>"Orange"</font>, <font color = red>"Yellow"</font>} with prompt <font color = red>"Choose a colour:"</font> <font color = green>default</font> items {<font color = red>"Red"</font>}</font>[/code]

As shown above, displaying the list of color options is not a problem. However, once the user clicks on 'OK', they will be returned to a dialog box, defined by the following:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>display dialog <font color = red>"Enter a background colour (as a hexadecimal value):"</font> <font color = green>default</font> answer <font color = red>"FFFFFF"</font> buttons {<font color = red>"Cancel"</font>, <font color = red>"Colour picker"</font>, <font color = red>"OK"</font>}</font>[/code]

My problem is copying the answer selected in the list into this dialog box so that the user can see that their choice has been recognised. A further problem is converting the items in the list into hexadecimal RGB values to display in the dialog box.

For example, in the first dialogue box, the user will be asked to enter a color value or pick from a list. Once the user has selected a colour from the list, I would like the script to display the color's equivalent hexadecimal RGB value in the original dialogue box. My questions are:
  • How do I place the value returned from a list (the "choose from list" command) into a dialogue box?
  • If the values displayed in the list are normal words, how do I get them to display their hexadecimal RGB values in the dialog box?

Thanks in advance for your help!
PM G4 DP 500 MHz, 768 Mb, DVD-ROM, 85 Gb, Mac OS X 10.3.9
PB G4 1.25 GHz, 512 Mb, DVD-R, 80 Gb, Mac OS X 10.4
     
Dedicated MacNNer
Join Date: Nov 1999
Location: Georgetown, Demerara, Guyana
Status: Offline
Reply With Quote
Jul 27, 2001, 05:03 PM
 
Hi Richyfp,

[1] The 'choose from list' command returns a value of varying type (e.g., a list, a string, or even a boolean), depending on the parameters supplied to the command and on whether the user clicks OK or Cancel. In your example, it would return a list containing a single string, viz., the chosen colour-name. To obtain the chosen colour-name, you could do something like this:-<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>set bColourPickWasCancelled to false

<font color = green>if</font> (button_pressed is <font color = red>"Colour picker"</font>) then
set theChosenColourNameAsList to ¬
(choose from list {<font color = red>"Red"</font>, <font color = red>"Green"</font>, <font color = red>"Blue"</font>, <font color = red>"Orange"</font>, <font color = red>"Yellow"</font>} ¬
with prompt <font color = red>"Choose a colour:"</font> <font color = green>default</font> items {<font color = red>"Red"</font>})

<font color = green>if</font> (theChosenColourNameAsList is false) then
-- The user pressed 'Cancel' in <font color = green>this</font> sub-dialogue
set bColourPickWasCancelled to true
<font color = green>else</font>
-- Extract the chosen colour
set theChosenColourName to (item <font color = blue>1</font> of theChosenColourNameAsList)
end
end</font>[/code]

[2.A] To convert the returned colour string into a hexadecimal string, you could set up a hardcoded "table" that maps colour names to their hex equivalents. E.g., you could use a list of sublists where each sublist represents a key/value pair, as follows:-<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>property k_ColourTable:{{<font color = red>"Red"</font>, <font color = red>"FF0000"</font>} ¬
, {<font color = red>"Green"</font>, <font color = red>"00FF00"</font>} ¬
, {<font color = red>"Blue"</font>, <font color = red>"0000FF"</font>} ¬
, {<font color = red>"Orange"</font>, <font color = red>"FF7F00"</font>} ¬
, {<font color = red>"Yellow"</font>, <font color = red>"FFFF00"</font>} ¬
}</font>[/code][2.B] Then, it would just be a matter of looping through the list's sublists to find the matching key/value pair, something like this:-<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>set bFoundColourHexValue to false

repeat with thisColourEntryRef in k_ColourTable
set thisColourEntry to (contents of thisColourEntryRef)

<font color = green>if</font> (theChosenColourName = (item <font color = blue>1</font> of thisColourEntry)) then
-- We found a match
set theChosenColourHexValue to (item <font color = blue>2</font> of thisColourEntry)
set bFoundColourHexValue to true
exit repeat
end
end</font>[/code]

[3] Unfortunately, there's no direct way to keep the main dialogue on-screen after the user clicks the 'Colour picker' button; by that time, the original dialogue has already been dismissed. However, it is possble to achieve a very similar effect, by using a repeat loop to continuously re-display the main dialogue after the 'Colour picker' sub-dialogue has itself been dismissed. In other words, putting it all together, we could do something like this:-<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>-- &lt; Colour <font color = red>"table"</font> is copied in here from listing <font color = blue>2.</font>A &gt;
property k_ColourTable:{{<font color = red>"Red"</font>, <font color = red>"FF0000"</font>} ¬
, {<font color = red>"Green"</font>, <font color = red>"00FF00"</font>} ¬
, {<font color = red>"Blue"</font>, <font color = red>"0000FF"</font>} ¬
, {<font color = red>"Orange"</font>, <font color = red>"FF7F00"</font>} ¬
, {<font color = red>"Yellow"</font>, <font color = red>"FFFF00"</font>} ¬
}

-- ------------------------------------------------------------------------------
on run
-- Initialisation
set bUserIsDone to false
set theChosenColourHexValue to <font color = red>"FFFFFF"</font> -- Default assumption

-- Keep prompting <font color = green>for</font> a colour hex-value, until the User dismisses the main dialogue
repeat until bUserIsDone
-- Display the main dialogue
set theReply to ¬
(display dialog (<font color = red>"Enter a background colour (as a hexadecimal value):"</font>) ¬
<font color = green>default</font> answer theChosenColourHexValue buttons {<font color = red>"Cancel"</font>, <font color = red>"Colour picker"</font>, <font color = red>"OK"</font>})

-- Process the User's desired action
set button_pressed to (button returned of theReply)

<font color = green>if</font> (button_pressed is <font color = red>"Colour picker"</font>) then
-- &lt; Code is copied in here from the listing <font color = blue>1</font> &gt;
-- &lt; This will obtain 'theChosenColourName' &gt;

set bColourPickWasCancelled to false

set theChosenColourNameAsList to ¬
(choose from list {<font color = red>"Red"</font>, <font color = red>"Green"</font>, <font color = red>"Blue"</font>, <font color = red>"Orange"</font>, <font color = red>"Yellow"</font>} ¬
with prompt <font color = red>"Choose a colour:"</font> <font color = green>default</font> items {<font color = red>"Red"</font>})

<font color = green>if</font> (theChosenColourNameAsList is false) then
-- The user pressed 'Cancel' in <font color = green>this</font> sub-dialogue
set bColourPickWasCancelled to true
<font color = green>else</font>
-- Extract the chosen colour
set theChosenColourName to (item <font color = blue>1</font> of theChosenColourNameAsList)
end

<font color = green>if</font> ( not bColourPickWasCancelled ) -- A colour was indeed picked
-- Translate the chosen colour-name into its hex equivalent

-- &lt; Code is copied in here from the listing <font color = blue>2.</font>B &gt;
-- &lt; This sets 'theChosenColourName' <font color = green>for</font> the next iteration &gt;
set bFoundColourHexValue to false

repeat with thisColourEntryRef in k_ColourTable
set thisColourEntry to (contents of thisColourEntryRef)

<font color = green>if</font> (theChosenColourName = (item <font color = blue>1</font> of thisColourEntry)) then
-- We found a match
set theChosenColourHexValue to (item <font color = blue>2</font> of thisColourEntry)
set bFoundColourHexValue to true
exit repeat
end
end

-- Ensure that we were able to translate
<font color = green>if</font> (not bFoundColourHexValue) then
set theErrMsg to (<font color = red>"Sorry, unknown colour-name: "</font> & theChosenColourName)
set theErrNum to <font color = blue>1000</font>
error theErrMsg number theErrNum
end
end

<font color = green>else</font> <font color = green>if</font> (button_pressed is <font color = red>"OK"</font>) then
-- Indicate that we're done, so we can exit the loop
set bUserIsDone to true

<font color = green>else</font> (* 'Cancel' button was pressed *)
-- Normally, <font color = green>this</font> would automatically be signalled as error <font color = blue>-128</font>,
-- so we shouldn't even get here...
end

end -- <font color = brown>// repeat until bUserIsDone</font>

-- &lt; At <font color = green>this</font> point, the desired colour hex-value is in 'theChosenColourHexValue' &gt;
-- &lt; It should be validated now, in <font color = green>case</font> it was typed directly &gt;
-- &lt; Then, we can <font color = green>do</font> the rest of the script &gt;
-- [ ... ]

<font color = green>return</font>
end</font>[/code]

Regards,

--Paul
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:06 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2