OCLC Macro Language for the complete beginner!


by Joel Hahn

If you're intimidated by the thought of editing or even (gasp!) writing your own OCLC macros, here's one place to start to pick up some of the essentials of programming with OML. Writing your own macros can be quite rewarding and even--dare I say--fun, once you get a handle on a few basic concepts and commands.

(Disclaimer/note: For the purposes of teaching the rudiments, I may leave out some details that don't impact much on the concept at hand but might be very important for intermediate or advanced programming. You have to learn to add before you can learn that repeated adding is the same as multiplying.)

Ok. You've finally gotten up the gumption to press that ol' New Macro button. You're faced with a barrage of windows demanding immediate responses (but, luckily, explaining themselves fairly well) and then finally a mostly-empty screen.

  ' MacroName: [the name you entered in the Macros window]
  ' MacroDescription: [the brief description of what the macro does, which
  '                   you entered in one of the windows that appeared.]
  sub main

  end sub

Quickie translation:

"MacroName" and "MacroDescription" are self-explanatory. You can change them all you want from this screen, but that won't have any impact on what displays elsewhere in CatME (such as the Macro window or the Customize... window). There are ways to change the name and description, so don't panic if you don't like what you typed!

"Sub main" and "end sub" are the core of the macro. They tell the computer that everything between those two commands is the heart of the program. In essense, when the macro runs, it starts at "sub main" and runs until it hits "end sub," at which point it stops and the stopsign button on the main window goes blank again.

The first character in each of the first two lines is also important to learn right off the bat. A "remark" or "comment" is a note from the programmer to whomever reads the program next; REM is not just a rock group, it is a command that tells the computer to ignore everything following the REM on the current line. A synonym for REM is the single quote mark ('); that is what is most commonly used for remarks in OML. Also, the verb phrase "to comment out" means "place a ' or a REM at the front of the line"; you will often see this in macros with instructions on how to customize them for your own uses.


Here's a little trick that will make your life a lot easier when writing macros.

  Dim CS as Object
  CS = CreateObject("Catme.Application")

Translation:

1) "Create a short-hand method of referring to an object, and use 'CS' as the short-hand for that."
2) "CS is short-hand for CatME.Application (basically the part of the CatME program where the CatME-specific macro commands are stored) for the purposes of having a macro run certain CatME-specific commands."

Now, instead of typing out "CatME.Application" in commands like "CatME.Application.GetActiveRecord" or "CatME.Application.AddField" (don't worry, I'll explain how those commands work in a minute), you only have to type "CS.AddField."

(Other than that, it's not all that important at this point that you understand exactly how these commands work, as long as you grasp the basic concept of creating the short-hand so that "CS" works with all of the CatME-specific commands I'll be using in these lessons.)

CS.GetActiveRecord and CS.AddField (see how handy that is?) are commands you'll find yourself using a lot. CS.GetActiveRecord will probably be present in most macros you write for CatME; it checks whether there is a bibliographic record window open and active in CatME. For example, here is a macro that does nothing but check to see if there a bibliographic record window open in CatME; if there isn't one, it displays an error:

  sub main
    dim CS as object
    set CS = CreateObject("CatME.Application")
    CS.GetActiveRecord
  end sub

Before I go on to CS.AddField, one brief but vitally important side note. The "Field" in this and other commands refers to an entire MARC field (such as the entire contents--including MARC tag--of the 245 line). This is very different from the way Passport handles data, and bears a little explaining for those who are used to Passport macros. Passport operates as a "screen editor", and as such allows the cursor to be moved anywhere on the page, keeps track of where fields are split when they are too long to fit on a single line, and allows text to be freely entered anywhere. CatME may seem similar to a user, but it internally treats each MARC tag as a single line, regardless of how long the line is. In other words, Passport divides the display into rows and columns, but CatME only uses rows. Also, in Passport, lengthy MARC fields (such as contents notes) may take up several rows, but in CatME, each MARC field is considered to be a single row, even if it appears as multiple lines on the screen.

As you might think from the name, CS.AddField means "Add a field to the active bibliographic record." Here is a simple macro that adds a 949 line to the end of the active bibliographic record:

  sub main
    dim CS as object
    set CS = CreateObject("CatME.Application")
    CS.GetActiveRecord
    bool = CS.AddField(99, "949    Note goes here.")
  end sub

What this macro does is set up the "CS" shortcut, make sure there is an active bibliographic record to act on, and then insert a new row number 99 with the contents "949    Note goes here."; if all goes well, the "bool" variable will be TRUE (or 1), but if the line cannot be added for some reason, the "bool" variable will be FALSE (or 0). (Note that normally in OML, "TRUE" is defined as "not zero"; CatME.Application commands always use 1 for TRUE, but other OML commands may use -1 for TRUE. I'll explain more about variables and values in a later lesson.) Since OCLC limits the number of rows a record can have to 50, adding a field as row number 99 virtually guarantees that it will always be the last row in the record. (Note that as soon as the row is added, CatME renumbers it so that there are no skipped row numbers; if there were 10 rows in the record before the above macro was run, the new line would instantly become row number 11, even though you specified it to be row number 99.) If you want to insert a row between two existing rows, simply use CS.AddField and use the row number the new line will become. If you want to make the new row the first line in a record, use "CS.AddField(1, ..."; if you want it to go between row five and row six, use CS.AddField(6, ..."

Another very useful command to know right away is CS.RunMacro, which--as you probably figured out--tells the computer to run another macro. The second part of the command is the macrobook which contains the macro, an exclamation point, and then the name of the macro to run; all enclosed in quotes with no spaces.

  sub main
    dim CS as object
    set CS = CreateObject("CatME.Application")
    bool = CS.RunMacro("MyMacros!Add949")
  end sub

In this case, the computer goes into the "MyMacros" macrobook, and runs the "Add949" macro. This particular example is functionally the same (though slightly slower) as just running the Add949 macro itself. This may seem needlessly redundant right now, but with this command, you won't have to completely duplicate macros when you have two macros that do very similar things. For example, you could have one macro that adds a 949 field, a second that adds a 910 field, and for those cases where you need both, a single macro that runs each of those in turn. More uses of CS.RunMacro may become evident as you write more--and longer--macros.

Next time, variables...


Return to Main page.