Hi guys,
I’m running into a few problems, I need some advice, please!
I have a two text fields with steppers. The first has to be formatted as two digit number, the second, three digits. I have a number formatter attached to each text field and it’s stepper that specifies the minimum integers as either two or three. In the simulator, this works well, if I enter 1 as soon as I tab out of the text field it becomes 001 or 01. I have the stepper and the text field setup with a two way connection that both sends and receives takeIntValueFrom: to keep them in correlation. If I enter in a number, then click the stepper, it picks up where the text field left of.
The problem comes in with cocoa bindings. I have the text field set to bind to user defaults and it never seems to work correctly. It will either only have the last value of the text field, even if the stepper has changed it (but the text field displays the correct value), or it will have the last value set, but not the most recent change (i.e. I have to change it twice to the same value to get it to register that value). Could someone explain the best practice here? I’m so happy to be rid of my messy glue!
Another thing along the same lines. The text field with three digits has a number that corresponds to a key in a plist. I’d like the static text below the field and stepper to automatically update it’s contents with the value of that key (which is the name associated with that number) whenever a number has been typed in. I almost have this working, except it is still one behind, and doesn’t work at all with the stepper.
So the way I have it setup right now is to take the value from defaults, look at it and add a zero as needed to make it three digits. I’d really like for this value to simply be formatted correctly when it’s written and read from defaults!
Below is some code from the program to give you an idea of what I’m doing:
(* === Event Handlers === *)
on clicked theObject
set objectName to name of theObject
(*Add your script here.*)
end clicked
on changed theObject
set objectName to name of theObject
if objectName is "storeNumber" then
if getDefault("storeNumber") is less than 10 then -- If store number is less than ten add two zeros, i.e. 1 becomes 001
log "less than 10"
set storeNumber to (0 & 0 & (getDefault("storeNumber") as string))
else if getDefault("storeNumber") is greater than 9 and getDefault("storeNumber") is less than 100 then -- If store number is between one and ninety-nine add a zero, i.e. 34 becomes 034
log "more than 10, less than 100"
set storeNumber to (0 & (getDefault("storeNumber") as string))
else
log "more than 100."
set storeNumber to (getDefault("storeNumber") as string) -- Otherwise, leave it as is, i.e. 235 would be left as 235
end if
set storeName to (do shell script "defaults read ~/Desktop/com.apple.imagingassistant.storelist " & ("R" & (storeNumber))) -- Grab value from key, adding R before store number, i.e. 034 becomes R034
log storeName -- This should return a string that is the name to be displayed.
set content of text field 3 of box 3 of tab view item 1 of tab view of window 1 to storeName -- This sets the static text to the name corresponding to the above selected number.
end if
end changed
(* === Custom Handlers === *)
-- Simple handler to save space and give more clarity in code.
--
on getDefault(defaultKey)
return contents of default entry defaultKey of user defaults
end getDefault
If I didn’t need to format, my code could be:
-- Imaging Assistant.applescript
-- Imaging Assistant
-- Created by Benji on 12/3/08.
(* === Event Handlers === *)
on changed theObject
set objectName to name of theObject
if objectName is "storeNumber" then
set content of text field 3 of box 3 of tab view item 1 of tab view of window 1 to (do shell script "defaults read ~/Desktop/com.apple.imagingassistant.storelist " & ("R" & getDefault(storeNumber))) -- This sets the static text to the name corresponding to the above selected number.
end if
end changed
(* === Custom Handlers === *)
-- Simple handler to save space and give more clarity in code.
--
on getDefault(defaultKey)
return contents of default entry defaultKey of user defaults
end getDefault
So what should I do? Use a different way of formatting? Use a handler other than on changed?
Please advise!