Progress bar - NStimer

Hello all,

Thank you for accepting me to your site :slight_smile:
I am new to the site and to ASOC - with little experience in AS.

I am trying to set a progress bar in ASOC but I can’t find a way to do it. I was reading this: http://macscripter.net/viewtopic.php?id=36954
It is the closest to what I want but its not exactly the way I want it and I don’t understand how to modify it.

What I want is to show the progress of the NStimer that count the “delay” time until it fires a handler.
The NStimer is delaying for “thedelay” time period that the user is set.
The progress bar is having an outlet “delaybar”, and the bar is undetermined.

This is my last attempt (I have tried lot of deferent things) but apparently the NStimer is not returning the seconds:

on button_(sender)
        set mytimer to NSTimer's scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(thedelay, me, "dotheloop:", 1, true)
        delayBar's incrementBy_(myTimer)
    end button_

Any ideas on this will be much appreciated!
Thank you in advance!

Hi,

the arguments of the method scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_() mean

timeInterval → the amount of seconds until the timer fires
target → the class to send the delegate method to
selector → the delegate method which will be called when the timer fires
userinfo → an custom object to be passed
repeats → does the timer fire continuously

In your case a method named dotheloop: is called when the timer fires, the argument is the NSTimer instance.
The variable theDelay must be defined somewhere


property thedelay : 1.0

on button:sender
	NSTimer's scheduledTimerWithTimeInterval:thedelay target:me selector:"dotheloop:" userInfo:1 repeats:true
end button:

on dotheloop:theTimer
	delayBar's incrementBy:(theTimer's userInfo())
end dotheloop:

Thank you for your time on this!
Although I think that I didn’t gave the right explanation on what I want to do

Let me clear things up

the “thedelay” variable is set by the user, so you never know the number. In your example if the user set the variable to lets say 500seconds then the bar will move by 1 every 500seconds, right? or I missing something? . What I want is to count the “thedelay” (500 seconds) in the bar and then fire the dotheloop.

Right now the NStimer is counting the “thedelay” and then fires the dotheloop. I want something in the middle to progress the bar accordingly to NStimer. Something like a countdown.

In other words I want to show the NStimer progress.

See all the script here:



script ReloaderAppDelegate
	property parent : class "NSObject"
	
    property thedelay: ""
    property NSTimer: class "NSTimer"
    property mytimer:""
    property myProgressBar: missing value
    property delaybar : missing value
    
	-- IBOutlets
	property window : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
    
    on dotheloop_(sender)
        delayBar's incrementBy_(0) -- to turn the bar to 0 (?)
        myProgressBar's startAnimation:me --this is another bar to indicate that the dotheloop is working-its just animate.
        
        --code that I want to execute every "thedelay" seconds.
            
        myProgressBar's stopAnimation:me
    end dotheloop_

    on stoptheloop_(sender)
        mytimer's invalidate()
        myProgressBar's stopAnimation:me
    end stoptheloop_
    
    on button_(sender)
        set mytimer to NSTimer's scheduledTimerWithTimeInterval_target_selector_userInfo_repeats_(thedelay, me, "dotheloop:", "", true)
    end button_

end script

then you need something like this


property thedelay : 0
property counter : 0

on dotheloop:sender
	delayBar's incrementBy:1
	set counter to counter - 1
	if counter is 0 then
		--code that I want to execute every "thedelay" seconds.
		restartProgressBar()
	end if
end dotheloop:

on button:sender
	-- thedelay is set
	restartProgressBar()
	NSTimer's scheduledTimerWithTimeInterval:1.0 target:me selector:"dotheloop:" userInfo:(missing value) repeats:true
end button:

on restartProgressBar()
	set counter to thedelay
	delayBar's setDoubleValue:0.0
	delayBar's setMaxValue:thedelay
end restartProgressBar

the user sets the variable thedelay and presses the button
A variable counter is set to the value of thedelay
the maximum value of the progress bar is set to value of the delay
the timer is set to fire each second
when the timer fires the delegate method is called, the variable counter is decremented and the progress bar is incremented
when the variable counter is zero the extra code is executed and the progress bar is reset

Thank you! This is great!

I was hoping to do it by reading the NStimer but this one is working just fine.

Thank for your time! :slight_smile: