How to run Origin periodically using Windows Task Scheduler

You may wonder how to run the Origin application (data import, analysis, graphing, etc.) periodically, say at a certain time every day, or even only one specific time on a specific day. This blog explains the procedure to set up such task.  Basically, you’ll need to program the sequence of the tasks, and use the Windows “Task Scheduler” to  run a script file  with a specific schedule.

We will use the following example to illustrate:

1) Make a target process
In this sample, we will grab the latest update from an NOAA page and generate a graph and export it as image file to rour desktop. The OriginC code is as below (you can also directly download this file and put the file under your <User Files Folder>\OriginC)

void ProcessTest_NOAA() {
	string strURL = "http://services.swpc.noaa.gov/text/3-day-forecast.txt";
	string strFile = GetOriginPath(ORIGIN_PATH_USER) + "NOAA.txt";
	int err = okutil_http_download(strURL, strFile);
	if (err == 0) {
		Worksheet wks;
		wks = ImportNOAAFile(strFile);
		DeleteFile(strFile);
		if (wks.IsValid()) {;
			ExportGraph(MakeGraph(wks));
		}
	}
	Project.ClearModified();
	LT_execute("exit;")
}

static Worksheet ImportNOAAFile(string strFullPath) {
	Worksheet wks;
	ASCIMP  ai;
    if(AscImpReadFileStruct(strFullPath, &ai)==0) {
    	ai.iDelimited = 0;
	    strcpy(ai.szFixedWidth,"10, 11, 11, 11");
       	ai.iHeaderLines = 13;
	    ai.iSubHeaderLines = 1;
		ai.iAutoSubHeaderLines = 0;
		ai.iRenameWks = 0;
		ai.nLongNames = 0;
		ai.iPartial = 1;
		ai.iPartialR1 = 0;
		ai.iPartialR2 = 8;
		ai.iPartialC1 = 0;
		ai.iPartialC2 = 3;
		
		wks = Project.ActiveLayer();
		wks.SetSize(-1, 5);
		Dataset ds;
		if (0 == wks.ImportASCII(strFullPath, ai)) {
			// Time
			ds.Attach(wks, 0);
			ds = UnwrapTime(wks.Columns(0), GetToday());
			wks.Columns(0).SetLongName("Time");
			wks.Columns(0).SetUnits("UT");
			wks.Columns(0).SetFormat(OKCOLTYPE_DATE); 
			wks.Columns(0).SetSubFormat(9); 
			
			// Data
			ds.Attach(wks, 4);
			ds.SetSize(0);
			for(int ii = 1; ii <= 3; ii++) {
				vector<double> vs;
				vs = UnwrapData(wks.Columns(1));
				ds.Append(vs);
				wks.DeleteCol(1);
			}
			wks.Columns(1).SetLongName("Kp index");
		}
    }
    return wks;
}

static vector<double> UnwrapTime(const Column &cc, double dToday) {
	vector<double> vs;
	vector<string> sa;
	cc.GetStringArray(sa);
	vs.SetSize(sa.GetSize()*2*3);
	for(int nDayOffset = 0; nDayOffset <=2; nDayOffset++) {
		for(int ii = 0; ii < sa.GetSize(); ii++) {
			vs[ii*2+nDayOffset*sa.GetSize()*2] = (int)dToday + nDayOffset + atof(sa[ii].Mid(0, 2))/24;
			vs[ii*2+1+nDayOffset*sa.GetSize()*2] = (int)dToday + nDayOffset + atof(sa[ii].Mid(3, 2))/24;
			if (ii == sa.GetSize()-1) {
				vs[ii*2+1+nDayOffset*sa.GetSize()*2] = (int)dToday + nDayOffset + 1;
			}
		}
	}
	
	return vs;
}

static vector<double> UnwrapData(const Column &cc) {
	vector<double> vs;
	vector<string> sa;
	cc.GetStringArray(sa);
	vs.SetSize(sa.GetSize()*2);
	for(int ii = 0; ii < sa.GetSize(); ii++) {
		vs[ii*2] = vs[ii*2+1] = ChkDataValue(sa[ii]);
	}
	
	return vs;
}

static double ChkDataValue(string str) {
	if (is_missing_value(atof(str))) {
		str.TrimLeft();
		str.TrimRight();
		return atof(str.Left(str.Find('(')));
	}
	else {
		return atof(str);
	}
}

static double GetToday() {
	SYSTEMTIME sysTime;
	GetSystemTime(&sysTime);
	double dDate;
	SystemTimeToJulianDate(&dDate, &sysTime);
	return dDate;
}

static GraphPage MakeGraph(const Worksheet &wks) {
	Curve cv(wks, 0, 1);
	GraphPage gp;
	gp.Create("Origin");
	GraphLayer gl = gp.Layers(0);
	DataPlot dp = gl.DataPlots(gl.AddPlot(cv, IDM_PLOT_LINE));
	gl.GraphObjects("Legend").Destroy();
	
	Tree tr;
	tr = dp.GetFormat(FPB_ALL, FOB_ALL, true, true);
	tr.Root.Line.Color.nVal = 20724986;	// red
	tr.Root.Line.Connect.nVal = 4;	//B-Spline
	tr.Root.Line.Width.dVal = 2;
	dp.ApplyFormat(tr, true, true); 
	
	gl.XAxis.Scale.IncrementBy.nVal = 0;
	gl.XAxis.Scale.Value.dVal = 1;
	gl.XAxis.Scale.RescaleMargin.dVal = 0;
	tr = gl.XAxis.GetFormat(FPB_ALL, FOB_ALL, true, true);
	tr.Root.Labels.BottomLabels.Type.nVal = 3;
	tr.Root.Labels.BottomLabels.DateFormat.nVal = 2;
	gl.XAxis.ApplyFormat(tr, true, true); 
	gl.Rescale();
	gp.LT_execute("page.aa=1;");
	
	return gp;
}

static void ExportGraph(const GraphPage &gp) {
	string strGraphType = "png";  
	string strFileName = get_date_str(GetToday(), LDF_ALPHAMONTH_NUMERICDAY) + "~" + get_date_str(GetToday()+2, LDF_ALPHAMONTH_NUMERICDAY);
	string strGraphPath = okutil_get_origin_path(ORIGIN_PATH_USER_APPDATA);	
	strGraphPath = strGraphPath.Left(strGraphPath.Find("AppData")) + "Desktop\\";
	strGraphPath += strFileName + "." + strGraphType;
	export_page(gp, strGraphPath, strGraphType);  
}

The LabTalk script to call the Origin C function is straight forward:

[Main]
run.loadoc(<FileName>, 16);
ProcessTest_NOAA();

Save this script to a file, say, ScheduledTask.OGS in your User Files Folder (UFF).

2) Start the Task Scheduler
Run the Window “Task Scheduler“. You can find this tool in the Control Panel:
Control Panel> System and Security> Administrative Tools> Task Scheduler     

3) Create a Basic Task
Select “Create Basic Task…” in the right side panel. You move to the “Create Basic Task” page. Here, enter your task’s name and the description like the screenshot below. Press “Next“. (You move to the “Task Trigger” page.)

4) Set the Type of Schedule
In the “Task Trigger” page, choose the type of event such as Daily, One Time, etc. Press “Next“. (You move to the type specific page like “Daily”.)
5) Set the Date and Time to Run
In the task specific page, set the date/time for the event. In this sample, we set it as 8:00 AM everyday. Press “Next“. (You move to the “Action” page.)


6) Select “Start a Program” as an Action
In the “Action” page, choose “Start a program“, and press “Next“. (You move to the “Start a Program” page.)

7) Set the Origin Program and the LabTalk Script to Run
In the “Start a Program” page, in the “Program/script:” field, enter the command line to call Origin to run the specific LabTalk script file. In our case, we enter:

"C:\Program Files\OriginLab\Origin2019\Origin96_64.exe" -rs run.section(ScheduledTask.OGS,main)

Here, C:\Program Files\OriginLab\Origin2019\ is the program folder path where Origin is installed. Your path may be different.  To check your path, choose “Help: Open Folder: Program Folder” menu.  Origin96_64.exe is the Origin executable program name. Note that different versions of Origin will have a different names, so you need to update this baed on your installed version.  ScheduledTask.OGS is the name of the LabTalk script file, and main is the script section name to run, which is the first line in the script file, that is “[Main]“.
Press “Next“. (A prompt would appear, and you click Yes. You move to the “Summary” page.)

8) Check and Complete
In the “Summary” page, check your input information, and if correct, press “Finish” button.  Otherwise, you can go back to previous pages by pressing “<Back” button.

Now your task is all set to run as scheduled!


Note:
If you want to run the task several times in a day, create multiple tasks.

About S Z

Male, single, alive.

View all posts by S Z →

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です