What's new

Script error in PS 2021


asela

Member
Messages
8
Likes
2
Hi there,

I have a script (Javascript) that copy / paste paths from one image to another.
It has worked for a long time, but with PS 2021 on Windows 10, I get an error in this code where the selected path is copied (2nd line),
Code:
var id122 = charIDToTypeID("copy");
executeAction( id122, undefined, DialogModes.NO );
"General Photoshop error occurred. This functionality may not be available in this version of Photoshop"

How can I fix this error?
Also is there an ExtendedScript editor for PS 2021?

Thanks
 

[ iLLuSioN ]

Well-Known Member
Messages
169
Likes
164
I think the error is only the cause of a previous script error.

If the script breaks off at this point, you should check whether the copy command is really available. If not, probably no path is selected either.
 

asela

Member
Messages
8
Likes
2
I think the error is only the cause of a previous script error.

If the script breaks off at this point, you should check whether the copy command is really available. If not, probably no path is selected either.
Thanks for looking into this.
Copy command is available according to PS documentation page 219, https://www.adobe.com/content/dam/acom/en/devnet/photoshop/pdfs/photoshop-javascript-ref-2020.pdf
Everything works fine up to that line of code, image with path is open in PS and Path 1 is selected as intended.
 

[ iLLuSioN ]

Well-Known Member
Messages
169
Likes
164
Now that you have published the script in the Adobe forum, I can say that it is not the script, but the fact that apparently no clipping path is available / selected / the first path is not a clipping path.

But without an example file you can't say anything more precise...
 

asela

Member
Messages
8
Likes
2
Now that you have published the script in the Adobe forum, I can say that it is not the script, but the fact that apparently no clipping path is available / selected / the first path is not a clipping path.

But without an example file you can't say anything more precise...
Did the script work for you in Photoshop 2021 (22.1.0)?
Also the same image file worked fine with the script in Photoshop 2018 version.
 

[ iLLuSioN ]

Well-Known Member
Messages
169
Likes
164
Yes. So I suspect that there is no clipping path at all.

However, since you have not provided a sample file, it cannot be checked.
Why don't you just check in the script whether the selected path is a clipping path?
 

asela

Member
Messages
8
Likes
2
Yes. So I suspect that there is no clipping path at all.

However, since you have not provided a sample file, it cannot be checked.
Why don't you just check in the script whether the selected path is a clipping path?
I checked with the code,
Code:
if (sourceDoc.pathItems[0].kind == PathKind.CLIPPINGPATH)
{
  boolCP = true;
}
and boolCP is true, so it's a clipping path.

Also these images in question work fine with the script in Photoshop 2018.

Thanks
 

asela

Member
Messages
8
Likes
2
Unfortunately I cannot give the original files, but I created two files in PS 2021 and attached them, they are giving the same error in PS2021
Can you please check?

Thanks

source.jpg

destination.jpg
 

asela

Member
Messages
8
Likes
2
Full script as follows to copy path from one image to another (have to provide file paths to sourceFile and destinationFile variables in the first two lines),
Code:
var sourceFile = new File("File path to source file"); // file path to image that has the clipping path
var destinationFile = new File("File path to destination file"); // file path to image to which we want to copy the clipping path from source file

open(sourceFile);
open(destinationFile);

var sourceDoc = documents[0];
var destinationDoc = documents[1];

activeDocument = sourceDoc;

sourceDoc.pathItems[0].select(); //get the top most path from source file

var id122 = charIDToTypeID("copy");
executeAction( id122, undefined, DialogModes.ERROR ); // copy the selected path
    
activeDocument = destinationDoc;

var id126 = charIDToTypeID( "past" );
executeAction( id126, undefined, DialogModes.NO ); // paste the copied path to destination file

sourceDoc.close(SaveOptions.DONOTSAVECHANGES); //close without any changes
destinationDoc.close(SaveOptions.SAVECHANGES); //save with changes

alert("Done");

sourceFile.close();
destinationFile.close();
 

chick

Active Member
Messages
37
Likes
0
asela, this is SO what I need, but I'm a novice and not sure how to use it. I copied your code and created the .jsx file and put it in Scripts, but how do I specify the "new File" path to source file? I expected it to take a selected path in one image and add it to the paths in the second image, or at least let me enter the names. Instead I got an error that it was not specified.

So do I need to edit this script each time I need to move a path? Would I have to give the Windows file paths to the images, or is this "new File" path something in PS?

And how do I select a path other than the top one in the source file or do I just have to move the desired path to the top of the list?

Any help appreciated!
 

asela

Member
Messages
8
Likes
2
asela, this is SO what I need, but I'm a novice and not sure how to use it. I copied your code and created the .jsx file and put it in Scripts, but how do I specify the "new File" path to source file? I expected it to take a selected path in one image and add it to the paths in the second image, or at least let me enter the names. Instead I got an error that it was not specified.

So do I need to edit this script each time I need to move a path? Would I have to give the Windows file paths to the images, or is this "new File" path something in PS?

And how do I select a path other than the top one in the source file or do I just have to move the desired path to the top of the list?

Any help appreciated!
To answer your questions,
You have to specify the path to files inside parenthesis of "new File()" as I have mentioned in the code (check the code comments).

You will need to add code to iterate a folder of images instead of just one file which is of not much use (you can add a GUI to allow user to select source and destination folders too instead of editing code to enter paths).

The code I posted will only copy top most path from a single image (sourceFile) to another (destinationFile). If you want to copy and paste all paths, you will have to iterate the paths collection and copy paste all of them.

You can learn more about PS scripting by referring the PDFs here (get the documents relevant to your version of PS and language you prefer to learn),
 

chick

Active Member
Messages
37
Likes
0
To answer your questions,
You have to specify the path to files inside parenthesis of "new File()" as I have mentioned in the code (check the code comments).

You will need to add code to iterate a folder of images instead of just one file which is of not much use (you can add a GUI to allow user to select source and destination folders too instead of editing code to enter paths).

The code I posted will only copy top most path from a single image (sourceFile) to another (destinationFile). If you want to copy and paste all paths, you will have to iterate the paths collection and copy paste all of them.

You can learn more about PS scripting by referring the PDFs here (get the documents relevant to your version of PS and language you prefer to learn),
Asela, Thank you very much for your answer. I'm afraid I have tried everything I can think of but I still get error when I try to use the script. I've attached a collage screenshot of the files, the script, and the error message. If you can see what I am doing wrong, I'd be very grateful.

Move Path Test Error.jpg
 

asela

Member
Messages
8
Likes
2
Asela, Thank you very much for your answer. I'm afraid I have tried everything I can think of but I still get error when I try to use the script. I've attached a collage screenshot of the files, the script, and the error message. If you can see what I am doing wrong, I'd be very grateful.

View attachment 119798
Looking at the error message, it looks like you have included this line in your code (are you sure you the code in the screenshot is what you are running in PS?),
"Full script as follows to copy path from one image to another (have to provide file paths to sourceFile and destinationFile variables in the first two lines),"
Please remove that and run.
 

chick

Active Member
Messages
37
Likes
0
Well, some very strange things are happening.

(1) I did remove the first lines, entered the Windows file name paths, and then copied the file into the Scripts folder. After your last answer, I went back and checked, sure enough, and the .jsx file in the Scripts folder was your original without the line removed. Thinking I had made a mistake, I repeated, double-checked, deleted the Scripts file, copied the new one, certain I had changed it, and then checked the .jsx file in the Scripts folder where I had just copied it. It was the old file with that first line still in it. The file I copied was correct, the file I pasted into Scripts was not. I'm not crazy, I can reproduce this at will.

(2) Also, I had put this ONLY into the PS 2020 folder, not into the 2019 or 2021 that I also still have installed. When I checked those, the incorrect .jsx file (still with the first line) was in all three places! Not sure at what point that happened.

(3) I deleted everything and started over again, and the script (with the first line removed) when copy/pasted into the 2020 Scripts folder still had the first line in it. I checked and repeated this several times to be sure this was actually happening.

(4) I deleted everything and started over again, this time using a different text editor, and the script (with the first line removed) when copy/pasted into the 2020 Scripts folder seemed ok. So I thought maybe the first text editor was doing this, how I cannot imagine.

(5) The next attempt to run the script gave me an error on the file path name. I fiddled for a long time before I realized that although I had specified "D:\Foldername\Filename.psd", it was being changed in the .jsx file to "D:/Foldername/Filename.psd". The backslashes were being changed to forward slashes when it was copy/pasted into Scripts. I checked the files, repeated the copy/paste enough times to finally convince myself I wasn't dreaming. I can reproduce and demonstrate this. I have no idea how or why this is happening.

(6) So I finally gave up on the second text editor and tried still a third text editor! Starting over, I edited the .jsx file directly in the Scripts folder, and this managed to get the .jsx file appearing to be correct in the Scripts folder, with the backslashes.

(7) When I ran it, I still got an error saying the path was wrong. I don't know any of these scripting languages (my programming is from pre-java days), but I did manage to find an example in the Adobe documentation you referred me to, showing a path name like "D:\\Foldername\Filename.psd" (with an extra backslash), but that also didn't work. Same error, no file found.

(8) I tried again using an alternate windows path format like "/d/Foldername/Filename.psd" which also appeared somewhere in an example. This found the .psd files!!

(9) But, with that path format, although PS 2020 ran the script, it failed and gave an error saying the "command Paste is not currently available".

(10) I did this all over again, putting everything in PS 2021 and it ran to completion!! Except it didn't copy the Photoshop path from one .psd file to the other. All it copied was the top layer, not the top path. So my destination file ended up with a new layer from the source file, but NOT a new path in it.

I just wanted you to know I'm not blaming your script, which I really wish I could make work. Apparently my PhD in Computer Science (yes, really) isn't enough to cope with this.
 

MarshySwamp

Member
Messages
7
Likes
6
@chick

I have made some modifications to the original script which may help you, there is no need to hard code in the source/destination doc paths:

JavaScript:
/*
https://www.photoshopgurus.com/forum/threads/script-error-in-ps-2021.72619/
Script error in PS 2021

Copy top path from Doc A to Doc B.jsx
Modified by Stephen Marsh 2021
*/

#target photoshop

if (app.documents.length === 0) {

    // open the files via native open dialog
    var openSource = File.openDialog("Please select the source file:");
    open(openSource);

    var openDestination = File.openDialog("Please select the destination file:");
    open(openDestination);

    // doc variables
    var sourceDoc = documents[0];
    var destinationDoc = documents[1];

    // set the active doc
    app.activeDocument = sourceDoc;

    // get the top most path from source file
    sourceDoc.pathItems[0].select();

    // copy the selected path
    var id122 = charIDToTypeID("copy");
    executeAction(id122, undefined, DialogModes.ERROR);

    // set the active doc
    app.activeDocument = destinationDoc;

    // paste the copied path to destination file
    var id126 = charIDToTypeID("past");
    executeAction(id126, undefined, DialogModes.NO);

    // close without any changes
    sourceDoc.close(SaveOptions.DONOTSAVECHANGES);

    // save with changes
    destinationDoc.close(SaveOptions.SAVECHANGES);

    // end of script notification
    alert("Done");

}

else {
    alert('No documents should be open when running this script!');
}
 

Top