Copy link to clipboard
Copied
Im looking to create a script that scans an indesign document and exports specific images and grouped items to a location. Im still new to scripting and ExtendScript, but I kinda have a handle on JS so it seems to be making sense... so far. I've used the following code from two other posts on here to get me started with what I needed, but there seems to be a few hiccups preventing this from working.
So far I can export all of the images to a desktop location, however, the grouped items are throwing an error. It is stating the grpFile.exportFile is not a function. I haven't even gotten to the specific selection, or ommission of certain images or groups. That seems to be phase III or IV
This is the code I am using so far:
var i,
myDoc = app.activeDocument,
myGroups = app.activeDocument.groups,
apis = myDoc.links.everyItem().getElements(), rect, fileName;
alert("Script is running. Press OK and wait until done...");
while ( rect = apis.pop() ) {
rect = rect.parent.parent;
if ( !(rect.hasOwnProperty ("graphics") ) ){
continue;
}
fileName = File ( rect.graphics[0].itemLink.filePath ).name;
fileName = fileName.replace( /\.[a-z]{2,4}$/i, '.jpg' );
app.jpegExportPreferences.exportResolution = 72;
app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MEDIUM;
var myFile = new File ("/Users/q1344763/Desktop/export-test/"+ fileName);
rect.exportFile(ExportFormat.JPG, myFile);
}
alert ("Exporting Groups...");
for ( i = 0; i < myGroups.length; i++ ) {
var grpFile = new File( "/Users/q1344763/Desktop/export-test/groups/" + i + '.jpg' );
grpFile.exportFile( ExportFormat.JPG, grpFile, false );
};
alert ("Done.");
You want to call exportFile on myGroups[i], not grpFile.
Also, does the "groups" folder exist before you try the export? If not, that could be throwing it.
var fol = Folder("/Users/q1344763/Desktop/export-test/groups/");
if (!fol.exists) {
fol.create();
}
Here is the full script that Phlume originally wrote with my corrections. If it fails, please post the error message here.
var i,
myDoc = app.activeDocument,
myGroups = app.activeDocument.groups,
apis = myDoc.links.everyItem().getElements(), rect, fileName;
alert("Script is running. Press OK and wait until done...");
while ( rect = apis.pop() ) {
rect = rect.parent.parent;
if ( !(rect.hasOwnProperty ("graphics") ) ){
continue;
}
fileName = File ( rect.graphics[0].itemLink.filePath ).n
...
Hi aviel222,
my suggested code would be the following:
var exportPrefs = app.jpegExportPreferences.properties;
var newExportPrefs =
{
antiAlias : true ,
exportResolution : 600 ,
jpegColorSpace : JpegColorSpaceEnum.CMYK ,
jpegQuality : JPEGOptionsQuality.MAXIMUM ,
jpegRenderingStyle : JPEGOptionsFormat.BASELINE_ENCODING ,
simulateOverprint : true
};
app.jpegExportPreferences.properties = newExportPrefs ;
var doc =app.documents[0];
var exportFolder = doc.fullName.parent;
var allGroupsA
...
Copy link to clipboard
Copied
You want to call exportFile on myGroups[i], not grpFile.
Also, does the "groups" folder exist before you try the export? If not, that could be throwing it.
var fol = Folder("/Users/q1344763/Desktop/export-test/groups/");
if (!fol.exists) {
fol.create();
}
Copy link to clipboard
Copied
Thank you Brianp311, both of those tips worked well, and have moved the ball closer to the end zone. I appreciate the assist.
Next is to restrict this action to a specific layer so that I can export only items that live on a layer designated for production export. The idea is that the designers will work on a specific layer for design, but then copy/paste finished items to an "export" layer and then run the script so only the desired images/groups, not *all* images or groups are exported... I will circle back with any difficulties.
Copy link to clipboard
Copied
Now, in trying to restrict the export to a specific layer, I can targt a layer for the groups, but the way the script parses the links, I cant seem to focus on just a specific layer (most likely because it is looking at the links palette?)
I have the code working to export only groups on the "ProdLayer" layer, however, all links are still exported. How might I adjust this code to more specifically target the selected layer? DO I have to adjust the apis vriable and how it captures the links, or checks if they are on the target layer... Or should I check their placement within the while loop with the rect.parent.parent. I am still unfamiliar with the exact logic around the rect.parent.parent path and how that relates to the links and why that selects them.
I tried adjusting the apis variable to target the myGroups to capture them (apis = myGroups.links.everyItem()...) but that didn't seem to work. Any further suggestions?
var i,
myDoc = app.activeDocument,
myLayer = myDoc.layers.itemByName( "ProdLayer" ),
myGroups = myDoc.layers.itemByName( "ProdLayer" ).groups,
apis = myDoc.links.everyItem().getElements(), rect, fileName;
alert("Script is running. Press OK and wait until done...");
while ( rect = apis.pop() ) {
rect = rect.parent.parent;
if ( !(rect.hasOwnProperty ("graphics") ) ){
continue;
}
Copy link to clipboard
Copied
Hi Phlume,
this is rather simple. Every page item has an itemLayer property that refers to the layer it is positioned on.
So you could easily check if the graphic frame is on the right layer:
if( rect.itemLayer != myLayer ){ continue }
FWIW: I would not loop the links in the document. Not every link refers to a placed image or graphic.
Better loop the allGraphics array of the document. So you will also catch graphics that are simply pasted and not linked.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Can someone post me this script ready,
Creating a new folder?
Because it does not work for me.
Copy link to clipboard
Copied
Hi aviel222,
unfortunately, it "does not work", does not tell very much.
Please post the link to your document on Dropbox or a similar service so that we could debug the code for your situation.
And best post the code you tried. Was there an error message? If yes, post it as well.
Thanks,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Uwe Laubender and Others,
I will explain myself clearly in the attachment:
https://www.dropbox.com/s/1tpakh48cu9z29i/aviel1.pdf?dl=0
Thanks a lot!!!!!!
Copy link to clipboard
Copied
Hi Aviel222
My post was a specific use case use for our work flow here. With the help of the forum I created and coded a solution myself. I think expecting to just "download the solution" from these forums is a bit against the concept of what these forums exist for. You may be able to simply buy the script if all you want is the solution. I personally can't help you as I am a beginner myself, but in the spirit of learning, why not try to fiddle with the code and see if you can put the pieces together and build it as I have?It took me a few weeks, but eventually I managed to make sense of the code, understand what it's limitations are (and mine) and produce a workign script for our organization.
Copy link to clipboard
Copied
I tried to put the pieces myself and failed.
No one else owes me anything
But if anyone still helps me - I would be happy.
Copy link to clipboard
Copied
Here is the full script that Phlume originally wrote with my corrections. If it fails, please post the error message here.
var i,
myDoc = app.activeDocument,
myGroups = app.activeDocument.groups,
apis = myDoc.links.everyItem().getElements(), rect, fileName;
alert("Script is running. Press OK and wait until done...");
while ( rect = apis.pop() ) {
rect = rect.parent.parent;
if ( !(rect.hasOwnProperty ("graphics") ) ){
continue;
}
fileName = File ( rect.graphics[0].itemLink.filePath ).name;
fileName = fileName.replace( /\.[a-z]{2,4}$/i, '.jpg' );
app.jpegExportPreferences.exportResolution = 72;
app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MEDIUM;
var fol = Folder("~/Desktop/export-test");
if (!fol.exists) { fol.create(); }
var myFile = new File (fol.fsName + "/"+ fileName);
rect.exportFile(ExportFormat.JPG, myFile);
}
alert ("Exporting Groups...");
for ( i = 0; i < myGroups.length; i++ ) {
var grpFile = new File( fol.fsName +"/group" + i + '.jpg' );
myGroups[i].exportFile( ExportFormat.JPG, grpFile, false );
};
alert ("Done.");
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy what I posted fresh into a new .jsx file and copy that into your scripts panel. I'm not sure why you're getting an Applescript error. It should be run as an Extendscript (jsx) file.
Copy link to clipboard
Copied
Right
There was a problem setting up the file,
But it still does not work.
He says:
https://www.dropbox.com/s/xc9600tqgs9dgfa/error2.pdf?dl=0
Copy link to clipboard
Copied
I may not have set a source folder and a destination folder correctly.
Copy link to clipboard
Copied
Sorry, I had a typo in "fsName" on the error line. I've fixed it in the original post. Try again.
Copy link to clipboard
Copied
Thanks !!!
I wanted to ask if the file could have the name of the jpeg
Imported to indesign,
Also, if possible pdf export of choice instead of jpeg
Copy link to clipboard
Copied
Following the previous post with the pdf that explained what I was requesting, I enclose the script that gave me the error:
https://www.dropbox.com/s/l2g5jgo0ck49pbe/avi5.jsx?dl=0
Copy link to clipboard
Copied
Hi aviel222,
my suggested code would be the following:
var exportPrefs = app.jpegExportPreferences.properties;
var newExportPrefs =
{
antiAlias : true ,
exportResolution : 600 ,
jpegColorSpace : JpegColorSpaceEnum.CMYK ,
jpegQuality : JPEGOptionsQuality.MAXIMUM ,
jpegRenderingStyle : JPEGOptionsFormat.BASELINE_ENCODING ,
simulateOverprint : true
};
app.jpegExportPreferences.properties = newExportPrefs ;
var doc =app.documents[0];
var exportFolder = doc.fullName.parent;
var allGroupsArray = doc.groups.everyItem().getElements();
for( var n=0; n<allGroupsArray.length; n++ )
{
var currentGroup = allGroupsArray[n];
// No graphic in group? Loop on:
if( currentGroup.allGraphics.length == 0 ){ continue };
var graphicName = "";
var itemLink = allGroupsArray[n].allGraphics[0].itemLink ;
// No item link? Graphic or image was pasted to the document.
if( itemLink == undefined )
{
graphicName = "pastedImage";
}
else
{
graphicName = itemLink.name ;
};
var exportFile =
File( exportFolder +"/"+ graphicName +"-"+Date.now() +".jpg" );
allGroupsArray[n].exportFile
(
ExportFormat.JPG ,
exportFile
);
};
app.jpegExportPreferences.properties = exportPrefs ;
Basically it is working with the first graphic found in a group. If there are more graphics or images inside a group they will be ignored. The graphic could be nested very deep, even pasted inside a group of groups. The array allGraphics will get it if there is any.
If the graphic inside the group has no item link ( the graphic or image was pasted to the page ) a substitution name is used.
You can adapt the code to your needs: Define an extra folder, right now the folder of the InDesign document is used, remove the suffix from the item link's name etc.pp. I also use a "unique string" to get a unique name in any case. That's important if the same graphic is used in different groups and you do not want to overwrite exported JPEG files.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Thanks!
I wanted to ask 2 last things:
1. If possible the script will be exported to a specific folder on the desktop and not on the desktop itself
2. that the export will be to pdf and not to jpeg
Copy link to clipboard
Copied
And also the file name has 2 dots
for example:
Untitled-1.ai-1596523093123.jpg
And that might be a problem if you use a windows system.
Copy link to clipboard
Copied
I actually managed to keep a new folder in the indesign file folder.
So I have 2 last requests left:
1. Export to pdf instead of jpeg
2. that the file name will not have 2 points (please see in the previous post
Copy link to clipboard
Copied
We are helpful, but we also expect that users seeking help in these forums are willing to put in some of the sweat equity to learn the fundamentals of scripting, just as Phlume did.
Examine the exportFile method under the Group object to see if you can identify how to change it from jpg to pdf. You also will need to handle the naming of the exported file correctly.
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Group.html#d1e232004__d1e234743
Stripping extensions from linked image names involves the manipulation of string objects. Using string.lastIndexOf(".") can be helpful in finding the position of the extension marker in the string, then using string.slice(0, string.lastIndexOf(".") can get you the file name without extension. Give it a shot.
Copy link to clipboard
Copied
I say again,
No one owes me anything
And I'm happy if anyone can help,
I'm not a programmer.
I am very grateful to all those who are willing to help.
Copy link to clipboard
Copied
Hi together,
export to PDF is a very different thing to do compared with export to JPEG.
Why? Because with export to PDF you always export the whole page ( or spread of pages ).
There is no functionality or method in scripting to export e.g. a group or item on the page and not get the whole page as smallest unit.
Therefore I would strongly suggest to open a new thread for this task.
But before doing this please search the web and this forum. This issue came up more than one time before and not much changed in the last 11 years ( after InDesign CS4 ) in this regards.
Thanks,
Uwe Laubender
( ACP )