Check for and change specific path name

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Limey

Check for and change specific path name

Post by Limey »

I have over 100,000 files that need to be checked for any instance where there is an occurrence of "Path 1" and if found change that path name to "path1" (lower case, no space).

Issues that might stop the action
Some of the files/documents will contain no paths
Some of the files/documents will contain multiple paths
Some of the files/documents will contain multiple paths, none of which are "Path 1"
The path should be a normal path but could possibly have been saved as a clipping path
I would like to make an action (run from a droplet) that will look at the paths in each document and IF "Path 1" is found, rename it to "path1", leave any other paths, if present, alone and save the file over itself.
If "Path 1" is not found or any of the above mentioned scenarios are found, then the script is to do nothing and allow the script to simply pass on to the next file.

Could someone please provide some script that would target and rename "Path 1" for me without stopping for input or erroring if it's not available?

Many thanks,
Paul (Limey)

P.S The code I have below is going through each path but stopping for me to rename them all but not looking specifically for "Path 1".

Code: Select all#target Photoshop
main();
function main(){
if(!documents.length) return;
var doc = activeDocument;
for(var a = doc.pathItems.length-1;a>-1;a--){
if(doc.pathItems[a].name  = "Path 1") doc.pathItems.name =  "path1";
    }
}
xbytor

Check for and change specific path name

Post by xbytor »

Should be:

Code: Select allif(doc.pathItems[a].name  == "Path 1") doc.pathItems.name =  "path1";
Limey

Check for and change specific path name

Post by Limey »

Thanks for the reply Xbytor!
I have run the script with the modification and it's going through each path starting at the highest path number and working back to zero. It's not requesting an input for the mane change but it is also not renaming "Path 1" to path1.

I have a test doc. with
Path 1
Path 2
Path 3
It's scooting right over them with no change (as shown by alert(a)
Code: Select all#target Photoshop
main();
function main(){
if(!documents.length) return;
var doc = activeDocument;
for(var a = doc.pathItems.length-1;a>-1;a--){
if(doc.pathItems[a].name  == "Path 1") doc.pathItems.name =  "path1";
//alert(a)
    }
}

Any thoughts?
Cheers!
Paul
pfaffenbichler

Check for and change specific path name

Post by pfaffenbichler »

With
Code: Select allif(doc.pathItems[a].name  == "Path 1") doc.pathItems.name =  "path1";
you are not addressing the path to be renamed because
Code: Select alldoc.pathItems
is not identical to
Code: Select alldoc.pathItems[a]
I guess.
Limey

Check for and change specific path name

Post by Limey »

I'll see if it makes any difference when I get to the office in the morning.

Thanks Xbytor!
pfaffenbichler

Check for and change specific path name

Post by pfaffenbichler »

To reiterate:
Code: Select alldoc.pathItems
does not identify one specific path.
Limey

Check for and change specific path name

Post by Limey »

I got'cha!
If pathItem[1] is equal to Path 1 then change pathItem[?] to path1 doesn't work!! makes perfect sense!
If pathItem[1] is equal to Path 1 then change pathItem[1] to path1 worked!

I got it sorted and it worked great! Code below:
Code: Select all#target Photoshop
main();
function main(){
if(!documents.length) return;
var doc = activeDocument;
for(var a = doc.pathItems.length-1;a>-1;a--){
if(doc.pathItems[a].name  == "Path 1") doc.pathItems[a].name = "path1";
    }
}

Save file IF changes made
As a part of the IF statement, can I include a SAVE, only if it's true? In other words if there is no renaming of a path, just continue without saving and allow the action to close the file. If a path is renamed, then save the file over itself and then allow the action to close the file.

Many thanks!
Paul
pfaffenbichler

Check for and change specific path name

Post by pfaffenbichler »

One option would be to create a boolean variable at the start of the Script that is false, then change that to true in the if-clause and at the end of the Script add another if-clause that is dependent on that variable.
Limey

Check for and change specific path name

Post by Limey »

This is the script that I am using now to search folders for any instance of a saved path named "path1". It then makes a copy of that file to another location. It would be nice if I could have it simply write the file name and/or path(location) to a log but this works for now and saving me weeks or months of work (1.3TB of files to go through)!


Code: Select all   function main() {
          // Select folder search files
        var searchFolder = Folder.selectDialog ("Select folder to search in")
        // Select folder to save files
        var saveFolder = Folder.selectDialog ("Select folder to save files")
        var searchFiles = searchFolder.getFiles(/\.(jpg|tif|psd|eps)$/i);
        for(var i=0;i<searchFiles.length;i++){
            searchFiles = getPhotoshopPathInfo( searchFiles );
            var moveFile = false;         

           if(searchFiles.hasPhotoshopSavedPath) moveFile = true;
           
            if( moveFile && saveFolder.exists ){ 
                var newFilepath = saveFolder.fullName+"/"+searchFiles.name;
                searchFiles.copy(newFilepath); // copy file to new location
            }
        }
    };

    function getPhotoshopPathInfo( fileObject ){
       var savedPathTag = "path1";

       fileObject.open('r');
       fileObject.encoding = 'BINARY';
       var str = fileObject.read();
       fileObject.close(); //close the file in memory
       fileObject.hasPhotoshopSavedPath = str.search(savedPathTag) == -1 ? false:true;
        return  fileObject;
}
    main();


Question: Is there a piece of code that could be inserted to show the name of the file the script is currently working on? I can alert the name but it obviously stops the script. It would be nice if there was a rolling display that would show the progress of the script through the list of files without overly slowing the script down.

I found a post on PS-Scripts for a progress bar. Is this something that could be used with the above script instead of showing the file names?
bb/viewtopic.php?f=13&t=786

Paul (Limey)