• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
2

Paste without format - preserve target ¶ format

Community Expert ,
May 30, 2022 May 30, 2022

Copy link to clipboard

Copied

Lin Sims asked for an option for Paste: Preserve the target formatting.

I want to implement this in the script FMfindRepl. So I developed UnformatClipBoard. I needed to circumvent the following problems:

  • After pasting there is no method to find the length of the pasted text range.I need to know this to apply the correct target format.
  • As it turns out that there are no properties defined for the clipboard.
  • I also have not found a method to apply the default paragraph format other than using FCodes.

Details for the function listed hereafter:

  • The copied selection must not contain more than one paragraph. Hence the selection must not contain an end-of-paragraph mark.
  • It is not 'looked into' objects. Hence any formatting inside a variable (e.g. font change) or a table or anchored frame is not modified.

The screen shot also shows the result from Paste Special > Unicode Text

 

UnformatClipboard.png

 

Two additional functions are required: ExecuteFCodes → Message .

Edit 2022-06-08: Function corrected (applying a ¶ format is not just setting the new name...)

 

KLD_Z.UnformatClipboard = function (oDoc, oPgf) { // =========================
// Calling       ExecuteFCodes (which calles Message)
// History       2022-06-08 Corrected: apply ¶ format is not just the name...
ApplyPgfFmt = function (oDoc, oPgf, sFormat) {
var oProps;
  oPgfFmt = oDoc.GetNamedPgfFmt(sFormat);
  if (oPgfFmt.ObjectValid()) {
    oProps = oPgfFmt.GetProps();
    oPgf.SetProps(oProps);
  } else {
    oPgf.Name = sFormat;
  }
} //--- end ApplyPgfFmt ---------------------
var oDoc, oPage, sPgfFmt, oLocation = {}, oPgfLocal, oTFr, oTL, oTR, MM = 185771;

  oDoc = app.ActiveDoc;
  if (app.Displaying == true) {                   // Avoid flicker
    app.Displaying = false;
  }
  oLocation.pgf      = oDoc.TextSelection.beg.obj;// Save current loc
  oLocation.txtRange = oDoc.TextSelection;
  oLocation.page     = oDoc.CurrentPage;
  sPgfFmt            = oPgf.Name;                 // Name of target paragraph format
// --- begin core function -----------------------
  oPage = oDoc.FirstMasterPageInDoc;
  oTFr = oDoc.NewTextFrame (oPage.PageFrame); // temporary text frame
  oTFr.Width  = 150 * MM;
  oTFr.Height =  50 * MM;
  oPgfLocal = oTFr.FirstPgf;
  oTR = new TextRange;
  oTR.end.obj = oTR.beg.obj = oPgfLocal;
  oTR.end.offset = oTR.beg.offset = 0;
  oDoc.TextSelection = (oPgfLocal,oTR);
  oDoc.Paste(0);                            // Paste the clip board
  oTR.beg.obj = oPgfLocal;¶
  oTR.beg.offset = 0;
  oTR.end.obj = oPgfLocal;
  oTR.end.offset = Constants.FV_OBJ_END_OFFSET;
  oDoc.TextSelection = oTR;
  KLD_Z.ExecuteFCodes (["SelectAll","CharacterDefaultPgfFont"]);
  ApplyPgfFmt (oDoc, oPgfLocal, sPgfFmt);  // Complete format must be applied
  oDoc.Copy (0);                           //  not interactive
  oTFr.Delete ();                          // Delete the helper text frame
// --- end core function -------------------------
  oDoc.CurrentPage = oLocation.page; // Restore current location
  oPgf = oLocation.pgf;
  if (oPgf.ObjectValid () === 1) {
    oDoc.TextSelection =   oLocation.txtRange;
  }
  oDoc.ScrollToText(oLocation.txtRange);
  if (app.Displaying == false) { // Restore previous state
    app.Displaying = true;
    app.ActiveDoc.Redisplay();
  }
} // --- end UnformatClipboard ----------------------------------------

 

 

 

TOPICS
Scripting

Views

196

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 04, 2022 Jun 04, 2022

Klaus, that is seriously awesome.

Votes

Translate

Translate
Community Expert ,
May 31, 2022 May 31, 2022

Copy link to clipboard

Copied

As additional background, my want for this comes mostly from needing to replace text strings with variables. There's no paste option for "Variable of Name" so you have to use the clipboard, but it brings along whatever the underlying format was from where you copied it. However, the same annoyance applies to simply pasting text from the clipboard.

 

Really, when you get down to it, why is it possible to search for things like Variable of Name but not paste them?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 04, 2022 Jun 04, 2022

Copy link to clipboard

Copied

Lin, I'm on the way...

It works already - but other functions in FMfindRepl must still be tested thoroughly...

Repl-Text-by-Variable.png

The availability of Options and Replace Modes depends on the selected Find Type (Text). The indicated radio button may get different labels - and if set, the drop-down list below is filled with items of the appropriate type.

Compared to the FM Find/Change panel I use radio buttons, because in a drop-down list I can not disable certain items IMHO it does not make sense to allow Regular Expressions for search of Anchored Frames...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 04, 2022 Jun 04, 2022

Copy link to clipboard

Copied

LATEST

Klaus, that is seriously awesome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines