Skip to main content
davidr89736007
Participating Frequently
November 22, 2023
Answered

Script to select and delete using coordinates

  • November 22, 2023
  • 2 replies
  • 1562 views

Let me start by saying I know next to nothing about scripting for Illustrator outside of how to run one. AI has usually helped me create the simple scripts I've needed recently, until now. What it is suggesting does nothing at all.

 

I have about 1700 PDF's I need to remove a header and footer from (they're all engineering specs and nothing is named or organized by layers). All of the headers and footers are in almost the exact same position. My thinking is using the rough coordinates of where they are I should be able to select and delete between the top-left and bottom-right coordinates of these areas. So far nothing has worked. 

 

Is this even possible? Any help is welcome.

Correct answer Kurt Gold
  • Do all files have the same artboard dimensions? If so, about what dimensions are we talking?
    • Yes from what I have seen they are all the same size. They are all 612px x 792px
  • Are all .pdf files single page documents or multiple page documents? Or a mixture of both?
    • All single page
  • What kind of objects do the files contain? Only (live) type objects? Or all kind of possible objects including raster art?
    • Primarily paths, occasionally clip groups. Have yet to see any actual type objexts. No raster at all (these all come from AutoCAD output)
  • Are there (nested) clipping masks in the files?
    • Occasionally there are some clipping masks/clip group with no explanation why. Again, they're AutoCAD output and I'm not terribly familiar with its export settings.

Based on your infos about the files, I've modified an action I once used for something different.

 

You can download a .zip file that contains a sample Illustrator file and an action set file:

 

Remover 1

 

Instructions:

 

- Download and unzip the file
- Open the sample file remover_sample_001.ai

- In the Align palette, make sure that the "Align to Artboard" option is turned on
- In the Actions palette flyout menu, load the action set remover.aia
- In the Actions palette, run the action remover_1

 

Note that the action may be modified depending on what files you are trying to process. But one would then indeed have to take a look at some real files from you.

 

2 replies

Inspiring
November 28, 2023

Call the script and select the folder containing the PDF.
Delete the object based on its location.
If this does not seem to work, try changing the value of [Parameters] in the script.
How does it work?

 

//Parameters
iheader_px = 80
ifooter_px = 80
iheader_margin = 20
ifooter_margin = 20
//
sFileType = "pdf"
main();

function main(){
	coordinateSystemOrg = app.coordinateSystem;
	try{
		app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
		folderRef = Folder.selectDialog("Select Folder");
		if( folderRef != false){
			fileList = folderRef.getFiles("*." + sFileType)
			if( fileList != false){
				ifl = fileList.length
				for (var i=0;i<ifl;i++){
					fileObj = new File(fileList[i].fsName);
					app.userInteractionLevel=UserInteractionLevel.DONTDISPLAYALERTS;
					open(fileObj);
					app.userInteractionLevel=UserInteractionLevel.DISPLAYALERTS;
					oDoc = app.activeDocument;
					iwidth = oDoc.artboards[0].artboardRect[2]-oDoc.artboards[0].artboardRect[0]
					iheight = oDoc.artboards[0].artboardRect[3]-oDoc.artboards[0].artboardRect[1]
					hx1 = 0;
					hy1 = 0;
					hx2 = iwidth;
					hy2 = iheader_px + iheader_margin;
					fx1 = 0
					fy1 = -(iheight + ifooter_px + ifooter_margin);
					fx2 = iwidth;
					fy2 = -iheight;
					opg = oDoc.pageItems
					ipg = opg.length
					for( j=ipg-1;j>-1;j--){
						if(checkinout(opg[j])){
							try{
								opg[j].remove()
							}catch(e){;
							};
						};
					};
					oDoc.close(SaveOptions.SAVECHANGES);
					oDoc = null
				};
			};
		};
		app.coordinateSystem = coordinateSystemOrg;
	}catch(e){;
		app.coordinateSystem = coordinateSystemOrg;
	};
}

function checkinout(obj){
	if (obj.geometricBounds){
		var x1 = obj.geometricBounds[0];
		var y1 = -obj.geometricBounds[1];
		var x2 = obj.geometricBounds[2];
		var y2 = -obj.geometricBounds[3];
		if (x1 > hx1 && x2 < hx2 && y1 > hy1 && y2 < hy2)
			return true
		else{
			if (x1 > fx1 && x2 < fx2 && y1 > fy1 && y2 < fy2)
				return true
			else{
				return false
			}
		}
	}
}
Kurt Gold
Community Expert
November 22, 2023

Can you share a couple of the .pdf files, so one can take a look?

 

davidr89736007
Participating Frequently
November 22, 2023

Due to the propietary nature of the files I can't share an actual one, but I mocked up some rectangles and text that more or less represents the organization of one of the PDFs. Hope this helps.

 

Again, I need to remove the header and the footer sections and then save the remaining "spec" section.

Kurt Gold
Community Expert
November 24, 2023

Based on your infos about the files, I've modified an action I once used for something different.

 

You can download a .zip file that contains a sample Illustrator file and an action set file:

 

Remover 1

 

Instructions:

 

- Download and unzip the file
- Open the sample file remover_sample_001.ai

- In the Align palette, make sure that the "Align to Artboard" option is turned on
- In the Actions palette flyout menu, load the action set remover.aia
- In the Actions palette, run the action remover_1

 

Note that the action may be modified depending on what files you are trying to process. But one would then indeed have to take a look at some real files from you.

 


David, I have another action that does not rely on flattening transparency. It's less destructive and moreover it is independent from the actual artboard dimensions.

 

Let me know if that is something you would rather prefer.