Automated Solutions for FrameMaker..

Posts tagged ‘Forums’

Extendscript: Remove trailing spaces after variables in your document

Hello everyone:

I was going through a query on the Adobe FrameMaker user to user forums, found this interesting problem statement:

And I thought, why not try and solve this problem statement. Had fun solving it.
Sharing it for the benefit of the FrameMaker Extendscript community.
var doc = app.ActiveDoc
var variable = doc.FirstVarInDoc

while(variable.ObjectValid())
{
var tr1 = variable.TextRange; //text range of the variable

	/*text range for next possible char*/
    var tl1 = new TextLoc(tr1.end.obj, tr1.end.offset + 1);	
    var tl2 = new TextLoc(tr1.end.obj, tr1.end.offset + 2);	
    var tr2  = new TextRange (tl1, tl2)

	var textitems = doc.GetTextForRange (tr2, Constants.FTI_String);

	if (textitems.length > 0)
	{
		var str = textitems[0].sdata;
		if(str != " ") //if space then add text
		{
			 doc.AddText (tr2.beg, " ");        
		}        
	}
	variable = variable.NextVarInDoc;
}

Hope this solution nails the problem statement. It might also lead you to attack similar problems under different contexts.

Best Wishes:
framegurus
email:framegurus@gmail.com
Advertisements