NSString problem?

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! :slight_smile:

I’m not sure how you got it to work with one number, stringWithFormat doesn’t work in ASOC for number formatting.

Ric

Hi,

alternatively


	property NSTimer : class "NSTimer"
	property secNum : 0
	
	on start_(sender)
		set secNum to 0
		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 secNum to secNum + 1
		set hr to (secNum div 3600) as string
		set mn to (secNum mod 3600 div 60) as string
		set sc to (secNum mod 3600 mod 60 div 1) as string
		set timeString to pad(hr) & ":" & pad(mn) & ":" & pad(sc)
		timeLabel's setStringValue_(timeString)
		
	end onTime
	
	on pad(v)
		return text -2 thru -1 of ("0" & v)
	end addZero


I’m not sure either. :slight_smile:

That stinks. Could you be so kind as to explain why?

After thinking abut it…

Does this mean that

stringWithFormat_(“%@”, “Hello”)

will work? It isn’t number formatting.

The scripting bridge translates AppleScript numbers (integer and real) to NSValue, not to a C primitive like int or float.
That’s the reason why placeholders like %d or %f don’t work

Thank you StefanK and rdelmar!

I finally figured it out. This works:

set p to 12
set timeString to NSString’s stringWithFormat_(“%@”, 12)

I guess “%@” can hold anything…(well, not anything) :slight_smile:

To make an proper stopwatch don’t try to sum number because you forget the execution time of the method. If the method takes 0.003 seconds it means every second is is in fact 1.003 seconds on your stopwatch and after 10 minutes your stopwatch runs two seconds behind.

Try translating this code into AppleScriptObjC and you’ll have a proper stopwatch

Thank you DJ! I must admit I did not think as much as I should have about this…