Highlighted

Iterate Through ActiveDocuments | What am I doing wrong?

Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

I am trying to loop through my Active Documents and the Code tends to get interrupted at different points. Im not sure what is causing it to stop. Code also doesnt run properly when I try and close each document. 

 

Below is my Script ------- Note: I am not a javascript expert. So be gentle with me. 

 

saveActDocJPG();

function saveActDocJPG() {

var docLength = app.documents.length;

for ( var i = 0; i < docLength; i++ ) {
if ( i === docLength ) {
break;
}

var doc = documents[ i ];
app.activeDocument = doc;
app.refresh();

////// change path below ///////////
var docPath = '/Volumes/CT-WORK/Prep Work/';

var folderString = docPath + "/" + "JPG-8";
if ( Folder( folderString ).exists == false ) {
new Folder( folderString ).create()
}

var docName = doc.name.split( '.' )[ 0 ];
var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 8;
jpgOptions.embedColorProfile = true;

doc.saveAs( new File( folderString + "/" + docName + '.jpg' ), jpgOptions, true );
// NOTE: Craps out when using doc.close
// doc.close( SaveOptions.DONOTSAVECHANGES );
// doc.close();
}

alert( "JPG-8" + "\n\n" + "You Saved " + docLength + " Open Files." );
}

Most Valuable Participant
Correct answer by JJMack | Most Valuable Participant

You also need to test Script well to eliminated bug for their a bug in that script.  I ran the script it saved 5 file in the folder but in the process it  overwrote a file  twice so there were only three files in the folder not 5.  Because the document name wast not handled well. So you should fix that bug.....]]

Capture.jpg

Topics

Actions and scripting, How to, Mac, Problem or error

Views

79

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

Iterate Through ActiveDocuments | What am I doing wrong?

Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

I am trying to loop through my Active Documents and the Code tends to get interrupted at different points. Im not sure what is causing it to stop. Code also doesnt run properly when I try and close each document. 

 

Below is my Script ------- Note: I am not a javascript expert. So be gentle with me. 

 

saveActDocJPG();

function saveActDocJPG() {

var docLength = app.documents.length;

for ( var i = 0; i < docLength; i++ ) {
if ( i === docLength ) {
break;
}

var doc = documents[ i ];
app.activeDocument = doc;
app.refresh();

////// change path below ///////////
var docPath = '/Volumes/CT-WORK/Prep Work/';

var folderString = docPath + "/" + "JPG-8";
if ( Folder( folderString ).exists == false ) {
new Folder( folderString ).create()
}

var docName = doc.name.split( '.' )[ 0 ];
var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 8;
jpgOptions.embedColorProfile = true;

doc.saveAs( new File( folderString + "/" + docName + '.jpg' ), jpgOptions, true );
// NOTE: Craps out when using doc.close
// doc.close( SaveOptions.DONOTSAVECHANGES );
// doc.close();
}

alert( "JPG-8" + "\n\n" + "You Saved " + docLength + " Open Files." );
}

Most Valuable Participant
Correct answer by JJMack | Most Valuable Participant

You also need to test Script well to eliminated bug for their a bug in that script.  I ran the script it saved 5 file in the folder but in the process it  overwrote a file  twice so there were only three files in the folder not 5.  Because the document name wast not handled well. So you should fix that bug.....]]

Capture.jpg

Topics

Actions and scripting, How to, Mac, Problem or error

Views

80

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
LEGEND ,
Sep 16, 2020

Copy link to clipboard

Copied

When you close th eimages in the for-clause you run up the number but decrease the number of actually open documents. 

So at some point 

var doc = documents[ i ];

can probably be nonsensical. 

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...
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

So, just remove it? 

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...
LEGEND ,
Sep 16, 2020

Copy link to clipboard

Copied

If you close the image/s affter saving the jpgs try leaving out the lines 

var doc = documents[ i ];

app.activeDocument = doc;

altogether. 

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...
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

Okay, let's see where this goes 🙂 Thank you for repsonding so quickly. 

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...
LEGEND ,
Sep 16, 2020

Copy link to clipboard

Copied

Sorry, replace them with 

var doc = app.activeDocument;

 

Edit (I changed the path to the Desktop, though): 

 

saveActDocJPG();
function saveActDocJPG() {
var docLength = app.documents.length;
for ( var i = 0; i < docLength; i++ ) {
var doc = app.activeDocument;
////// change path below ///////////
var docPath = '~/Desktop';
var folderString = docPath + "/" + "JPG-8";
if ( Folder( folderString ).exists == false ) {
new Folder( folderString ).create()
}
var docName = doc.name.split( '.' )[ 0 ];
var jpgOptions = new JPEGSaveOptions();
jpgOptions.quality = 8;
jpgOptions.embedColorProfile = true;
doc.saveAs( new File( folderString + "/" + docName + '.jpg' ), jpgOptions, true );
doc.close( SaveOptions.DONOTSAVECHANGES );
}
alert( "JPG-8" + "\n\n" + "You Saved " + docLength + " Open Files." );
}

 

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...
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

I did try this. 

I have 47 documents open. Im looping through each opened document, saving a jpg and closing it. 

 

When i remove the 

var doc = documents[ i ];

app.activeDocument = doc;

 

it no longer loops through my opened documents. I also tried to assign my "doc" as a variable but it only kept saving the same document over and over lol. Back to the drawing board! 😐

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...
LEGEND ,
Sep 16, 2020

Copy link to clipboard

Copied

I added edited code to the previous post, try that (and change the Path back to your intended Folder). 

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...
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

I saw that just now. Thank you. You posted it quicker than I tested your recommendation lol. 🙂 Okay, let's see how what you did differently. 

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...
Most Valuable Participant ,
Sep 16, 2020

Copy link to clipboard

Copied

You also need to test Script well to eliminated bug for their a bug in that script.  I ran the script it saved 5 file in the folder but in the process it  overwrote a file  twice so there were only three files in the folder not 5.  Because the document name wast not handled well. So you should fix that bug.....]]

Capture.jpg

JJMack

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...
Community Beginner ,
Sep 16, 2020

Copy link to clipboard

Copied

Thank you JJMack. 

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...