Adobe InDesign script to affect only selected Table/Text Frame - scripting

I am new to scripting and copied this one below and it works great but not all tables are the same in the document and I just want to affect the selected tables/text frames.
Is there an easy way to make this code work the way I am looking to do.
var myDoc = app.activeDocument;
var myWidths = [.5,.35,.44,.44];
for(var T=0; T < myDoc.textFrames.length; T++){
for(var i=0; i < myDoc.textFrames[T].tables.length; i++){
for(var j=0; j < myWidths.length; j++){
myDoc.textFrames[T].tables[i].columns[j].width = myWidths[j];
}
}
}
Thanks for any help, just starting to dive into InDesign Scripting and understand it.

Yes, it can be done quite easy:
var myWidths = [.5,.35,.44,.44];
var sel = app.selection;
if (sel.length != 1) exit();
var frame = sel[0];
if (frame.constructor.name != 'TextFrame') exit();
for (var i = 0; i < frame.tables.length; i++) {
for (var j = 0; j < myWidths.length; j++) {
frame.tables[i].columns[j].width = myWidths[j];
}
}
It will work for one selected text frame.
If you need to process several selected frames here is another variant of the code:
var myWidths = [.5,.35,.44,.44];
var frames = app.selection
var f = frames.length
while(f--) {
if (frames[f].constructor.name != 'TextFrame') continue;
var tables = frames[f].tables;
var t = tables.length;
while(t--) {
var table = tables[t];
var c = table.columns.length;
while(c--) {
table.columns[c].width = myWidths[c];
}
}
}

Related

Change line spacing in a cell in a Google Doc using Google App Script

I have a Google Doc which has a table that needs updating weekly and content to be inserted from a spreadsheet. I successfully paste in the correct data into each cell but the lines are spaced at 1.15 not single spaced.
Any ideas on how to accomplish this?
Here is the code I have tried:
const doc = DocumentApp.getActiveDocument();
var paddingTop = 1.5; // You can adjust the height by modifying this.
var paddingBottom = 1.5; // You can adjust the height by modifying this.
var tables = doc.getBody().getTables();
tables.forEach(table => {
for (var r = 0; r < table.getNumRows(); r++) {
var row = table.getRow(r);
for (var c = 0; c < row.getNumCells(); c++) {
row.getCell(c).setPaddingTop(paddingTop).setPaddingBottom(paddingBottom);
var p=row.getCell(c);
Logger.log(c)
for(i=0;i<p.length; i++){
p[i].setLineSpacing(100);
}
}
}
});
This does not change the line spacing inside the cells at all.
This is what worked for me:
var paddingTop = 1.5; // You can adjust the height by modifying this.
var paddingBottom = 1.5; // You can adjust the height by modifying this.
var paraStyle = {};
paraStyle[DocumentApp.Attribute.LINE_SPACING] = 1;
var tables = doc.getBody().getTables();
tables.forEach(table => {
for (var r = 0; r < table.getNumRows(); r++) {
var row = table.getRow(r);
for (var c = 0; c < row.getNumCells(); c++) {
var cell=row.getCell(c);
cell.setPaddingTop(paddingTop)
cell.setPaddingBottom(paddingBottom);
var items = cell.getNumChildren();
for (var i = 0; i < items; i++){
var paraInCell = cell.getChild(i).asParagraph();
paraInCell.setAttributes(paraStyle);
}
}
}
});

Same variable for multiple sheets but only execute if export button on one sheet is pressed

I am trying to code an data export. The export is executed when a button is pressed and the script linked to the button will be executed. The data is transfered to a masterdata sheet.
The script is valid for 4 different sheets and i am trying to code it, so that i do not have to rename all the vaiables for each sheet.
I found some solution that i tried, by creating a variable with the sheet names and then call this variable in the following code.
Problem 1: Error "TypeError: Cannot read property 'copyTo' of null" occurs.
Problem 2: How to ensure that only the data from the sheet where the export button is pressed will be exported?
I tried a lot and cannot find a solution.
var sheetListArray = ["B1-Prozessfragen", "B2-Prozessfragen"];
var ss = SpreadsheetApp.getActiveSpreadsheet();
for( var k = 1 ; k <= sheetListArray.length ; k++)
ExportData1g (sheetListArray[k]);
PB1g (sheetListArray[k]);
function ExportData1g (sheetname) {
var result = SpreadsheetApp.getUi().alert("Fertig-Hacken gesetzt?", SpreadsheetApp.getUi().ButtonSet.OK_CANCEL); {
if (result === SpreadsheetApp.getUi().Button.OK) {
const ssh=ss.getSheetByName(sheetname);
const cell = ssh.getRange("CheckButton1").getValue();
if (cell == "1" || "0") {
(PB1g ());
}
else {return;} }
else {return;}
}
const ssh=ss.getSheetByName(sheetname);
var dataRange = ssh.getRange('CheckBox1');
var values = dataRange.getValues();
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
if (values[i][j] == true) {
values[i][j] = false;
}}
dataRange.setValues(values);
var commentRange = ssh.getRange ('ResetComment1');
var dataRange = ssh.getRange('EmptyBoxes1');
var values = dataRange.getValues();
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
if (values[i][j] == 1) {
values[i][j] = 0;
}
}
}
commentRange.clearContent();
dataRange.setValues(values);
} }
function PB1g(sheetname) {
const ssh=ss.getSheetByName(sheetname);
const tss=SpreadsheetApp.openById("1wMw5H48Fmc1R8WGy34d80x9-W996c9GNVuAD3-mFtVQ");
const tsh=tss.getSheetByName("PB");
const nsh=ssh.copyTo(tss);
var firstEmtyRow = tsh.getLastRow () + 1;
var timestamp = new Date();
tsh.appendRow([timestamp]);
nsh.getRange("CopytoMasterdata1").copyTo(tsh.getRange("B" + firstEmtyRow), {contentsOnly:true});
tss.deleteSheet(nsh); }

Loop over path points in Photoshop

I'm trying to iterate over a create path in Photoshop finding out the anchor points position etc
var srcDoc = app.activeDocument;
// create the array of PathPointInfo objects
var lineArray = new Array();
lineArray.push(new PathPointInfo());
lineArray[0].kind = PointKind.CORNERPOINT;
lineArray[0].anchor = new Array(20, 160);
lineArray[0].leftDirection = [35, 200];
lineArray[0].rightDirection = lineArray[0].anchor;
lineArray.push(new PathPointInfo());
lineArray[1].kind = PointKind.CORNERPOINT;
lineArray[1].anchor = new Array(20, 40);
lineArray[1].leftDirection = lineArray[1].anchor;
lineArray[1].rightDirection = [220, 260];
// create a SubPathInfo object, which holds the line array in its entireSubPath property.
var lineSubPathArray = new Array();
lineSubPathArray.push(new SubPathInfo());
lineSubPathArray[0].operation = ShapeOperation.SHAPEXOR;
lineSubPathArray[0].closed = false;
lineSubPathArray[0].entireSubPath = lineArray;
//create the path item, passing subpath to add method
var myPathItem = srcDoc.pathItems.add("A Line", lineSubPathArray);
for (var i = 0; i < lineSubPathArray[0].entireSubPath.length; i++)
{
var b = lineSubPathArray[0].entireSubPath[i].anchor;
alert(b);
}
This works fine, but instead of creating the path and finding out it's information I want to loop over each path and get the same. This should be the same as the loop above only without explicitly calling lineSubPathArray and its parts.
for (var i = 0; i < srcDoc.pathItems[0].subPathItems.pathPoints.length; i++) // wrong I think
{
var b = srcDoc.pathItems[0].entireSubPath[i].anchor; // wrong
alert(b);
}
Almost: you need to iterate through subPathItems which consist of pathPoints:
var srcDoc = activeDocument;
var workPath = srcDoc.pathItems[0];
var i, k, b;
for (i = 0; i < workPath.subPathItems.length; i++) {
for (k = 0; k < workPath.subPathItems[i].pathPoints.length; k++) {
b = workPath.subPathItems[i].pathPoints[k].anchor;
alert(b);
}
}

InDesign copy layer to another document

I am trying to write a script that copies a layer from one document into another.
var srcDocName = 0;
var destDocName = 1;
var layerNameOriginal = "Original";
var layerNameCopyTo = "Destination";
var destDoc = app.documents.item(destDocName);
var layerSrc = app.documents.item(srcDocName).layers.item(layerNameOriginal);
try {
layerSrc.duplicate(destDoc, ElementPlacement.INSIDE);
}
catch(e) {
alert(e)
}
Apparently this works in Photoshop but not in InDesign. I have been trying for ages to find some decent documentation for InDesign scripting. But all I can find is the CS scripting guide, which isn't of much use.
http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/products/indesign/pdfs/InDesignCS5_ScriptingGuide_JS.pdf
If someone can point me to a good reference to the object model I would be grateful.
After some more googling I finally found the answer:
var sourceLayer = app.documents[0].layers.itemByName("Layer1");
var destLayer = app.documents[1].layers[0];
sourceLayer.pageItems.everyItem().duplicate(destLayer);
I also came across jongware which seems to be a complete Object reference extracted directly out of Adobe CS.
You can use this script: https://redokun.com/blog/indesign-copy-entire-layer-one-file-another
The underlying implementation is basically the same, but we've added a UI so it's not necessary to edit the script every time the layer name changes.
Edit: We've been told that the solution above doesn't work with threaded text frames, so I re-wrote the script. The new implementation is way more complex but it now supports threaded TFs.
To expand on the solution offered by Loopo and offer you the ability to copy all layers from 1 document to another...
main();
function main()
{
var source = GetSourceDocument();
if(source == -1)
{
return;
}
var target = GetTargetDocument ();
if(target == -1)
{
return;
}
if(target == source)
{
return;
}
copyLayersOver(source, target);
}
function GetSourceDocument()
{
var returnVal = -1;
var oldPrefs = app.scriptPreferences.userInteractionLevel;
app.scriptPreferences.userInteractionLevel=UserInteractionLevels.INTERACT_WITH_ALL;
var dialog = app.dialogs.add({name:"Document to Copy From", canCanel: true, label:"DocumentToCopyFrom"});
var col1 = dialog.dialogColumns.add();
var StringList= [];
for(var i = 0; i<app.documents.length; i++)
{
StringList.push("[" + app.documents[i].index + "] " + app.documents[i].name);
}
var ddl = col1.dropdowns.add({id:"SourceDocDDL", stringList: StringList});
if(dialog.show() == true)
{
returnVal = ddl.stringList[ddl.selectedIndex].split("]")[0].substr(1);
}
else
{
returnVal -1;
}
dialog.destroy();
app.scriptPreferences.userInteractionLevel = oldPrefs;
return returnVal;
}
function GetTargetDocument()
{
var returnVal = -1;
var oldPrefs = app.scriptPreferences.userInteractionLevel;
app.scriptPreferences.userInteractionLevel=UserInteractionLevels.INTERACT_WITH_ALL;
var dialog = app.dialogs.add({name:"Document to Copy To", canCanel: true, label:"DocumentToCopyTo"});
var col1 = dialog.dialogColumns.add();
var StringList= [];
for(var i = 0; i<app.documents.length; i++)
{
StringList.push("[" + app.documents[i].index + "] " + app.documents[i].name);
}
var ddl = col1.dropdowns.add({id:"SourceDocDDL", stringList: StringList});
if(dialog.show() == true)
{
returnVal = ddl.stringList[ddl.selectedIndex].split("]")[0].substr(1);
}
else
{
returnVal -1;
}
dialog.destroy();
app.scriptPreferences.userInteractionLevel = oldPrefs;
return returnVal;
}
function copyLayersOver(source, target)
{
var sourceDocument = app.documents[source];
var targetDocument = app.documents[target];
var sourceLayers = sourceDocument.layers;
//Match the number of pages
while(targetDocument.pages.length < sourceDocument.pages.length)
{
targetDocument.pages.add();
}
//copy the layers over
for(var i= 0; i < sourceLayers.length; i++)
{
var names = targetDocument.layers.everyItem().name;
var merge = false;
for(var y = 0; y < names.length; y++)
{
if(names[y] == sourceLayers[i].name)
{
merge = true;
break;
}
}
if(merge)
{
var targetLayer = targetDocument.layers.add();
targetLayer.name = "temp";
sourceLayers[i].pageItems.everyItem().duplicate(targetLayer);
targetDocument.layers.itemByName(sourceLayers[i].name).merge(targetLayer);
}
else
{
var targetLayer = targetDocument.layers.add();
targetLayer.name = sourceLayers[i].name;
targetLayer.layerColor = sourceLayers[i].layerColor;
sourceLayers[i].pageItems.everyItem().duplicate(targetLayer);
}
}
}

How to use aiBringToFront in Illustrator Extendscript?

In Illustrator, I want to create a function in one of my scripts for Bring to Front. In the CS5 Type Library, I find the object, AiZOrderMethod.aiBringToFront, however, I can't figure out how to use it.
Here is what I've come up with so far:
//BringToFront
mySelection = activeDocument.selection;
if (mySelection.length>0){
var doc = app.activeDocument; //current document
var s = doc.selection; //current slection
var sl = s.length; //number of selected objects
s.AiZOrderMethod.aiBringToFront();
//for(var i = 0 ; i < sl; i++) s[i].aibringtofront(); //for each selected element...
app.redraw();
}else{
alert("Nothing selected!")
}
app.activeDocument.selection is an Array
Try this:
var doc = app.activeDocument; //current document
var sel = doc.selection; // array
var sl = sel.length; //number of selected objects
if (sl>0){
for(var i = 0 ; i < sl; i++){
// for every item in selection array
sel[i].zOrder(ZOrderMethod.BRINGTOFRONT);
}
app.redraw();
}else{
alert("Nothing selected!")
}