Different ways to execute a function

Is it possible to execute a function the moment that an already open Igor experiment becomes the active window? (I'm using windows vista).

I know that you can execute a function from the prompt, or by some event like a mouse click, or the moment that an experiment opens the first time. Maybe there is some global variable that is changed any time igor is active or not? and I can trigger off of that?

I have a program that communicates with a scale for weighing things, and any time the Igor window becomes active, I'd like it to automatically go get the weight. Right now I have it executing from a button click, and I'd like to remove that step.

I have been using igor for last couple of years, but I'm still pretty new to it.

I appreciate any help.
Thanks!
GH
Hello GH,

You could use a hook function that triggers on the 'activate' event for the window. To set the hook function you use the SetWindow operation as in:

SetWindow myPanelName hook=myHookFunction

You need to have a (compiled) hook function in your procedure that looks something like:

Function myHookFunction(infoStr)
String infoStr

String event= StringByKey("EVENT",infoStr)
strswitch(event)
case "activate":
runTheScale()
break
endswitch

return 0
End

There are other options using named hooks but this approach should be simple enough.

HTH,

A.G.
WaveMetrics, Inc.

Igor wrote:

runTheScale()



Thanks for the quick reply, Igor. I will try a hook function, it seems that should do it. What is the above line though, I'm getting an error on that one?
gravityhomer wrote:
Igor wrote:

runTheScale()



Thanks for the quick reply, Igor. I will try a hook function, it seems that should do it. What is the above line though, I'm getting an error on that one?


That would be whatever the name of the function you've written that actually collects the measurement from your scale.
aclight wrote:
gravityhomer wrote:
Igor wrote:

runTheScale()



Thanks for the quick reply, Igor. I will try a hook function, it seems that should do it. What is the above line though, I'm getting an error on that one?


That would be whatever the name of the function you've written that actually collects the measurement from your scale.

haha, woops, thanks. Brain wasn't working on that one.
So this definitely works, but there is a small glitch that I don't think will cause a problem. If I stay within Igor and alternate back and forth between the panel and the procedure window then every time the panel is activated the function executes. However if I alternate back and forth between a web browser and igor, the function executes once when igor is activated and once when the web browser is activated (so it basically executes twice each time). Now I would just be getting the same weight twice, so I don't think this matters. But am I doing something wrong, or do you think this is the way it should be?

Function PanelNow()

NewPanel /N=myPanelName
SetWindow myPanelName hook=myHookFunction
end

Function myHookFunction(infoStr)             
    String infoStr
       
    String event= StringByKey("EVENT",infoStr)
    strswitch(event)
        case "activate":           
                   Test1()
        break
    endswitch
   

    return 0
End


Function Test1()
    Variable/G n
    n+=1
    Print "hello "+num2str(n) +" times"
end
GH,

The hook function is called with "activate" message whenever the window is activated. It does not differentiate between cases where the previously active window belonged to IGOR or to another application. If you plan to work on multiple windows within IGOR the hook function approach is not ideal. I recommend that you simply add a button control somewhere and press the button when you are sure that you want a measurement.

AG
Sorry I wasn't clear before. It DOES work if I stay within Igor. If I have multiple windows open in Igor (like a procedure, multiple panels, etc.), the program only executes whenever the panel "myPanelName" is the one that is activated. Which is the behavior I was hoping for.

Oh, it seems to be working fine now for external programs now too. The problem seemed to come before if I used the Start bar to switch back and forth between active programs.

But I agree, this is not the ideal way to trigger something. It is just something I am trying as a simple work around.

Thanks for the help. This forum is awesome. I will definitely check back often.