Hello,
I want to use an AMTokenField in conjuction with an stepper for input of integer numbers.
Therefore I created an AMTokenField and an stepper and bound both values to a parameter of the type “Number”.
This seemed to work well, but whenever I enter a negative value (e.g. -10) an “uncaught exception” is raised and the value of my parameter ist stuck to that value. I even cannot reset the value of the parameter neither with the stepper nor programmatically. The new value (e.g. 0) is reflected by the AMTokenField, but when I run the action, the old value (-10) is being used.
Now I started off to write a lot of code working around that issue, binding another parameter (Text) to the token field, linking it manually to the Number parameter (controlTextDidChange_), validating everything, excluding fields with tokens…
But as I have five such AMTokenFields and five steppers in my ui and they are all linked together (if you change a “minutes” field, this may change the “hours” and “days” fields as well), so this really makes up a lot of code - and I am not sure, if it will work flawless in the end.
Is there an easier way to do this?
EDIT: It seems the main issue is that values get stuck somewhere, somehow (no exception raised anymore). Now I wrote most of the workaround, but still my parameters get stuck somewhere and cannot be changed anymore.
code examples (it´s not complete as you can see):
on controlTextDidChange_(sender)
set ctdcObj to sender's object
set ctdcStr to ctdcObj's stringValue() as text
tell me to log "controlTextDidChange_: " & ctdcStr
if (ctdcStr = "-") or (ctdcStr = "") then return -- assuming the input is not complete
if ctdcStr contains "$(" then return -- a token is used
try
set ctdcVal to ctdcStr as integer
set ctdcStr to ctdcVal as text
on error
set ctdcVal to 0
set ctdcStr to ""
end try
if ctdcObj is yearsField then
else if ctdcObj is monthsField then
else if ctdcObj is daysField then
else if ctdcObj is hoursField then
set ctdcOldVal to my parameters()'s valueForKey_("uiHours") as integer
if ctdcStr = "" then
log "restore hours: " & ctdcOldVal
my parameters()'s setValue_forKey_(ctdcOldVal as text, "uiHoursToken")
hoursField's setStringValue_(ctdcOldVal as text)
else
log "set hours: " & ctdcOldVal & " -> " & ctdcVal
my parameters()'s setValue_forKey_(ctdcVal as text, "uiHoursToken")
my parameters()'s setValue_forKey_(ctdcVal, "uiHours")
end if
else if ctdcObj is minutesField then
end if
end controlTextDidChange_
validation on end of editing:
on updateTokens_(sender)
log "updateTokens_"
try
set uptStr to my parameters()'s valueForKey_("uiDaysToken") as text
set uptStr to uptStr & my parameters()'s valueForKey_("uiHoursToken") as text
set uptStr to uptStr & my parameters()'s valueForKey_("uiMinutesToken") as text
if uptStr contains "$(" then
log "Tokens are used"
return
end if
on error errText
log errText
return
end try
try
set uptD to my parameters()'s valueForKey_("uiDaysToken") as integer
on error
set uptD to my parameters()'s valueForKey_("uiDays") as integer
end try
try
set uptH to my parameters()'s valueForKey_("uiHoursToken") as integer
on error
set uptH to my parameters()'s valueForKey_("uiHours") as integer
end try
try
set uptM to my parameters()'s valueForKey_("uiMinutesToken") as integer
on error
set uptM to my parameters()'s valueForKey_("uiMinutes") as integer
end try
set uptOff to (uptD * days) + (uptH * hours) + (uptM * minutes)
-- log "updateTimes - input days: " & uptD & " hours: " & uptH & " minutes: " & uptM
set uptD to uptOff div days
my parameters()'s setValue_forKey_(uptD, "uiDays")
my parameters()'s setValue_forKey_(uptD as text, "uiDaysToken")
set uptOff to (uptOff mod days) as integer
set uptH to uptOff div hours
my parameters()'s setValue_forKey_(uptH, "uiHours")
my parameters()'s setValue_forKey_(uptH as text, "uiHoursToken")
set uptOff to (uptOff mod hours) as integer
set uptM to uptOff div minutes
my parameters()'s setValue_forKey_(uptM, "uiMinutes")
my parameters()'s setValue_forKey_(uptM as text, "uiMinutesToken")
-- log "updateTimes - offset days: " & uptD & " hours: " & uptH & " minutes: " & uptM
end updateTokens_