2.5.1.12 On A Timer

The Timer (Command) executes the TimerProc macro, and the combination can be used to run a script every n seconds.

The following example shows a timer procedure that runs every 2 seconds to check if a data file on disk has been modified, and it is then re-imported if new.

In order to run this scipt example, perform the following steps first:

  1. Create a simple two-column ascii file c:\temp\mydata.dat or any other desired name and location
  2. Start a new project and import the file into a new book with default ascii settings. The book short name changes to mydata
  3. Create a line+symbol plot of the data, and set the graph x and y axis rescale property to auto so that graph updates when new data is added
  4. Keep the graph as the active window
  5. Save the script below to the [AfterOpenDoc] section of the ProjectEvents.OGS file attached to the project.
  6. Add the following command to the [BeforeCloseDoc] section of ProjectEvents.OGS:
    timer -k;
  7. Save the Origin Project, close, and then re-open the project. Now any time the project is opened, the timer will start, and when the project is closed the timer will stop executing.
  8. Go to the data file on disk and edit and add a few more data points
  9. The timer procedure will trigger a re-import and the graph will update with the additional new data
// Set up the TimerProc macro
def TimerProc {
	// Check if file exists, and quit if it does not
	string str$="c:\temp\mydata.dat";
	if(0 == exist(str$) ) return;

	// Get date/time of file on disk 
	double dtDisk = exist(str$,5);
	
	// Run script on data book
	// Assuming here that book short name is mydata
	win -o mydata {
		// Get date/time of last import
		double dtLast = page.info.system.import.filedate;
	
		// If file on disk is newer, then re-import the file
		if( dtDisk > dtLast ) reimport;
	}
}

// Set TimerProc to be executed every 2 seconds
timer 2;

The Samples\LabTalk Script Examples subfolder has a sample Origin Project named Reimport File Using Timer.OPJ which has script similar to above set up. You can open this OPJ to view the script and try this feature.