Skip to main content
K.Daube
Community Expert
Community Expert
August 25, 2021
Answered

Text Range not working as expected

  • August 25, 2021
  • 2 replies
  • 248 views

Dear all,

I want to use the automatic numbering for paragraphs to convert an arbitrary integer into a string. The automatic numbering provides a great range of numbering systems (numeric, roman, ...)

In the following script the text range does not behave - although I have used this method in other scripts with success.

 

 

//@target framemaker
var KLD_Z = KLD_Z || {};                          // global script object
//@include ..\GetText.jsx

KLD_Z.main = function () { // <><><><><><><><><><><><><><><><><><><><><><>
$.bp(true);
var oDoc = app.ActiveDoc, oTL1, oTL2, oTR, sText, oMasterPage, oTxtFrame,
    oPgf, PT = 65536;

  oMasterPage = oDoc.FirstMasterPageInDoc; // Add a temporary text frame
  oTxtFrame = oDoc.NewTextFrame (oMasterPage.PageFrame);
  oTxtFrame.Width  = 400 * PT;
  oPgf = oTxtFrame.FirstPgf;
  oPgf.PgfIsAutoNum = 1;        // set the value and the numbering format
//oPgf.AutoNumString = "<Indic n=1234>";
  oPgf.AutoNumString = "<r=1234>";
  oTL1 = new TextLoc (oPgf, 0);
  oTL2 = new TextLoc (oPgf, Constants.FV_OBJ_END_OFFSET - 1); // exclude ¶
  oTR = new TextRange (oTL1, oTL2);
  oDoc.TextSelection = oTR;              // → does not select anything
  sText = KLD_Z.GetText (oDoc, oTR);     // → empty
  oTxtFrame.Delete ();                   // Delete the temp. text frame
  return sText;
} //--- end main --------------------------------------------------------

$.writeln("→ " + KLD_Z.main ());                  // → empty

 

 

This topic has been closed for replies.
Correct answer K.Daube

Following my idea with the MIF file I have this script:

KLD_Z.EvalAutoNum = function (sAutoNumFmt, iValue) { // =====================
/*            Format an integer according to an auto-numbering format
Arguments     sAutoNum        Auto-number string to be evaluated, e.g. "Indic n"
              iValue          Integer to evaluated
Calling       ReadDataFile (which uses GetSimpleOpenProps)
History       2021-08-16
*/
var aMifLines = [], index1, index2, j, nLines, oDoc, oFlow, oPage, oPgf, oTFr, 
    saveParms, saveRetParms, sFilename, sLine, sTempPath, sText, sValue, 
    CM = 1857713;

// --- Creante new document and save with name (not visible)
  sTempPath  = app.TmpDir;
  sFilename = sTempPath + "\\z-miftour";
// (w, h, nC, cGap, topM, botM, leftM, rightM, sidedness, makeVisible)
  oDoc = CustomDoc(21*CM, 21*CM, 1, 0, 1*CM, 1*CM, 2*CM, 2*CM, 0, false);
  saveParms = GetSaveDefaultParams();   // Get the save parameters and set the file type
  saveRetParms = new PropVals();
  j = GetPropIndex (saveParms, Constants.FS_FileType);
  saveParms[j].propVal.ival = Constants.FV_SaveFmtBinary;
  oDoc.Save (sFilename + ".fm", saveParms, saveRetParms);

// --- Modify the first paragraph to Autonumbering, e.g. <Indic n=1234>
  sValue = "<" + sAutoNumFmt + "=" + iValue + ">";
  oPgf  = oDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
  oPgf.PgfIsAutoNum = 1;
  oPgf.AutoNumString = sValue;

// --- Save the document as mif
  saveRetParms = new PropVals();
  j = GetPropIndex (saveParms, Constants.FS_FileType);
  saveParms[j].propVal.ival = Constants.FV_SaveFmtInterchange;
  oDoc.Save (sFilename + ".mif", saveParms, saveRetParms);

// --- Read MIF file, line containing "<PgfNumString `١٢٣٤'>"
  KLD_Z.ReadDataFile (sFilename + ".mif", aMifLines);
  nLines = aMifLines.length;
  for (j = 0; j < nLines; j++) {
    if (aMifLines[j].indexOf("<PgfNumString") <= 0) continue;
    sLine = aMifLines[j];
    break;
  }

// --- Extract what is need for further processing (١٢٣٤)
  index1 = sLine.indexOf (" `");
  index2 = sLine.indexOf ("'>");
  sText  = sLine.substring (index1, index2);
// --- Delete both files - not necessary, are overwritten next time
// --- Return the extracted value
  return sText;
} //--- end EvalAutoNum -----------------------------------------------------

I have tested it with the formats "r", "indic n" and "kanji n". Of couse the target paragraph requires an appropriate font the last one - for the others TNR is sufficient.

2 replies

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthorCorrect answer
Community Expert
August 27, 2021

Following my idea with the MIF file I have this script:

KLD_Z.EvalAutoNum = function (sAutoNumFmt, iValue) { // =====================
/*            Format an integer according to an auto-numbering format
Arguments     sAutoNum        Auto-number string to be evaluated, e.g. "Indic n"
              iValue          Integer to evaluated
Calling       ReadDataFile (which uses GetSimpleOpenProps)
History       2021-08-16
*/
var aMifLines = [], index1, index2, j, nLines, oDoc, oFlow, oPage, oPgf, oTFr, 
    saveParms, saveRetParms, sFilename, sLine, sTempPath, sText, sValue, 
    CM = 1857713;

// --- Creante new document and save with name (not visible)
  sTempPath  = app.TmpDir;
  sFilename = sTempPath + "\\z-miftour";
// (w, h, nC, cGap, topM, botM, leftM, rightM, sidedness, makeVisible)
  oDoc = CustomDoc(21*CM, 21*CM, 1, 0, 1*CM, 1*CM, 2*CM, 2*CM, 0, false);
  saveParms = GetSaveDefaultParams();   // Get the save parameters and set the file type
  saveRetParms = new PropVals();
  j = GetPropIndex (saveParms, Constants.FS_FileType);
  saveParms[j].propVal.ival = Constants.FV_SaveFmtBinary;
  oDoc.Save (sFilename + ".fm", saveParms, saveRetParms);

// --- Modify the first paragraph to Autonumbering, e.g. <Indic n=1234>
  sValue = "<" + sAutoNumFmt + "=" + iValue + ">";
  oPgf  = oDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
  oPgf.PgfIsAutoNum = 1;
  oPgf.AutoNumString = sValue;

// --- Save the document as mif
  saveRetParms = new PropVals();
  j = GetPropIndex (saveParms, Constants.FS_FileType);
  saveParms[j].propVal.ival = Constants.FV_SaveFmtInterchange;
  oDoc.Save (sFilename + ".mif", saveParms, saveRetParms);

// --- Read MIF file, line containing "<PgfNumString `١٢٣٤'>"
  KLD_Z.ReadDataFile (sFilename + ".mif", aMifLines);
  nLines = aMifLines.length;
  for (j = 0; j < nLines; j++) {
    if (aMifLines[j].indexOf("<PgfNumString") <= 0) continue;
    sLine = aMifLines[j];
    break;
  }

// --- Extract what is need for further processing (١٢٣٤)
  index1 = sLine.indexOf (" `");
  index2 = sLine.indexOf ("'>");
  sText  = sLine.substring (index1, index2);
// --- Delete both files - not necessary, are overwritten next time
// --- Return the extracted value
  return sText;
} //--- end EvalAutoNum -----------------------------------------------------

I have tested it with the formats "r", "indic n" and "kanji n". Of couse the target paragraph requires an appropriate font the last one - for the others TNR is sufficient.

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
August 26, 2021

It turns out that...

  • The auto number can not be selected (neither with the UI, nor with the Text Range)
  • There is no pgf property PgfNumString, although it exists in MIF
  • There is no Constants.FTI_xxx to get the autonumber by means of GetTextForRange

So for t he time being I'm at the wall...

Edit

  • I'm currently developing a function along these lines
  • Creante new document (portrait
  • Modify the first paragraph to Autonumbering with, for example <Indic n=1234>
  • ave the document as z-miftour.fm
  • Save the document as ...mif
  • Read from the MIF file the line containing "<PgfNumString `١٢٣٤'>"
  • Extract what is need for further processing (١٢٣٤)
  • Delete both the fm and the mif file
  • Return the extracted value