Here is some code I’ve written for testing a piece of a Studio app. Its a simple interface with a combo box and a single button. The combo box has a list of names. One is chosen. If the name chosen is AAA Not Here, then you’re prompted to enter a name. This name gets added to the combo box items. The question I have is this - The list, that the Items in the combo box come from, are listed as a property, with the hopes that as the list grows each new name will be added to the list and then be available as a choice. I realize that using the build & run will compile the code and the property will revert to what it is initially set to in the script. I tried running the app from the “Products” to see if the list was retained and it didn’t seem to retain any new info when added, quit, and run again. Does the property declaration mean something a little different in 'Studio than in standard AppleScript? Here’s the code.
property theCustList : {"AAA Not Here", "ThisCustomer", "ThatCustomer"}
global temp, sortedList
on clicked theObject
(*Add your script here.*)
set sortedList to {}
set temp to {}
set cust to (content of combo box "combo1" of window of theObject) as string
display dialog cust
set v to (every combo box item of combo box "combo1" of window of theObject)
set c to count v
if cust = "AAA Not Here" then
display dialog "Who is the customer?" default answer ""
set cust to text returned of the result
if cust is not in theCustList then
set theCustList to theCustList & cust
end if
end if
set theCount to count theCustList
repeat with k from 1 to theCount -- I don't know if this code is necessary in 'Studio or not. This just alphabetizes the list.
repeat with anItem in theCustList
set theChk to anItem
repeat with i from 1 to (count theCustList)
if theChk is not less than item i of theCustList then
set theChk to item i of theCustList
end if
end repeat
end repeat
set sortedList to sortedList & theChk
repeat with i from 1 to (count theCustList)
if item i of theCustList is not theChk then
set temp to temp & item i of theCustList
end if
end repeat
set theCustList to temp
set temp to {}
end repeat
set theCustList to sortedList
display dialog theCustList as string
delete every combo box item of combo box "combo1" of window of theObject
repeat with anItem in theCustList
make new combo box item at end of combo box items of combo box "combo1" of window of theObject with data anItem
end repeat
return theCustList
end clicked
Ok, after much searching and piddling around, I found the stuff on Cocoa Binding. However, I’m at a loss here on how to get this to work the way I’d like. What I want is a list (a general list that starts out with a few selections in it) that can be modified by the user. If an item is not in the list it can be added and then chosen on a different run of the program. So I need a persistent list value that can be changed when necessary. I have tried using a combo box (drop down list). While experimenting with this I ended up puting in a text field, taking information from that field and adding it to the preference. I then added a combo box (which is ultimately what I want to use if posible) and used a loop to place the info from the preference into it. However, I have found that the preference, when something is entered, ends up being a list with individual characters of the last entry being the contents as opposed to being every word of the (hopeful) contents of the list. Any Ideas please? Oh, on the interface I did include a button which, when clicked, sends the info for processing. Here’s the code I have so far.
And another curiosity. The text entered into the text field only gets put into the preference when the return or enter key is pressed and not when the “Ok” button is clicked. What’s up with that?
on clicked theObject
(*Add your script here.*)
set cust to contents of text field "text1" of window of theObject
if cust is "AAA Not Here" then
set ansr to text returned of (display dialog "Who is the customer?" default answer "")
end if
try
display dialog ansr
end try
set t to contents of default entry "newList" of user defaults
set x to count t
repeat with y in t
display dialog y
end repeat
delete every combo box item of combo box "combo1" of window "bindTest"
repeat with i from 1 to (count t)
make new combo box item at end of combo box items of combo box "combo1" of window of theObject with data item i of t
end repeat
end clicked
on will finish launching theObject
(*Add your script here.*)
make new default entry at end of default entry of user defaults with properties {name:"newList", contents:"AAA Not Here"}
(*delete every combo box item of combo box "combo1" of window "bindTest"
repeat with i from 1 to (count t)
make new combo box item at end of combo box items of combo box "combo1" of window "bindTest"
end repeat*)
end will finish launching
I am not entirely sure what you want to do with your preferences list, but here’s a solution to some of the issues you were raising:
on awake from nib theObject
UpDateCombo(theObject)
end awake from nib
on will finish launching theObject
make new default entry at end of default entry of user defaults with properties {name:"newList", contents:{"AAA Not Here"}}
end will finish launching
on clicked theObject
set Win to window of theObject
if name of theObject = "quit" then quit
if name of theObject = "OK" then UpDateCombo(Win)
end clicked
on end editing theObject
set Win to window of theObject
UpDateCombo(Win)
end end editing
on UpDateCombo(Win)
set cust to contents of text field "text1" of Win
if cust is "AAA Not Here" then
set ansr to text returned of (display dialog "Who is the customer?" default answer "")
end if
try
display dialog ansr
end try
set t to (contents of default entry "newList" of user defaults)
if cust is not equal to "" and t does not contain cust then
copy cust to end of t
set contents of default entry "newList" of user defaults to t
end if
set t to contents of default entry "newList" of user defaults
delete every combo box item of combo box "combo1" of Win
repeat with i from 1 to (count t)
make new combo box item at end of combo box items of combo box "combo1" of Win with data (item i of t)
end repeat
end UpDateCombo
By placing the UpdateCombo handler in a separate section, you can call it from an object that was clicked, end editing, awake from nib, or whatever.
By changing the contents of NewList to a list, it makes it easier to populate your combo box from it. I added a line to ensure that no duplicates were created - is that what you were intending?
I have yet to try this, but this looks like it might be what I was looking for. I probably won’t get a chance to really try this for a couple of days at least. We suddenly got busy here at work so I’ll have to let you know. Thank you for the help.