Copy link to clipboard
Copied
There are other manners to automate captions (checkbox on import, variable text + object and p-styles), but I thought I could get this to work and now at a loss. If someone has a moment to look at this, target the line myCaption.contents = <Metadata:Description></Metadata:Description>;
For if I was to use myCaption.contents = "caption text goes here"; you would be served placeholder text. I thought I would be closer to snagging the description, but no cigar as yet. š
Something like this?
capset();
function capset() {
var mDoc = app.activeDocument,
//anchored or grouped items excluded
myGraphics = mDoc.splineItems.everyItem().getElements(),
picContainer,
gb,
myCaption;
while (picContainer = myGraphics.pop()) {
//exclude empty frames
if (!picContainer.graphics.length) continue;
gb = picContainer.geometricBounds;
//add a frame to to pictureās parent, which is a Page
myCaptio
...
Hi @MadMac55, just for learning purposes, here's a version of your script with a bit of error handling added. For example, the other script will throw an error if there isn't metadata for the linked image.
function main() {
addCaptionToImages('My Object Style', 30);
function addCaptionToImages(objStyleName, captionBoxHeight) {
// setting this explicitly so we can
// be sure what captionBoxHeight means!
app.scriptPreferences.measurementUnit = MeasurementUnits.PO
...
Copy link to clipboard
Copied
Something like this?
capset();
function capset() {
var mDoc = app.activeDocument,
//anchored or grouped items excluded
myGraphics = mDoc.splineItems.everyItem().getElements(),
picContainer,
gb,
myCaption;
while (picContainer = myGraphics.pop()) {
//exclude empty frames
if (!picContainer.graphics.length) continue;
gb = picContainer.geometricBounds;
//add a frame to to pictureās parent, which is a Page
myCaption = picContainer.parentPage.textFrames.add();
//set position and size of the caption
myCaption.geometricBounds = [gb[2], gb[1], gb[2] + 3, gb[3]];
// get xmp data
var img = picContainer.images[0],
lnk = img.itemLink,
imgTitle = lnk.linkXmp.documentTitle,
imgAuthor = lnk.linkXmp.author,
imgDescription = lnk.linkXmp.description;
//add placeholder contents
myCaption.contents = imgDescription;
mDoc.groups.add([picContainer, myCaption]);
}
}
Using the linkXmp property of the link.
- Mark
Copy link to clipboard
Copied
So on the money! And more. š
Thank you for taking me there. This is fantastic.
Copy link to clipboard
Copied
Hey Mark, where would you put a line of code for Paragraph or Object style?
Copy link to clipboard
Copied
// object style applied to frame
myCaption.applyObjectStyle(mDoc.objectStyles.itemByName('My Object Style'));
// paragraph style applied to paragraphs of (story of) frame
myCaption.paragraphs.everyItem().appliedParagraphStyle = mDoc.paragraphStyles.itemByName('My Style');
You can read about the method: 'applyObjectStyle" here. Also you can include the paragraph style with the object style, which is probably sensible in most cases.
- Mark
Copy link to clipboard
Copied
Thank you so much!
Copy link to clipboard
Copied
By the way, here's a list of some more linkXmp properties:
linkXmp.author
linkXmp.copyrightInfoURL
linkXmp.copyrightNotice
linkXmp.copyrightStatus
linkXmp.creationDate
linkXmp.creator
linkXmp.description
linkXmp.documentTitle
linkXmp.format
linkXmp.jobName
linkXmp.keywords
linkXmp.modificationDate
linkXmp.serverURL
Copy link to clipboard
Copied
Hi @MadMac55, just for learning purposes, here's a version of your script with a bit of error handling added. For example, the other script will throw an error if there isn't metadata for the linked image.
function main() {
addCaptionToImages('My Object Style', 30);
function addCaptionToImages(objStyleName, captionBoxHeight) {
// setting this explicitly so we can
// be sure what captionBoxHeight means!
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
captionBoxHeight = captionBoxHeight || 20;
var doc = app.activeDocument,
imgs = doc.allGraphics,
img,
captionBox;
while (img = imgs.pop()) {
try {
var frame = img.parent,
b = frame.geometricBounds,
// get metadata
lnk = img.itemLink,
filename = lnk.name,
imgTitle = lnk.linkXmp.documentTitle,
imgAuthor = lnk.linkXmp.author,
imgDescription = lnk.linkXmp.description;
} catch (error) {
/* metadata not available */
}
// exclude images that aren't at the root level of a page or spread
if (!(frame.parent.constructor.name == 'Page' || frame.parent.constructor.name == 'Spread')) continue;
// add a frame for the caption text
captionBox = img.parentPage.textFrames.add();
// set position and size of the caption
captionBox.geometricBounds = [b[2], b[1], b[2] + captionBoxHeight, b[3]];
//add caption text
captionBox.contents = imgDescription || filename + ': no metadata found.';
// object style
try {
if (objStyleName != undefined)
captionBox.applyObjectStyle(doc.objectStyles.itemByName(objStyleName));
} catch (error) {
/* object style not found in document */
}
// group
try {
doc.groups.add([frame, captionBox]);
} catch (error) {
/* can't group objects */
}
}
}
} // end main
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Caption To Images');
Copy link to clipboard
Copied
This is just better than great!! I am experimenting with where to insert the name of the object style as my initial guess is off.
captionBox.applyObjectStyle(doc.objectStyles.itemByName(name of style goes here));
Maybe i have to define that above.
Copy link to clipboard
Copied
The link to Jongware's API really helped also. I have it running with the object style now. Cant thank you enough. :-). Great lesson right here. I know others will appreciate this thread.
Copy link to clipboard
Copied
In this version I moved it out of the function so you can specify when you call the function. Look for 'My Object Style' string passed in to the function (close to top).
Copy link to clipboard
Copied
Well, Mark, this thread just keeps getting better and better! š