Highlighted

Close all documents except first

Explorer ,
Oct 06, 2020

Copy link to clipboard

Copied

I'm trying to make a photoshop script that closes all documents without saving except for the first document. The first document will be a psd/psdc, the rest will be images

Currently I have the below code, this closes all documents

while (app.documents.length > 0) {
   app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}

I've tried modifying it the same way I've done with a previous script, but it closes random documents, and not even all of them, it can leave 5-6 documents open

for (var i = 1; i < app.documents.length; i++) {
   app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}

Can anyone point me in the right direction

The problem is that the length property decreases with each iteration of the loop (that is, the number of open documents changes), as a result, the loop ends without going through all the documents. The solution is to move length into a separate variable.
The second point is that by closing the active document, you can close the one you want (it depends on which tab the focus is on and which document you selected earlier). The solution is to close the second document in the list every iteration.

var len = app.documents.length
for (var i = 1; i < len; i++) {
   app.documents[1].close(SaveOptions.DONOTSAVECHANGES);
}

or 

while (app.documents.length > 1) {
   app.documents[1].close(SaveOptions.DONOTSAVECHANGES);
}
TOPICS
Actions and scripting, How to, Mac

Views

42

Likes

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

Close all documents except first

Explorer ,
Oct 06, 2020

Copy link to clipboard

Copied

I'm trying to make a photoshop script that closes all documents without saving except for the first document. The first document will be a psd/psdc, the rest will be images

Currently I have the below code, this closes all documents

while (app.documents.length > 0) {
   app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}

I've tried modifying it the same way I've done with a previous script, but it closes random documents, and not even all of them, it can leave 5-6 documents open

for (var i = 1; i < app.documents.length; i++) {
   app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}

Can anyone point me in the right direction

The problem is that the length property decreases with each iteration of the loop (that is, the number of open documents changes), as a result, the loop ends without going through all the documents. The solution is to move length into a separate variable.
The second point is that by closing the active document, you can close the one you want (it depends on which tab the focus is on and which document you selected earlier). The solution is to close the second document in the list every iteration.

var len = app.documents.length
for (var i = 1; i < len; i++) {
   app.documents[1].close(SaveOptions.DONOTSAVECHANGES);
}

or 

while (app.documents.length > 1) {
   app.documents[1].close(SaveOptions.DONOTSAVECHANGES);
}
TOPICS
Actions and scripting, How to, Mac

Views

43

Likes

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
Oct 06, 2020 0
Enthusiast ,
Oct 06, 2020

Copy link to clipboard

Copied

The problem is that the length property decreases with each iteration of the loop (that is, the number of open documents changes), as a result, the loop ends without going through all the documents. The solution is to move length into a separate variable.
The second point is that by closing the active document, you can close the one you want (it depends on which tab the focus is on and which document you selected earlier). The solution is to close the second document in the list every iteration.

var len = app.documents.length
for (var i = 1; i < len; i++) {
   app.documents[1].close(SaveOptions.DONOTSAVECHANGES);
}

or 

while (app.documents.length > 1) {
   app.documents[1].close(SaveOptions.DONOTSAVECHANGES);
}

Likes

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
Reply
Loading...
Oct 06, 2020 3