Auto-start when procedure file is compiled

Here's a trick to start up some code when the procedures are compiled (so the user doesn't have to choose any menu or run a command).

Use a dynamic Menu definition to do two things:

1) Return the menu item string, as per normal.
2) Schedule the starting-up code with Execute/P.

Here's a demo. Copy and paste into the Procedure window and somehow cause compilation to occur (by, say, simply closing the Procedure window):

Menu "Macros", dynamic
    AutoStartMenu(),/Q,DoNormalStuff()
End

Function/S AutoStartMenu()

    // Test if auto start is needed
    NVAR/Z haveStarted= root:gHaveStarted
    if( !NVAR_Exists(haveStarted) )
        Variable/G root:gHaveStarted=1 // do this now to prevent double scheduling
        Execute/P/Q/Z "StartUp()"
    endif
    return "Do Normal Stuff"
End

Function StartUp()
    DoAlert 0, "Starting Up"
End
To make the StartUp function run every time procedures are compiled or re-compiled, modify it as so ...

Function StartUp()
       DoAlert 0, "Starting Up"
       killvariables/Z root:gHaveStarted   // killing variable here causes next compile to rerun this function
       return 0
end


--
J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAH

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More