How to import a layer from one document into a new untitled document?

New Here ,
Jul 27, 2022 Jul 27, 2022

Copy link to clipboard

Copied

I have an indesign file that I use as a template for other indesign files. The file has one layer that is a tile page which I copy and paste onto other documents. I'd like to write a script that lauches a new document with that layer already brought over from my template. I have some ideas but I'm getting stuck.

 

In short, I can create a new document by writing:

 

 

var myDocument = app.documents.add()

 

 

But I'm not sure how to properly call for the layer from my template file and then paste it into my new document. I know I can open the template file using:

 

 

app.open(File('C:/Users/userprofile/Documents/Template_file.indd'))

 

 

And then start working in that file... but I don't what to do that because then I may accidentally save over the template file.

 

I need to know if I can reference an indesign file and its layers without actually opening it. 

 

Another option would be to open the template file but as a new untiled document? If thats possible?

 

Any ideas are appreciated.

 

Thanks,

 

Ben

TOPICS
Scripting

Views

85

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Adobe Community Professional , Jul 27, 2022 Jul 27, 2022

You can open the template file as a copy: 

 

app.open(File('C:/Users/userprofile/Documents/Template_file.indd')), true, OpenOptions.OPEN_COPY);

Or just save the doc as an INDT file. 

 

Likes

Translate

Translate
Adobe Community Professional ,
Jul 27, 2022 Jul 27, 2022

Copy link to clipboard

Copied

You can open the template file as a copy: 

 

app.open(File('C:/Users/userprofile/Documents/Template_file.indd')), true, OpenOptions.OPEN_COPY);

Or just save the doc as an INDT file. 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 27, 2022 Jul 27, 2022

Copy link to clipboard

Copied

I see it as important that all of my documents have the same name and same order to be able to copy and paste from one document to the other as layer names are remembered, their order not.

  1. Turn on in layer panel menu Paste remembers Layers.
  2. Build up all needed layers in the right order.
  3. Put a small object (empty frame) in the most bottom layer.
  4. Hold down the alt key and move the object’s proxy from one layer to next and copy it.
  5. Select all the objects and add them to the CC Library
  6. In any new document drag this Library Element to the page and delete it immediately. All layers are added.
  7. Delete the generic Layer 1

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 28, 2022 Jul 28, 2022

Copy link to clipboard

Copied

@Ben-ind said: "I'd like to write a script that lauches a new document with that layer already brought over from my template."

 

Hi Ben,

do you want just an additional layer with the layer's name from your template?

Or do you also want other properties of this layer like layer color?

 

See into DOM documentation, into properties with read/write access:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Layer.html

 

Note: Method move() with object layer cannot move a layer from one document to another.

It just moves the layer in the layer stack of the same document.

 

If both documents are open, no other document, and your template document is not the active one, you could act like this where the new document is the target and the source, your template is not the currently active document.

 

If the layer is not present in the target document, so targetLayer is not valid, the script will add a new layer in the target with the same properties of the source layer. If a layer with the same name is already in the target document, it will update all properties from the source layer:

 

 

var targetDoc = app.documents[0];
var sourceDoc = app.documents[1];

var layerName = "MyLayerName";

var sourceLayer = sourceDoc.layers.itemByName( layerName );

if( !sourceLayer.isValid )
{
	alert( "ERROR: Layer of source not found." );
	exit();
};

var targetLayer = targetDoc.layers.itemByName( layerName );

if( !targetLayer.isValid )
{
	targetDoc.layers.add
	(
		sourceLayer.properties
	);
}
else
{
	targetLayer.properties = sourceLayer.properties
};

 

 

IMPORTANT: What this does not handle is the stacking order of layers in the Layers panel.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

 

EDITED code and added a direct test for the layer in the source document.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jul 28, 2022 Jul 28, 2022

Copy link to clipboard

Copied

LATEST

Hi Ben,

just updated the code in my reply above.

Another question:

 

Should objects on your source layer from the template document come along when you "import" the layer to a new document? That would require extra steps and perhaps a different strategy.

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines