Created
November 8, 2016 12:51
-
-
Save agopaul/280accee6b5434ad5bcabfde546984c6 to your computer and use it in GitHub Desktop.
Layer cloner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #target indesign | |
| #include "./../lib/underscore.js"; | |
| #include "./../lib/sprintf.js"; | |
| /* | |
| * Layer cloner | |
| * Redokun Srls 2016 | |
| * | |
| * See: https://redokun.com/blog/indesign-copy-entire-layer-one-file-another | |
| * | |
| * Dependencies: | |
| * | |
| * - http://underscorejs.org/ | |
| * - https://github.com/alexei/sprintf.js/ | |
| * | |
| * @license MIT (http://www.opensource.org/licenses/MIT) | |
| * @author Paolo Agostinetto <[email protected]> | |
| */ | |
| const VERSION = '1.3.2'; | |
| var isDebug = false; | |
| var allDocs = app.documents; | |
| if (allDocs.length > 1) { | |
| var activeDoc = app.activeDocument; | |
| var itemLayer, itemDoc, go = false; | |
| // Exclude current doc | |
| var otherDocs = _.filter(allDocs, function (doc) { | |
| return doc != activeDoc; | |
| }); | |
| // Get all doc names | |
| var docNames = _.map(otherDocs, function(doc){ | |
| return doc.name; | |
| }); | |
| // Get all layers names | |
| var layersNames = _.map(activeDoc.layers, function(layer){ | |
| return layer.name; | |
| }); | |
| // Create window | |
| var win = new Window("dialog", sprintf("Layer cloner v%s (redokun.com)", VERSION)); | |
| win.spacing = 15; | |
| this.windowRef = win; | |
| win.TxtTitle = win.add("statictext", undefined, "Copy layer to other document"); | |
| win.TxtTitle.alignment = "left"; | |
| win.Panel1 = win.add('panel', undefined, ''); | |
| var panel = win.Panel1; | |
| panel.spacing = 15; | |
| // Layer selection | |
| panel.Txt1 = panel.add("statictext", undefined, "Select source layer"); | |
| panel.Txt1.alignment = "left"; | |
| panel.LayerList = panel.add("dropdownlist", undefined, layersNames); | |
| panel.LayerList.alignment = "left"; | |
| panel.LayerList.selection = 0; | |
| itemLayer = panel.LayerList.selection.index; | |
| panel.LayerList.onChange = function() { | |
| itemLayer = panel.LayerList.selection.index; | |
| return itemLayer; | |
| }; | |
| // Doc selection | |
| panel.Txt2 = panel.add("statictext", undefined, "Select destination document"); | |
| panel.Txt2.alignment = "left"; | |
| panel.NewList = panel.add("dropdownlist", undefined, docNames); | |
| panel.NewList.alignment = "left"; | |
| panel.NewList.selection = 0; | |
| itemDoc = panel.NewList.selection.index; | |
| panel.NewList.onChange = function() { | |
| itemDoc = panel.NewList.selection.index; | |
| return itemDoc; | |
| }; | |
| // Window actions | |
| panel.Group01 = panel.add("group", undefined); | |
| panel.Group01.alignment = "fill"; | |
| panel.Group01.cancelBtn = panel.Group01.add("button", undefined, "Cancel"); | |
| panel.Group01.cancelElement = panel.Group01.cancelBtn; | |
| panel.Group01.quitBtn = panel.Group01.add("button", undefined, "Ok"); | |
| panel.Group01.quitBtn.onClick = function() { | |
| go = true; | |
| win.close(); | |
| }; | |
| // Footnote text (credits) | |
| win.TxtCredit1 = win.add("statictext", undefined, sprintf("Layer cloner (v%s)", VERSION)); | |
| win.TxtCredit2 = win.add("statictext", undefined, "For any inquiries reach us at: [email protected]"); | |
| win.TxtCredit3 = win.add("statictext", undefined, "Redokun 2016 (https://redokun.com)"); | |
| // Show | |
| win.show(); | |
| // Copy | |
| if(go){ | |
| /* | |
| * Var | |
| */ | |
| var destDoc = otherDocs[itemDoc]; | |
| var sourceLayer = activeDoc.layers[itemLayer]; | |
| // Check if layer already exists | |
| var hasLayer = destDoc.layers.itemByName(sourceLayer.name); | |
| if(!isDebug && hasLayer.isValid){ | |
| var errStr = sprintf("Layer '%s' already exists inside target document", sourceLayer.name); | |
| errorAlert(errStr); | |
| } | |
| else { | |
| // New layer | |
| var destLayer; | |
| if(isDebug && hasLayer && hasLayer.isValid){ | |
| destLayer = hasLayer; | |
| } | |
| else{ | |
| destLayer = destDoc.layers.add({name: sourceLayer.name}); | |
| } | |
| var linkMap = [], // Old Item -> prev old Item, new old Item | |
| tfMap = {}; // Old Item -> New Item | |
| /* | |
| * Copy Items | |
| */ | |
| var item, destItem, | |
| elementCount = sourceLayer.pageItems.length; | |
| // Copy them | |
| for(var i = 0; i < elementCount; i++){ | |
| item = sourceLayer.pageItems.item(i); | |
| tfMap[item.id] = destItem = item.duplicate(destLayer); | |
| if(typeof item.previousTextFrame != "undefined" && item.previousTextFrame && item.textFrameIndex > 0){ | |
| linkMap.push({ | |
| item: item, | |
| prev: item.previousTextFrame | |
| }); | |
| } | |
| } | |
| // Re-link threaded TFs | |
| repairTextFramesLinks(linkMap, tfMap); | |
| // Focus on new doc | |
| app.activeDocument = destDoc; | |
| alert("Done"); | |
| } | |
| } | |
| } | |
| else { | |
| errorAlert("Open the source and the destination file first. Execute the script when the source document is active."); | |
| } | |
| /** | |
| * Error dialog | |
| * | |
| * @param str | |
| */ | |
| function errorAlert(str){ | |
| alert(str, true); | |
| } | |
| /** | |
| * Relink TFs | |
| * | |
| * @param linkMap | |
| * @param copyMap | |
| */ | |
| function repairTextFramesLinks(linkMap, copyMap){ | |
| _.each(linkMap, function (element) { | |
| var oldItem = element.item; | |
| var parentItem = copyMap[oldItem.id]; | |
| var newPrevItem; | |
| if(element.prev){ | |
| newPrevItem = copyMap[element.prev.id]; | |
| parentItem.previousTextFrame = newPrevItem; // Set previous (and implicitly merge contents) | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment