Skip to main content
Known Participant
March 19, 2022
Answered

APL with the fiirst letter (automatically). Do you know how?

  • March 19, 2022
  • 2 replies
  • 789 views

Hello folks!

 

I'm trying to create an APL that brings to me alone the first letter of each group automatically without have to format every time.

Attached is what I have and what I want to try to do (it is higlighted in red)

 

Regards

 

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

    I just finished the programming of this. You may contact me offline for the script, which will become part of ma FMjsxLib project.

    2 replies

    K.Daube
    Community Expert
    K.DaubeCommunity ExpertCorrect answer
    Community Expert
    March 22, 2022

    I just finished the programming of this. You may contact me offline for the script, which will become part of ma FMjsxLib project.

    frameexpert
    Community Expert
    Community Expert
    March 22, 2022

    Which approach did you use?

    K.Daube
    Community Expert
    Community Expert
    March 22, 2022

    Rick, the core is this:

    KLD_Z.InsertGroupTitles = function (oDoc, sExcludes) { // ==================
    /*            Insert group title character in (alphabetic) lists similar to indexes
    Arguments     oDoc            Current document
                  sExcludes       List of characters which can be the first in a paragraph, but shall be skipped.
                                  An example are start apostrophes «‹“„
    Returns       true for success, false in case of an error
    Calling       ExecuteFCodes, GetTheText, Message
    Comment       • The process starts with the paragraph of the current location.
                  • Any change of the first character (Or second, in case the first is an exlude)
                    triggers the insertion of a paragraph with format GroupTitlesIX.
                    If this is not in the catalogue, it will be created.
                  • The process halts either at the end of the document or if a paragraph starts
                    with the special character "optional hypehn", x04
    History       2022-03-22
    */
    var msg_01, CRLF="\x0A" , oPgf, oPgfID, oPgfFmt, oPgfProps, oTL1, oTL2, oTR,
        sBegin = "\xFF", sChar, sFirst, sParaFormat = "GroupTitlesIX", sTermination = "\x04";
      msg_01 ="Current location is not in a paragraph."
      msg_02 ="Paragraph format for the group titles (GroupTitlesIX) is not in catalgoue → it has been created.\nYou will need to define the format (font, size etc.) properly."
    
    function GetFirst2Chars (oDoc, oPgf, sExcludes) {
    var oTL1 = new TextLoc(), oTL2 = new TextLoc(), oTR, sChar, sFirst;
      oTL1.obj = oTL2.obj = oPgf;
      oTL1.offset = 0;
      oTL2.offset = 2;
      oTR = new TextRange (oTL1, oTL2)
      sFirst = KLD_Z.GetTheText(oDoc, oTR);           // first 2 chars
      sChar = sFirst.substr (0, 1);
      if (sExcludes.indexOf (sChar) < 0) {
        sFirst = sChar;
      } else {
        sFirst = sFirst.substr (1, 1);                // skip excluded char
      }
      return sFirst;
    } // --- End of GetFirst2Chars -------------------
    
      oPgf = oDoc.TextSelection.beg.obj;
      if (!oPgf.ObjectValid()) {
        KLD_Z.Message ("E", msg_01, "InsertGroupTitles");
        return false;
      }
      oPgfFmt = oDoc.GetNamedObject(Constants.FO_PgfFmt, sParaFormat);
      if (!oPgfFmt.ObjectValid()) {
        oPgfFmt = oDoc.GetNamedObject(Constants.FO_PgfFmt, "Body");
        oPgfProps = oPgf.GetProps();
        oPgfFmt = oDoc.NewNamedObject (Constants.FO_PgfFmt, sParaFormat); // define new ¶ format
        oPgfFmt.SetProps(oPgfProps);
        KLD_Z.Message ("I", msg_02, "InsertGroupTitles");
      }
      sFirst = GetFirst2Chars (oDoc, oPgf, sExcludes);// --- Get first char in this paragraph not excluded
    
      while (sFirst != sTermination) {
        if (sBegin != sFirst) {                       // ---  insert pargraph with grouptitle
          oTL1 = new TextLoc (oPgf, 0);
          oDoc.AddText (oTL1, sFirst + CRLF);         // insert title line
          KLD_Z.ExecuteFCodes(["IPToPrevParaStart"]); // go back to inserted line
          oPgfProps = oPgfFmt.GetProps();
          oPgf.SetProps(oPgfProps);                   // apply ¶ format
          KLD_Z.ExecuteFCodes(["IPToNextParaStart"]); // skip the insertion
        }
        sBegin = sFirst;                              // --- sBegin is the new first character
        oPgf = oPgf.NextPgfInFlow;                    // --- Take next paragraph
        if (!oPgf.ObjectValid()) { break;}            // end of document
        sFirst = GetFirst2Chars (oDoc, oPgf, sExcludes);
      }
      return true;
    } // --- end InsertGroupTitles ---------------------------------------------------
    

     

    frameexpert
    Community Expert
    Community Expert
    March 19, 2022

    Only indexes have automatic letter headings like this. You could use an index, but then you would need an index marker containing every occurrence that you want to include. There are two ways you could use a script to automate this:

    1. Have a script navigate the documents in your book and add an index marker for each paragraphs of a certain format (or formats). Then you would generate an IX, which will give you the letter headings.
    2. Keep generating an APL and have a script that would post-process the APL and add the letter headings. A script like this could be triggered automatically whenever you generate the APL.

    Personally, I would prefer the second approach since you already have an APL and don't need to worry about markers.