Sorry, I don't have time to explain, but this code works! //Refr //https://indisnip.wordpress.com/2010/09/02/quicktip-insert-text-variable-to-text-frame/ //DESCRIPTION:Label image with its size // Jongware, 13-Jan-2012 // No Guarantees, Etc. // https://indesignsecrets.com/topic/script-to-label-selection-with-height-and-width if (app.selection[0] instanceof Rectangle) //Check if selected object is a graphic frame (I can't find a way to check if it is a linked image { app.doScript(labelItem, ScriptLanguage.JAVASCRIPT, app.selection[0], UndoModes.ENTIRE_SCRIPT, "Label Image Size"); } else { alert ("No valid selection.\nPlease select an image with the black arrow before running this script."); } function labelItem (item) { var myDocument = app.activeDocument; //Define variables as text-variables from the document var tvDim = app.activeDocument.textVariables.item("dim"); var tvPPI = app.activeDocument.textVariables.item("PPI"); var tvPPIb = app.activeDocument.textVariables.item("PPI b"); var tvProsent = app.activeDocument.textVariables.item("prosent"); //Get name of image, without file type var mySel = app.selection[0]; var myImageName = mySel.graphics[0].itemLink.name; var myImageName = myImageName.slice(0,-4); var myTextFrame = myDocument.textFrames.item(myImageName); // Save current selected layer because adding one will make it active! activeLayer = app.activeDocument.layoutWindows[0].activeLayer; // Make sure the layer "bildenavn" exists try { app.activeDocument.layers.add({name:"bildenavn", ignoreWrap:true, layerColor: [90,192,90], printable: true}); } catch (_) { } reportLayer = app.activeDocument.layers.item("bildenavn"); // Make sure the swatch "bildenavn" exists try { app.activeDocument.colors.add({name:"bildenavn", space:ColorSpace.RGB, colorValue:[90,192,90], colorModel:ColorModel.PROCESS}); } catch (_) { } reportGreen = app.activeDocument.swatches.item("bildenavn"); // Make sure the paragraph style "bildeinfo" exists try { app.activeDocument.paragraphStyles.add({name:"bildeinfo", appliedFont:"Kozuka Gothic Pro", pointSize:18, fillColor:[Paper], linecoler:[black], justification: Justification.CENTER_ALIGN}); } catch (_) { } reportStyle = app.activeDocument.paragraphStyles.item("bildeinfo"); // Make sure measurement units are in mm hmmu = app.activeDocument.viewPreferences.horizontalMeasurementUnits; vmmu = app.activeDocument.viewPreferences.verticalMeasurementUnits; app.activeDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.MILLIMETERS; app.activeDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.MILLIMETERS; // .. and Smart Quotes is Off sqt = app.activeDocument.textPreferences.typographersQuotes; app.activeDocument.textPreferences.typographersQuotes = false; // Save all of the previous settings to be able to restore them later :) // fetch width and height for the numbers, left and bottom for the lines gb = item.geometricBounds; // gb now holds [ top, left, bottom, right ] values -- get shorthand values top = gb[0]; left = gb[1]; bottom = gb[2]; right = gb[3]; width = Math.round(Math.abs(right - left)*1000)/1000; height= Math.round(Math.abs(bottom - top)*1000)/1000; // Retrieve the current page. // Actually, in CS5 and newer, page items have a property for this (parentPage), // but I don't have that one near, and it's also nicer to make it work in CS4 // (and possibly even in older versions). pg = item.parent; while (1) { if (pg instanceof InsertionPoint) pg = pg.parentTextframes[0]; if (pg instanceof Document || pg instanceof Spread || pg instanceof Page) break; pg = pg.parent; } // Draw text frame #1: Height // Put it at the left of the selection, 0.5" wide and 0.2" off the left side frh = pg.textFrames.add(reportLayer, {geometricBounds:[ top +10, left +10, top +55, left + 80 ], name: myImageName, textFramePreferencestextFramePreferences: {verticalJustification: VerticalJustification.CENTER_ALIGN, ignoreWrap: true } }); // ... and put text into it. frh.insertionPoints[0].appliedParagraphStyle = reportStyle; //Insert these text variables into the image's text frame myTextFrame.texts[0].textVariableInstances.add({associatedTextVariable:tvDim}); myTextFrame.parentStory.insertionPoints.item(-1).contents = "\rEffektiv PPI: "; myTextFrame.texts[0].textVariableInstances.add({associatedTextVariable:tvPPI}); myTextFrame.parentStory.insertionPoints.item(-1).contents = "\rBildets PPI: "; myTextFrame.texts[0].textVariableInstances.add({associatedTextVariable:tvPPIb}); myTextFrame.parentStory.insertionPoints.item(-1).contents = "\rSkala: "; myTextFrame.texts[0].textVariableInstances.add({associatedTextVariable:tvProsent}); //Make the result of the text variabelts into integer var mySel = app.selection[0]; //var myVarValue = mySel.textVariableInstances.item("File Name").resultText; var myDim = myDocument.textFrames.item(myImageName).textVariableInstances.item("dim").resultText; var myPPI = myDocument.textFrames.item(myImageName).textVariableInstances.item("PPI").resultText; var myPPIb= myDocument.textFrames.item(myImageName).textVariableInstances.item("PPI b").resultText; var myProsent = myDocument.textFrames.item(myImageName).textVariableInstances.item("prosent").resultText; var myWidth = myDim.slice(0,4); var intMyWidth = parseInt(myWidth); var intPPI = parseInt(myPPI); var intPPIb = parseInt(myPPIb); var myProsent = myProsent.slice(0,-1); var intProsent = parseInt(myProsent); var newWidth = intMyWidth * intProsent * 300 / intPPIb / 100; var newWidth = Math.ceil(newWidth) //Remove var myMetadataString = "\nBredde " + intMyWidth + "\nNy bredde " + newWidth; frh.parentStory.insertionPoints.item(-1).contents = String(myMetadataString); // Restore old layer selection app.activeDocument.layoutWindows[0].activeLayer = activeLayer; // Restore previous global settings app.activeDocument.viewPreferences.horizontalMeasurementUnits = hmmu; app.activeDocument.viewPreferences.verticalMeasurementUnits = vmmu; app.activeDocument.textPreferences.typographersQuotes = sqt; }
... View more