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

Getting to the previous paragraph

Community Beginner ,
Oct 13, 2020 Oct 13, 2020

Copy link to clipboard

Copied

Hello there,

what I want to do is to iterate through all frames with pictures in my doc. While going through I want to save the file name of the picture in a variable, then delete the frame with the picture and append the file name of the picture to the end of the paragraph that was above the frame with the picture.

What I got so far is this: 

 

var doc= app.ActiveDoc;
var count = 0;
var graphicPath;

CountGraphics(doc);
DeleteGraphics(doc);

//COUNT GRAPHICS
function CountGraphics(doc)
{ 
     var graphic = doc.FirstGraphicInDoc;
     while (graphic) {
        
         if (graphic.type == Constants.FO_Inset) {
             count++;
             }
         graphic = graphic.NextGraphicInDoc;
      }
 }

//DELETE GRAPHICS + FRAMES
function DeleteGraphics(doc)
{
    for(i = 0; i < count; i++) {
        
     var graphic = doc.FirstGraphicInDoc;
     while (graphic) {
        
         if (graphic.type == Constants.FO_Inset) {
             
             graphicPath = graphic.InsetFile;
    
             graphic.FrameParent.Delete();

             }

         graphic = graphic.NextGraphicInDoc;
      }
   }
 }

 This works so far for the saving file name and deleting of the frames. 

What I am stuck on is the part to select the previous paragraph and go to the end of it to write the file name of the graphic to it. 

 

So like within the the loop I want to catch the previous paragraph of the frame I am at at the moment. 

Thought about something like  "graphic.FrameParent.PrevPgfInFlow" or sth... 

 

Thanks in advance for dealing with my stuff ^^°

 

 

TOPICS
Scripting

Views

244

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 ,
Oct 13, 2020 Oct 13, 2020

Copy link to clipboard

Copied

I am pressed for time but I will give you this:

// Store the anchored frame's paragraph object.

pgf = frame.TextLoc.obj;

 

// Get the previous paragraph in the flow.

pgf = pgf.PrevPgfInFlow;

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 ,
Oct 13, 2020 Oct 13, 2020

Copy link to clipboard

Copied

Assuming that "frame" is your anchored frame variable. It may be "graphic" or whatever else you specified.

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 Beginner ,
Oct 14, 2020 Oct 14, 2020

Copy link to clipboard

Copied

Hi again.

I did some good progress with your help. I can now applay the graphic Path to the previous paragraph. However I am now trying to figure out how to apply it to the END of the paragraph. I know there is something like pgf.end/beg.

My solution so far:

 

 

function DeleteGraphics(doc)
{
    var pgf;
    var tl;
    
    for(i = 0; i < countG; i++) {
        
     var graphic = doc.FirstGraphicInDoc;
     while (graphic) {
        
         if (graphic.type == Constants.FO_Inset) {
             
             graphicPath = graphic.InsetFile;
             pgf = graphic.FrameParent.TextLoc.obj.PrevPgfInFlow;
             tl = new TextLoc(pgf, 0);
             doc.AddText(tl, "[GRAPHIC]: " + graphicPath);
             
             graphic.FrameParent.Delete();

             }
         
         graphic = graphic.NextGraphicInDoc;
         
      }
   }
 }

I don't know how to apply the END to the textloc. 

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 ,
Oct 13, 2020 Oct 13, 2020

Copy link to clipboard

Copied

By the way, great job in separating your tasks into functions.

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 ,
Oct 14, 2020 Oct 14, 2020

Copy link to clipboard

Copied

Hi Rick,


I know that you like functions. However, isn't the first function superfluous?

You can omit the FOR in the second function. And you have the WHILE and the IF anyway.

Just out of curiosity so that I know what I can improve in my scripts.


Best regards


Winfried

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 Beginner ,
Oct 14, 2020 Oct 14, 2020

Copy link to clipboard

Copied

Hi Rick,

 

I am not that happy with the functions either. But I tried to leave out the FOR and / or the WHILE in the second loop.

But then it only loops for one time (didn't really figure out why yet...). So I made a function to count the graphics first so that I can loop through x-times with a FOR loop. 

Its working this way, but I am trying to find a more elegant way. But couldn't come up with one yet. 

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 Beginner ,
Oct 14, 2020 Oct 14, 2020

Copy link to clipboard

Copied

Sorry, I meant Winfried... (cant update my post?) 

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
Enthusiast ,
Oct 14, 2020 Oct 14, 2020

Copy link to clipboard

Copied

LATEST

I would go a different way:

Remember: there can be more than one graphic in an frame.

That can be the reason, why your second loop doesn't work, because when you delete a frame with multiple graphics in it then NextGraphicInDoc won't work.

 

My way would be: gather all Frames with Insets in an array. Then delete these frames.

If one Frame contains multiple graphics I would gather their names in a string.

 

    var oDoc, oGraphic,  gaFrames=[];  
    var oGraphicObject;
    
    oDoc = app.ActiveDoc;  
    
    if (oDoc.ObjectValid())
        {
        GatherGraphics(oDoc);
        
        DeleteGraphics(oDoc);
        }
    else 
        {
        alert("No active Document");    
        }

function GatherGraphics(doc)
{ 
     var graphic = doc.FirstGraphicInDoc;
     
     while (graphic) 
        {
         if (graphic.type == Constants.FO_Inset)
            {
             gaFrames.push(graphic.FrameParent);
             }
         graphic = graphic.NextGraphicInDoc;
      }
 }

function DeleteGraphics(doc)
{
    var pgf, sText="";
    
    for(i = 0; i < gaFrames.length; i++)
        {
        sText = CreateText(gaFrames[i]);
        
        pgf = gaFrames[i].TextLoc.obj.PrevPgfInFlow;
        tl = new TextLoc(pgf, Constants.FV_OBJ_END_OFFSET-1);

        doc.AddText(tl, "[GRAPHIC]: " + sText);
        
        gaFrames[i].Delete();
        }
}

function CreateText(fFrame)
{
    var graphic = fFrame.FirstGraphicInFrame;
    var sText = "";
    var aText = [];
    
    // if more than 1 Inset in 1 Frame
    while (graphic.ObjectValid()) 
        {
         aText.push(graphic.InsetFile);
         graphic = graphic.NextGraphicInFrame;
        }
    
    return sText = aText.join("--");    
}

 

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