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

Remove the parent textframe of the images

Participant ,
Nov 30, 2023 Nov 30, 2023

Copy link to clipboard

Copied

Hi,

 

Using the script to remove the parent image text frame, I was unable to remove the parent frame after executing the script. Please find attached the script. Could you please guide us on this matter?  

 

MonishaRajendran_0-1701342632928.png

 

 

var xOffset = 150;
var yOffset = 37.5;
var pageWidth = 480;
var pageHeight = 540;
var frameX = 60;
var frameY = 640;
var frameWidth = 50;
var frameHeight = 50;
var doc = app.activeDocument;
for (var i = 0; i < doc.pages.length; i++) {
  var page = doc.pages[i];
  var pageHeight = page.bounds[2] - page.bounds[0];
  var textframeheight = page.textFrames.length;
  var imageFrames = []; // Create an empty array to store image frames
  var imageNamesArray = [];
  // Loop through all text frames on the page
  for (var j = 0; j < textframeheight; j++) {
    var frame = page.textFrames[j];
  if (frame.associatedXMLElement && frame.associatedXMLElement.markupTag) {
      var markupTagName = frame.associatedXMLElement.markupTag.name;
else if (frame.associatedXMLElement.markupTag.name == 'category_name') {  
      var pagedetails = page.allPageItems.length;
      for (var n = 0; n < pagedetails; n++) {          
        if (page.allPageItems[n] instanceof Image) {
           var pageItem = page.allPageItems[n]; 
          var parentFrame = page.allPageItems[n].parent;
          var imageName = page.allPageItems[n].itemLink.name;
       var xmlString = ReadXMLFileDetails();
var xmlDoc = new XML(xmlString);
// Define the XPath expression
var imageTypeElement = xmlDoc.xpath("//image_type");

// Check if the image_type element exists
if (imageTypeElement != null && imageTypeElement != '') {
    // Extract values from the image_type element
    var imageNodes = imageTypeElement.children();
    var imageFiles = [];
    var splitImageFiles = [];
    var imageNamesArray = [];

    for (var img = 0; img < imageNodes.length(); img++) {
        var imageFileAttribute = imageNodes[img].attribute("IMAGE_FILE");
        if (imageFileAttribute != null && imageFileAttribute != '') {
            var imageFile = imageFileAttribute.toString();
            imageFiles.push(imageFile);
            splitImageFiles.push(imageFile.split(','));
        }
    }

    for (var spltimg = 0; spltimg < splitImageFiles.length; spltimg++) {
        if (splitImageFiles[spltimg].join(', ') != null && splitImageFiles[spltimg].join(', ') != '') {
            imageNamesArray.push(splitImageFiles[spltimg].join(', '));
        }
    }
} else {
    alert("Error: 'image_type' element not found in XML.");
}
           for (var k = 0; k < imageNamesArray.length; k++) {
//~   // Get the path to the image file
   var imagePath = FolderPath() + "/" + imageNamesArray[k];
  var activePage = app.activeWindow.activePage;
  var activePageNum = activePage.documentOffset;
   var currentFrameX = frameX + k * (frameWidth + 10);
   var newFrame = activePage.textFrames.add({geometricBounds: [frameY, currentFrameX, frameY + frameHeight, currentFrameX + frameWidth]});
   var placedItem = newFrame.place(File(imagePath));
  newFrame.fit(FitOptions.proportionally);
 }
for (var t = 0;t < page.allPageItems.length; t++) {
        if (page.allPageItems[t] instanceof Image) {
            var parentFrame = page.allPageItems[t].parent;
            parentFrame.remove();
        }
    }
        }
      }
    }
}

}
}

function ReadXMLFileDetails()
{
	
		myDoc = app.activeDocument;
		myLinks = myDoc.links;
		for (j = myLinks.length - 1; j >= 0; j--)
		{  
			myName = myLinks[j].filePath; 
			var ext =  myName.substring(myName.length-3,myName.length) ;
			if (ext=="xml")
			{   
				var myXMLFile = File(myName);  
				var myResult = myXMLFile.open("r", undefined, undefined); 
				if(myResult == true)
				{ 
				var myXMLDefaults = myXMLFile.read(); 
               // alert(myXMLDefaults)
				myXMLFile.close(); 
				var myXMLDefaults = new XML(myXMLDefaults); 
				return myXMLDefaults;
				}			
		}//for
	}	
}
function FolderPath()
{// Get the active document
var partBeforeImages
var doc = app.activeDocument;
for (var k = 0; k < doc.links.length; k++) {
    var link = doc.links[k];
    var filePath = link.filePath;
    var file = new File(filePath);
    var folderPath = file.parent.fsName;
     if (folderPath.indexOf("Images") !== -1) {
        // Split folderPath based on the word "Images"
        var pathParts = folderPath.split("Images");
         partBeforeImages= pathParts[0];
        var partAfterImages = pathParts[1];
    }
   
}
return partBeforeImages;
}

 

-Monisha
TOPICS
Bug , How to , Scripting

Views

135

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 ,
Nov 30, 2023 Nov 30, 2023

Copy link to clipboard

Copied

Hi @MonishaRajendran , Have you checked what the parent of the image is before remove()?

Add $.writeln(parentFrame) before parentFrame.remove() and check your console.

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
Participant ,
Nov 30, 2023 Nov 30, 2023

Copy link to clipboard

Copied

@rob day  Yeah, I have checked for the parentFrame but the textframe is not removed

-Monisha

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 ,
Nov 30, 2023 Nov 30, 2023

Copy link to clipboard

Copied

So the image is still there? Or empty TextFrame?

 

Parent of the Image can't be TextFrame directly - so if you expect the Image to be in a TextFrame - you would have to refer to the parent of the parent of your TextFrame Image:

 

 

var parentFrame = page.allPageItems[t].parent.parent;

 

 

Or something like that...

 

... Or even triple parent - as Parent of the Rectangle in the Text will be Character - so you would have to get ParentTextFrame:

 

 

var parentFrame = page.allPageItems[t].parent.parent.parentTextFrames[0];

 

 

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 ,
Nov 30, 2023 Nov 30, 2023

Copy link to clipboard

Copied

What Object(s) does the .writeln return? Are they all the same?

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
Participant ,
Nov 30, 2023 Nov 30, 2023

Copy link to clipboard

Copied

LATEST

The writeline returns the "object Rectangle" values. And the same values are repeated throughout the process.

-Monisha

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