Run a function on each selected frame in turn
Hi all,
I've made a script that places a text caption to either the side of a graphic frame or below it. At the moment, it only works with a single, selected graphic frame (must have an image inside it to capture the metadata). But I've hit a brick wall trying to get the script to loop when more than one frame is selected.
Here's the script before I started trying to loop it:
var doc = app.activeDocument;
var myPage = app.activeWindow.activePage;
var mySelection = app.selection[0];
var savedUnits = app.scriptPreferences.measurementUnit;
var filename = mySelection.graphics[0].itemLink.name;
var textFrameHeight = 10;
var textFrame = myPage.textFrames.add();
var captionText = filename.replace(/(.*)(\..*)/i, "$1");
var b = mySelection.geometricBounds;
var frameWidth = b[3]-b[1];
var frameHeight = b[2]-b[0];
var objectStyle = doc.objectStyles.itemByName("Photo Caption align base");
//THE DIALOG
var myList = ["Below", "Side"];
var myDialog = app.dialogs.add({name:"Credit/Caption", canCancel:true});
with (myDialog )
with (dialogColumns.add())
with(borderPanels.add())
{
staticTexts.add ({staticLabel:"Select:"});
var myDropSelection = dropdowns.add({stringList:myList, selectedIndex:1});
}
if(myDialog.show() == true){
if(myDropSelection.selectedIndex == 0){
baseCaption();
}
else if(myDropSelection.selectedIndex == 1){
sideCaption();
}
myDialog.destroy();
}
function baseCaption(){
// add a text frame for the caption below the graphic
textFrame.geometricBounds = [b[2], b[1], b[2] + textFrameHeight, b[3]];
//add caption text
textFrame.contents = captionText;
try {
if(objectStyle.isValid)
textFrame.applyObjectStyle(objectStyle);
} catch (error) {
/* object style not found in document */
}
}
function sideCaption(){
// add a text frame to the right-hand side of the graphic
//[y1, x1, y2, x2] - coordinates of the top-left and bottom-right corners
//[0, 1, 2, 3] - array positions for coordinates
textFrame.geometricBounds =
[b[2] ,b[1] + frameWidth, b[2] + textFrameHeight, b[3] + frameHeight];
textFrame.properties = {
rotationAngle : 90
};
//fill it with the caption text
textFrame.contents = captionText;
try {
if(objectStyle.isValid)
textFrame.applyObjectStyle(objectStyle);
} catch (error) {
/* object style not found in document */
}
}If some kind soul can offer any advice, that would be great.
Many thanks, J
