ExtendScript of the Week: Keyboard Simulation in FrameMaker via Fcodes
Fcodes could be called as JIT automation for FrameMaker. I.e. with minimal effort, one could automate FM tasks with it. If you are a starter it could be very effective in automating some of the small tasks – in a breeze!
Fcodes are small programmatic commands/constants that can automatically simulate user input within FrameMaker. It is relatively (very) easier to automate small tasks via Fcodes than using the real API functions. It’s not that I am proposing ‘not’ to use API functions, but it is just an alternative for starters who would like to automate ‘small tasks in small time’. Fcodes are not new, they exist since very early versions of FM. With ExtendScript, they have become a little bit easier to use.
Fcodes are actually hexadecimal codes that specify individual user actions in FM. There are 1000+ Fcodes available with FM that a user may use. The entire list of Fcodes can be found in <FDK installation>\include\fcodes.h
(and FDK can be downloaded from http://www.adobe.com/devnet/framemaker.html).
There are 2 ways to use Fcodes:
- via Extendscript
- or via FDK Client.
We can use the Fcodes in FM Extendscript (.jsx) or the FM API (.dll). The function Fcodes()
in Extendscript / F_ApiFcodes()
in API respectively can be used to execute fcodes command. The fcodes command works like this: it sends an array of Fcodes to FrameMaker which is executed in FM, one by one (as if the user performed the action manually).
The example below describes some extremely simple code on how to create ExtendScript-based Fcode clients for simulating user actions in FrameMaker.
Simple steps:
- In FM10, File -> Script -> New Script (script editor opens)
- copy paste just few lines of code from e.g.1 or 2.
- Simply click Execute or F5.
Example 1: Writing Text in the Document via script
Fcodes can be used to simply write in a document or a dialog automatically. The 2-line example below shows how easily one could write in the document via Fcodes in ExtendScript.
FcodeList = new Array(FCodes.CSR_TOP, 'HI!', Fcodes.HIGH_WORD_PREV, FCodes.TXT_BOLD); //Fcodes
Fcodes(FcodeList); // Fcode Execution
Example 2: Add Misspelled words (Squigglied words) to User Dictionary via script
We have a scenario where we have written a document that has been checked for spellings but still has some keywords that show as misspelled. The user would like to learn all those words (add them to user dictionary). By executing an Fcode we may add all those keywords to our user dictionary. Here’s how to do it (this example adds the first misspelled word to the user dictionary):
FcodeList_Pers = new Array(FCodes.KBD_CHECKDOC, FCodes.KBD_ADDUSRDICT); //Fcodes
for (i=1; i<=1; i++) // Loop -> 1 could be made n
{
Fcodes(FcodeList_Pers); //Execute Fcode
}
Example 3: Setting custom value in Paragraph Line Spacing dialog via script
We have a scenario where we need to set the paragraph line spacing to a particular value. We just need to traverse to the paragraph(s) and execute this Fcode. Important: This type of script that involves dialog focus needs to be run from within FM only. (via File -> Script -> Catalog/Run [a saved script])
FcodeList = new Array(FCodes.PGF_LINE_SPACE, FCodes.START_DIALOG, FCodes.KBD_ItemNextLogical, '4','0', FCodes.KBD_RETURN, FCodes.END_DIALOG); // Fcodes
Fcodes(FcodeList); // FCodes Execution
Description of the Fcodes as used in Example 3:
FCodes.PGF_LINE_SPACE
opens “Paragraph Line Spacing” dialogFCodes.START_DIALOG
denotes the dialog is in useFCodes.KBD_ItemNextLogical
denotes SINGLE TAB in the dialog (i.e. pressing a TAB key in dialog takes the focus to the text field)'4','0'
writes the value 40 instead of 42. (Since the text field 42 is already selected after pressing the tab)FCodes.KBD_RETURN
performs the “Set” (also closes the dialog)FCodes.END_DIALOG
denotes the dialog is not in use nowFcodes(FcodeList)
statement executes the above sequence of Fcodes
With this information and examples, we should be able to write simple snippets for automating our tasks in FM. For reference, the entire list of Fcodes is being attached.
Important Points
- Do a manual traversal of the workflow before executing Fcodes.
- Each Fcode need to be preceded by “FCodes.”
- Any Fcode client during automatic execution needs to gain the focus so it is imperative we stop the mouse & keyboard activity when the Fcode based client runs.
- The .jsx file containing Fcodes on dialogs should be executed from the “Script Explorer” ONLY
- When a dialog launches another dialog use
END_DIALOG
for previousSTART_DIALOG
for the newly launched dialog. - There is an existing issue with Fcodes not working properly over the Book (Bug# 3099221)
Do let us know your story with Fcodes for FrameMaker!