How to create an App using an X-Function

Introduction

X-Function provides a framework for building Origin tools. Using X-Function, we can only focus on the function details and the code, and don’t have to be concerned about the tool’s GUI. The ease of use makes X-Function an attractive approach to creating Apps.

In this blog post, I will show you how to create an App using X-Function. I will present the process of creating an app and continuing to develop an app.

I will assume that you already know how to create an X-Function. But if you are unfamiliar with that, documentation is available online.

Getting Started

The first step in creating an app is to get the components of an app ready. The absolute necessary components of an Origin app include:

(1) Code files

(2) A 32 * 32 pixel PNG file for the App icon

Creating / Downloading the X-Function

In this blog, we will include an X-Function as a code file. This X-Function will perform the task of copying data from one column to another. We can:

  • Create the X-Function following the tutorial.

or

  • Download the zip file to achieve the X-Function directly.

Creating a Folder and Adding Files

Creating a Folder

The next step is to create a folder for the App.

(1) Open Code Builder, go to Workspace tab that is by default docked to the left side, you will see a folder named Apps.

(2) To create the folder for a new App, right-click on the Apps and select New, and then rename this new folder to the name of the App, for example, name it “My App”.

Adding Files

At this point, we can add the components mentioned above to the App folder.

(1) Right-click on “My App” folder and select Add Files…

(2) Browse to select the icon and the X-Function (OXF file), and click Open button to add them to the current folder.

Packaging and Installing an App

Packaging an App

Now we can package the App into an OPX file.

(1) Again in Workspace tab of Code Builder, right-click on the folder “My App” and select Generate… to open Package Manager.

(2) In Package Manager, edit the required fields as illustrated below. The most important field is Launch Script. That is a single-line LabTalk script that will run when the icon is clicked in the App Gallery. In this case, we will type the script CopyCol -d;

Note: “CopyCol” is the name of the X-Function, while “-d” is an X-Function execution option for bringing up the dialog of the X-Function.

(3) Once all of the fields have been properly filled in, we can select the menu File of Package Manager to save the OPX file.

For a more detailed look at Package Manager, see this page.

Installing the new App

Once saved, the OPX can be dragged into Origin to install the App. When first installed, there will be a prompt to replace the existing files. It is okay to select Replace All.

Upon installation, a new icon will be added to Apps Gallery. We can click the icon to launch the App.

Updating the App

Updating the X-Function

We will probably want to continue developing the App by adding or changing the code files.

Let’s suppose that we would like to add a new option to the dialog to sort the output column, and we are going to use X-Function Builder to improve the X-Function CopyCol.

(1) First, right-click on the App icon and select Show in Folder. That is the location where Origin stores the files of the App, so remember it.

(2) Open X-Function Builder, and click Open button and select the X-Function CopyCol from the App Folder mentioned above.

(3) Right-click on the Variables list and select Add Variable. Change the name, label, and other values of the new variable as illustrated below:

(4) Click Code Builder button and modify the code of main function to:

ocol = icol;
if(1 == sort)
    ocol.Sort();
else if(2 == sort)
    ocol.Sort(SORT_DESCENDING);

(5) After this, compile and return to X-Function Builder, then save the X-Function by clicking Save button.

Updating the OPX File

Now, we can update the OPX file with the latest changes.

(1) In Code Builder, right-click again on the App folder in the Workspace tab and select Generate….

(2) All the fields are already filled with the last-used values. Generally, we can just increment the version number and save the OPX file.

(3) After regenerating the opx file, we don’t have to reinstall the App. Origin will automatically help to change. We can just click the icon to launch the App and enjoy the new App.

Leave a Reply

Your email address will not be published. Required fields are marked *