Skip to main content
ChristianK-Fr
Inspiring
September 30, 2021
Answered

Script to check all PathItem Names

  • September 30, 2021
  • 2 replies
  • 2515 views

Hi, 

I've something like this: 

 

var doc = activeDocument;
var pArray = [];
  var pLen = doc.pathItems.length
alert ("Number of Path Found :" + pLen);  
for (var i = 0; i < pLen; i++) {
          pArray.push(doc.pathItems[i].name)
var curPath = doc.pathItems.getByName(pArray[i]);
alert (curPath);
}

 

 but it give me the name in alert for each path found : 3 paths -> 3 alerts

I try to see if there's a way to have all paths name found all together, and not step by step

 

In fact, want to do this : 

In image, found paths, and check if a path is named "DetPath". If not, then select the first path to rename it "DetPath"

My issue is when the second, or third path is named "DetPath", and not the first, it renamed the first path "DetPath", but script stopped because the second path already named "DetPath"....

So before it have to rename, first check all names found then let's go, you can rename, or not (then alert that there already have a path named correctly iin image)

Hope it's clear 😉

 

This topic has been closed for replies.
Correct answer jazz-y

Hi, 

I try this : 

// app.displayDialogs = DialogModes.NO;
var doc = activeDocument;
var pLen = doc.pathItems.length
var pArray = [];
for(var i=0;i<pLen;i++){
    pArray.push(doc.pathItems[i].name);
    }
for(var i=0;i<pLen;i++){
	var curPath = doc.pathItems.getByName (pArray[i]);
	curPath.name = curPath.name.toLowerCase();
    	if(curPath.name !='decapath')
	{curPath.remove();
	}
}	

 if I have image with patItems different from "decapath", it prompt the pathItem name dialog, and I have to click on "OK" then it suppress the pathItems, and for all others doing the same.

- and if I put on script DialogMode.No, script stop and nothing happen....

I didn't understand.... is it the remove() command ? How can I force to validate it ?

Thanks in advance ,

Christian


It seems that you cannot rename the path in this way without displaying a dialog box.

Why are you renaming it? It is enough to store the name in a variable, or use it directly when comparing in an if:

var doc = activeDocument;
var pLen = doc.pathItems.length
var pArray = [];
for(var i=0;i<pLen;i++){
    pArray.push(doc.pathItems[i].name);
    }
for(var i=0;i<pLen;i++){
	var curPath = doc.pathItems.getByName (pArray[i]);
    	if(curPath.name.toLowerCase()!='decapath')
	{curPath.remove();
	}
}	

 

2 replies

Chuck Uebele
Community Expert
Community Expert
September 30, 2021

If you're still using Extendscript tool Kit, you can also use: $.wrtieln(curPath) and it will list them in the console panel.

Stephen Marsh
Community Expert
Community Expert
September 30, 2021

@Chuck Uebele 

 

Ah serendipity...

 

Scripting for photoshop - JSON just returns [object Object]

 

Is there an equivalent of $.writeIn() for Visual Studio Code ExtendScript Debugger?

Kukurykus
Legend
September 30, 2021

To correct code change last 3 lines to:

 

}

alert (pArray);

 

For renaming the path use only:

 

!(String([].slice.call(pI = activeDocument.pathItems))
.split('[PathItem detPath]').length - 1) && pI[0].name = 'detPath'

 

ChristianK-Fr
Inspiring
September 30, 2021

Many thanks ! 

I'll try it and let you know !

 

 

So, it works, but have the same problem, in fact : 

- if the current Path is renamed it made error IF other Path have the same name.

This case is if images have multiples Path, and one of this have the right name "detPath", other than the first one.

For example : 

Image with Paths : 

- "chairs"

- "skins"

- "detPath"

In a script :

- Find 3 Paths -ok

- Find namesPath -ok alert (chairs,skins,detPath) ok

- first Path : "chairs" renamed in "detPath" : error detPath already exist.

So the script have to check all Path, if it find "detPath", done, it's good, no paths to rename,

if it doens't find it, so rename the first or alert " there's multiple paths, please select wich one to rename"

 

Hope it's clear, again, and thanks for your precious help !

Kukurykus
Legend
September 30, 2021

You can not have 2 same named path items. Anyway have you try the other code from me as sole script? This works exactly the way you describes. When there's already some path named 'defPath' it does not change name of the first path item, but if not any of them is named yet as 'defPath' it changes name of first path item.