Hello all! Hopefully this will be a quick question.
I have made a simple stopwatch in Objective-C before, and now I wanted to try to make one in AppleScriptObjC. So I wrote the following code.
property NSTimer : class "NSTimer"
property NSString : class "NSString"
on start_(sender)
set schTime to NSTimer's alloc()'s init()
set schTime to NSTimer's scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(1.0, me, "OnTime", missing value, true)
end start_
on OnTime()
set sec1Num to sec1Num + 1
if sec1Num = 10 then
set sec1Num to 0
set sec2Num to sec2Num + 1
end if
if sec2Num = 6 then
set sec2Num to 0
set min1Num to min1Num + 1
end if
if min1Num = 10 then
set min1Num to 0
set min2Num to min2Num + 1
end if
if min2Num = 6 then
set min2Num to 0
set hour1Num to hour1Num + 1
end if
if hour1Num = 10 then
set hour1Num to 0
set hour2Num to hour2Num + 1
end if
set timeString to NSString's stringWithFormat_("%d%d:%d%d:%d%d", {hour2Num, hour1Num, min2Num, min1Num, sec2Num, sec1Num})
timeLabel's setStringValue_(timeString)
end OnTime
So now you see how I am approaching this. I just set the label to the NSString. Well, I tried the stringWithFormat with and without the curly brackets and it doesn’t work. I also originally tried “%i” instead of “%d”. I used “%i” in my Objective-C stopwatch.
What happens when the timer runs is that every second, the numbers in the label are changed randomly. Interestingly, the colons “:” never show up in the label.
The Objective-C stopwatch worked with the exact if statements with no problems.
EDIT I would also like to add that I tried the stringWIthFormat with one number, and it worked just fine.
Thank you all for your help!