22.214.171.124 From Graphical Objects
Graphic Objects (text, lines and shapes) can be tied to events and contain script that runs on those events or by using the run method for those objects. Since graphical objects are attached to a page, they are saved in Templates, Window files and Project files.
Some of your scripts may be used so often that you would like to automate their execution by assigning one or more of them to a button on the Origin project's graphical-user interface (GUI). To do so, follow the steps below:
From a new open Origin project:
- Select the text tool from the tool menu on the left side of the project window -->
- Now click in the open space to the right of the two empty columns of the blank worksheet in the Book1 window. This will open a text box. Type "Hello" in the text box and click outside the box--you have now created a label for the button.
- Now hold down the ALT key while double-clicking on the text that you just created. A window called Programming Control will appear.
- Not that there is an Object Name in the upper left of the dialog. Change this to Greeting.
- In the lower text box of the Programming Control window, enter our script:
type -b "Hello World";
- Also in the Programming Control window, in the Script, Run After box, select Button Up, and click OK.
- You have now created a button that, when pressed, executes your script and prints "Hello World" in a pop-up window.
Unlike a text script which exists only in the Classic Script Window, this button and the script it runs will be saved when you save your Origin project.
The script behind any graphic object an be run using its name and the run method. Open the Command Window and type:
and press Enter.
Here is a script that creates a vertical line on a graph that can be moved to report the interpolated value of your data at the X position represented by the line:
// Create a vertical line on our graph
draw -n MyCursor -l -v $(x1+(x2-x1)/2);
MyCursor.HMOVE = 1; // Allow horizontal movement
MyCursor.color = color(orange); // Change color to orange
MyCursor.linewidth = 3; // Make line thicker
// Add a label to the graph
label -sl -a $(MyCursor.x) $(Y2+0.05*(Y2-Y1)) -n MyLabel $(%C(MyCursor.x));
// Assign a script to the line ..
MyCursor.script$="MyLabel.x = MyCursor.x;
MyLabel.y = Y2 + MyLabel.dy;
// .. and make the script run after the line is moved
MyCursor.Script = 2;
Any Graphical Object (text, lines and shapes) can have an attached script that runs when an event occurs.
In this example, a rectangle (named RECT) on a graph is set to have a script run when the rectangle is either Moved or Sized.
- Use the Rectangle tool on the Tools toolbar to draw a rectangle on a graph.
- Use the Back(data) tool on the Object Edit toolbar to push the rectangle behind the data.
- Hold down the Alt key and double-click on the rectangle to open Programming Control. Note that the objects name is Rect.
- Enter the following script:
%B = %C;
%A = xof(%B);
dsRect = ((%A >= rect.x1) && (%A <= rect.x2) &&
(%B >= rect.y3) && (%B <= rect.y1))?%B:0/0;
type -a Mean of $(stats.mean);
- Choose the Moved or Sized event in the Script, Run After drop down list.
- Click OK.
When you Move or Resize this rectangle, the script calculates the mean of all the points within the rectangle and types the result to the Script Window. You can also define a Graphic Object range and run the script like:
GObject gobj = [Graph1]1!rect;