Reflections on Pro/INTRALINK Scripting

Have you used Pro/INTRALINK scripting? Perhaps you've looked at the resulting Java source code and wondered what other commands are available? Disappointed at the lack of detailed documentation? Don't wait for PTC to tell you what's there—have Java do it for you.

Using the Reflection package, Java can tell you a lot about the classes and methods available at runtime. This standard package is often used in web services applications and other situations where code is called dynamically, such as creating a scripting API to a Java program.

To use the Reflection package, the starting point is an empty Pro/INTRALINK script.

 

Recording an Empty Script

1. To get to the Scripting Options dialog from within the Pro/INTRALINK client, hit Ctrl+S. This is available without having to login. 

2. Enter a file name on the Recording tab (“empty.java” in this example).




3. To create a minimal script, hit the Start button and then the Stop button. You should get a message dialog stating that the compilation was successful.



4. The resulting empty.java file (found in the .proi/.data/user.data/source folder) contains more or less the following code:

 

import com.ptc.intralink.client.script.*;

import com.ptc.intralink.script.*;

 

public class empty extends ILIntralinkScript {

  ILIntralinkScriptInterface IL =

    (ILIntralinkScriptInterface)getScriptInterface();

 

  private void run0 () throws Exception {

  } // End of run0

 

  public void run () throws Exception {

    run0 (); // recorded

  } // End of function

 

} // End Macro Recording

 

Editing the Script

1. To have Java report some information using the Reflection package, we'll use a text editor to add some import statements and code to the empty run0 method. Note: From my understanding, PTC does not support manually changing the Java code of a script or the resulting program, so keep this in mind before contacting the PTC Technical Support hotline.

2. After the edits, the resulting code will look like this (added code shown in bold).

 

import com.ptc.intralink.client.script.*;

import com.ptc.intralink.script.*;

 

import java.lang.reflect.*;

import java.io.*;

 

public class empty extends ILIntralinkScript {

  ILIntralinkScriptInterface IL =

    (ILIntralinkScriptInterface)getScriptInterface();

 

  private void run0 () throws Exception {

 

    Class c = IL.getClass();

    Method m[] = c.getDeclaredMethods();

 

    FileWriter fw =

      new FileWriter("IL_script_methods.txt");

    String newline = System.getProperty("line.separator");

 

    for (int i = 0; i < m.length; i++) {

      fw.write(m[i].toString() + newline);

    }

    fw.close();

 

  } // End of run0

  public void run () throws Exception {

    run0 (); // recorded

  } // End of function

} // End Macro Recording

 

The first additional import statement (import java.lang.reflect.*;) makes the Reflection functionality available, and the second (import java.io.*;) allows you to use the FileWriter class to output the data to a file.

3. In the body of the run0 method, extract the class of the IL object, from which we get the available methods, which are put in the “m” array.

Class c = IL.getClass();

Method m[] = c.getDeclaredMethods();

4. Next, we set up the output file, which will be written in the Pro/INTRALINK session’s current working directory, and then figure out the line terminator for text files (which is different for Windows than for Unix).

    FileWriter fw = new FileWriter("IL_script_methods.txt");

    String newline = System.getProperty("line.separator");

5. Looping through the array members of “m,” the information for each method is written to the output file:

for (int i = 0; i < m.length; i++) {

fw.write(m[i].toString() + newline);

}

6. Finally, we close the output file handle so we don't run out of file descriptors.

             fw.close();

 

Recompiling the Script

1. Once the edits to empty.java are complete, we’ll recompile the script. From the Recompile tab in the Scripting Options dialog, hit the Recompile button.

 

 

2. If you typed everything right, you should get another compilation success dialog.

 

 

Running the Script

After a successful recompile, Pro/INTRALINK will shift the Scripting Options dialog to the Playback tab. When you hit the Playback button, the script will run without displaying any visual output.

 

 

Interpreting the Output

When you look at the output in the "IL_script_methods.txt" file, you see many of the methods that get used when you record a sequence of actions. Notice that some take arguments (String, int, etc.), while others take no arguments. The “void” keyword indicates that nothing is returned, which seems typical for these methods.

Sample of output file:

  public void closeWindow()

  public void setActiveWindow ( String, String, String )

  public void select ( String, int )

  public void select ( String, String )

  public void printContent ( String, int )

  public void ok()

 

Some methods, such as select(), are listed multiple times with different arguments. This is called overloading, where different methods can have the same name but take different arguments. Java knows which method should be used.

For both select() methods, the first argument is the type (i.e., “WSPI” for Workspace objects, "PIV" for Commonspace objects). The second argument is either the integer row number (starting with zero) from the active table display or the object name. The object name syntax is "ws1/abc.prt" for Workspace objects and "abc.prt/main/B/2" for Commonspace objects. 

The String argument version is used when the Use Key Recording? option is checked during the record phase. The int argument version is used when it is not checked. Using the row number can make things easier, i.e., in a Locate results display if you know there will only be one result. Otherwise, you need to know the name, branch, revision, and version to select a Commonspace object, and the Workspace name (as well as the object name) to select a Workspace object.

Interesting Methods for Selections and Attribute Values

While most are self-explanatory, some methods are a little different than the others. It's probably not possible to record the following methods directly, but they can be added manually to the Java code.

public String[] getSelectedObjects ( String )

  • Returns a String array of the objects that are selected. You can loop through the elements of the array to do something with each object individually (e.g., Where Used report, Check out, Check in, etc.). Examples for the String argument are "WSPI" for Workspace selections and "PIV" for Commonspace selections.

public Object getTableCellValue ( String, int, int )

  • Returns a value for a given cell in the active table display. This method seems to be available starting in Pro/INTRALINK 3.3.
  • As with getSelectedObjects(), the String argument is either "WSPI" for Workspace table displays or "PIV" for Commonspace table displays. The first int argument is the row number in the table display, and the second argument is the column number. Note that the column and row numbers are zero-based. The value for row #1 is 0 (zero), for row #2 is 1 (one), for row #3 is 2, etc. The same applies to the column numbers.
  • Since the Object returned by getTableCellValue() is essentially a String object, you can apply String methods to it, such as toString(), toUpperCase(), toLowerCase(), startsWith(), endsWith(), etc.

 

There's no easy way to figure out the total number of columns in the current display, or which attribute value is displayed in a particular column, unless you know this information ahead of time. This is limiting, but the method is still very useful in controlled situations where you use (or create) a specific table display configuration.

 

Example for Selections and Attribute Values

As an example, the following code will select all objects in the active Workspace window, loop through all of the selected objects, and print the object name and the attribute value in column #3 to "ws_output.txt" (in the Pro/INTRALINK client’s current working directory).

 

  IL.selectAll( "WSPI" );

  String objects[] = IL.getSelectedObjects( "WSPI" );

  FileWriter fw = new FileWriter("ws_output.txt");

  String newline = System.getProperty("line.separator");

  for (int i = 0; i < objects.length; i++) {

    Object tcv = IL.getTableCellValue("WSPI", i, 2);

    String str = tcv.toString();

    fw.write(objects[i] + "\t" + str + newline);

  }

  fw.close();

 

If you only have two columns displayed, the script will generate an error message. Keep in mind that if you repeatedly perform getSelectedObjects() on a large table display, you will eventually consume a lot of your system’s RAM.

 

Interesting Methods for Printing

The next group of methods applies to printing. PTC Tech Support provided some details on these when asked how to print to a folder outside of the .proi structure.

public void setReportAdapterDefaultPath ( String )

  • Controls the folder to which the print output will go.

public void setReportAdapterDefaultName ( String )

  • Controls the default file name used in the print dialog.

public void setReportAdapterDefaultState ( int )

  • Controls the options that would be selected in the print dialog box (e.g., HTML, Append, etc.).

Once you have executed any of these methods, the settings become the default for future printing in that Pro/INTRALINK session. 

Only the DefaultPath method is really useful, since the printContent() method already allows you to specify the output filename, the type of data, and whether it creates a new file or appends to an existing one. Use the DefaultPath method to direct your output to a folder outside of the .proi folder structure.

 

Conclusions

Gaining a better understanding all of the available methods is critical in building more complex and efficient applications. The undocumented methods allow applications to adapt dynamically to the Pro/INTRALINK metadata and change their behavior in an intelligent manner. Although PTC will provide little, if any, support for customized Pro/INTRALINK scripts, the advantages to those who require automation far outweigh any uncertainty about their unsupported nature.

 

Marc Mettes can be reached by email at marcs_mailbox@yahoo.com.

PTC/USER 2005: Better than Ever

Keeping the Connection

Pro/INTRALINK Meets Windchill in Release 8.0

Creating a Cylindrical Cam Using Mechanism Design Extension

Pro/ENGINEER Professionals: Test Your Expertise

Quickly Replacing Components that Assembly Many Times

Reflections on Pro/INTRALINK Scripting

Dimension Justification Made Easy

Using Axis Pattern in Wildfire 2.0