Friday, December 28, 2007

Cocoa: Timed methods

Sometimes one wants to run a job a regular or scheduled times. One option would be to start a loop that runs until some abort condition is reached and that sleeps after the job is done. This is easily done, but has the big disadvantage to block the GUI while it is sleeping.
A better alternative is to set up a timer that calls you back from time to time. Cocoa offers the NSTimer class for this. It offers a wealth of methods to set up the timer and insert it into the RunLoop. The timer will then send from time to time a message to a method of your app that does the job.
It is actually easy to create the job. First you need to decide which method will be notified. In our example here it is - (void) runIt (with no arguments).
The code to set up the timer could then look like this:

NSTimer *timer;
timer = [NSTimer scheduledTimerWithTimeInterval:10.0
  target:self
  selector: @selector(runIt)
  userInfo:nil
  repeats: YES];

So this one schedules the execution of the runIt method of the same class the timer is created in, to repeat every 10 seconds.
Simple.
To cancel the timer again, one just has to send it an invalidate method:
[timer invalidate]

I was first experimenting with scheduledTimerWithTimeInterval:invocation:repeats: but for whatever reason it did not work, even if the timer said it is valid. But the above works well for me and is even easier.

Some Mac coding ... finally

I did some coding on Mac OS X lately. Basically the first time some real stuff besides just firing Xcode and InterfaceBuilder and closing them after some poking around.

At first it feels somewhat strange to code in Objective-C, given that I mostly did Java coding for the last five years, but then it feels good again. I am writing 'again' on purpose, as the whole programing is like it was over ten years ago on the NeXT.

I will try to put some stuff online, as I did fight against some obstacles and want to share the results.

Saturday, December 08, 2007

Small Applescript to make iTunes play

I recently put my Mac mini into a server room and am running it headless now.
As this one also has the iTunes library and the connection to the stereo, I was using VNC to connect to it and to start and stop iTunes. Of course that is cumbersome.
So I googled a little on "applescript" and "iTunes" and found this wonderful site.

Using the information on it I was able to come up with this script, that I saved as playTunes.scpt

tell application "iTunes"
if player state is paused then
play user playlist "a_Ungespielt"
return "Playing"
else
pause
return "Stopped"
end if
end tell


It will play the contents of a playlist called "a_Ungespielt" if iTunes is paused and stop playing otherwise.

I can now just log into the mini and start/stop playing with "osascript playTunes.scpt".

Saturday, December 01, 2007

Marlene is standing on her own


Marlene is growing well. She is eight months old now and since three weeks, she is able to sit up on her own. She is not yet able to crawl on arms and legs, but she is already moving around quite a bit.

Today she was for the first time able to stand up on her own by holding the bars of her bed and pulling herself up. She was totally happy and excited - as we have been as well