Copy link to clipboard
Copied
I've built a script to create my own paragraph styles for caption, and the next step is to apply them. Is there a way to Caption Setup "Paragaraph Style" and Layer via script? I could only find documentation for CS6, and reference to captions in online resources is fairly limited.
My captions are always formatted with underscores, so if there isn't a way I can findGrep and apply paragraph styles that way if I have to, though it may initally format incorrectly.
Copy link to clipboard
Copied
It probably can, but you'd have to give us a bit more info. Some screnshots of before and after situations would be useful.
Copy link to clipboard
Copied
Sure! All I'm trying to do is edit "Object > Captions > Captions Setup" from:
To:
Basically I'm trying to change Paragraph Style and Layer to the preferences showin in the current active document. Is it possible?
Copy link to clipboard
Copied
Probably. You'd have to delve into the object model. Take an object-model viewer (e.g. https://www.indesignjs.de/extendscriptAPI/indesign-latest/#about.html) and look for 'caption'. See which properties are available to you.
Copy link to clipboard
Copied
I had been looking there, and the only thing that seems to come up is Caption Metadata Variable Preference.
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#CaptionMetadataVariablePreference.html#d1...
It may also be read only?
https://developer.adobe.com/indesign/dom/api/c/CaptionMetadataVariablePreference/
I always have a tough time figuring out where these things are in the model, and poke around using John Wundes "The Document Object Model Explorer". Meant for AI, but works for everything.
Copy link to clipboard
Copied
> I always have a tough time figuring out where these things are in the model,
You're not alone. . .
I had a look and indeed there's not a lot there. Not sure how to do what you want.
Copy link to clipboard
Copied
https://creativepro.com/files/kahrel/indesign/menu_actions.html
I did stumble upon this page I which looks like it's on your repository. The menu action I want is called "#LinksUIGenerateCaptionPrefsMenu". Not sure how to invoke without launching, though this approach has been useful before for other things like changing label colors on pages. I'm sure it's the type of dialog/menu that may make this impossible.
app.menuActions.item("$ID/#LinksUIGenerateCaptionPrefsMenu").invoke();
Copy link to clipboard
Copied
You can also use the action IDs 0x20624 and 0x2062B - those rarely change.
Copy link to clipboard
Copied
Hi @wckdtall , I do it by invoking the Generate Live Caption menu item to script the caption generation—not sure if there is a better way. This example gets the parent frame of every placed link, invokes the menu, uses the file name for the caption, and styles the caption with paragraph and object styles:
app.doScript(makeCaptions, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Captions');
/**
* Make a Live Caption for every placed link
*/
function makeCaptions(){
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
var d = app.activeDocument;
var lnks = d.links.everyItem().getElements()
for (var i = 0; i < lnks.length; i++){
lnks[i].parent.parent.select();
var makeCap = app.menuActions.itemByName("Generate Live Caption")
makeCap.invoke();
setCaptionMeta("Name");
setCaptionText(d.textFrames[0], 20, 5)
};
}
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
/**
* Sets the caption’s metadata
* @ param the metadata name to use
* @ return void
*/
function setCaptionMeta(n){
var tv = app.activeDocument.textVariables
for (var i = 0; i < tv.length; i++){
try {
var tvo = tv[i].variableOptions
if (tvo.constructor.name == "CaptionMetadataVariablePreference") {
tv[i].variableOptions.metadataProviderName = n
}
}catch(e) {}
}
}
/**
* Style the caption text frames
* @ param the link’s parent frame
* @ param the caption height
* @ param the caption offset
* @ return void
*/
function setCaptionText(tf, h, off){
tf.select();
var cStatic = app.menuActions.itemByName("Convert to Static Caption");
cStatic.invoke();
var b = tf.geometricBounds;
tf.geometricBounds = [b[0], b[1], b[0]+h, b[3]];
tf.textFramePreferences.insetSpacing = [off, 0, 0, 0];
var ps = makeParaStyle(app.activeDocument, "Caption");
ps.properties = {basedOn:"[No Paragraph Style]", appliedFont:"Myriad Pro Regular", pointSize:8, leftIndent:0};
tf.parentStory.appliedParagraphStyle = ps;
var ls = makeObjStyle(app.activeDocument, "Caption Frame");
ls.properties = {basedOn:"[None]", appliedParagraphStyle:ps, strokeColor:"[None]", strokeWeight:"0"};
tf.appliedObjectStyle = ls;
}
/**
* Makes a new named ObjectStyle
* @ param the document to add the style to
* @ param style name
* @ returns the new object style
*/
function makeObjStyle(d, n){
var os;
try {
d.objectStyles.add({name:n});
}catch(e) {
os = d.objectStyles.itemByName(n);
}
return d.objectStyles.itemByName(n);
}
/**
* Makes a new named Paragraph Style
* @ param the document to add the style to
* @ param style name
* @ returns the new paragraph style
*/
function makeParaStyle(d, n){
var ps;
try {
d.paragraphStyles.add({name:n});
}catch(e) {
ps = d.paragraphStyles.itemByName(n);
}
return d.paragraphStyles.itemByName(n);
}
before and after:
Copy link to clipboard
Copied
From the plug-in side: IID_ILINKCAPTIONPREFS has properties matching both groups of the dialog. E.g. from the bottom dialog group that are frame offset, paragraph style, layer and alignment.
The layer is stored by string, so don't rename the actual layer object after that.
The paragraph style is stored by ID.
There are "magic" strings such as "#CaptionSameLayerAsImage" and "#LinkInfoNameStr" - Rob, with your "Name" you eventually run into localization issues.
For the missing script properties I'd suggest you file a feature request via Uservoice.
Otherwise the approach by Rob looks ok. There is nothing special about the result from the generate caption menu action, poking around after the run will do.
Copy link to clipboard
Copied
Thanks! Is there a way to invoke ID_ILINKCAPTIONPREFS not via SDK/plugin? Rob's method seems to work well, but I'm simply trying to set the options as part of document set up. I may eventually automate running captions automatically, but mostly it can't tell if you've already created a caption, and live captions don't allow for any grep manipulation.