Skip to content

Instantly share code, notes, and snippets.

@bastien
Created June 21, 2012 09:23
Show Gist options
  • Save bastien/2964807 to your computer and use it in GitHub Desktop.
Save bastien/2964807 to your computer and use it in GitHub Desktop.
Extracting a page from an InDesign document (2 different approaches)
var doc = app.activeDocument;
if(doc.saved == true)
{
extract(2);
}
else
{
alert("Save you file.");
}
function extract(pagePosition){
var docpath = doc.fullName;
for(var i=doc.pages.length; i>pagePosition;i--)
{
doc.pages[i-1].remove();
}
for(var i=pagePosition-1; i>=1;i--)
{
doc.pages[i-1].remove();
}
var exportdoc = doc.save(File('~/test_extract.indd'));
exportdoc.close(SaveOptions.no);
app.open(File(docpath));
}
var doc = app.activeDocument;
if(doc.saved == true)
{
extract(2);
}
else
{
alert("Save you file.");
}
function extract(pagePosition){
var exportDoc = app.documents.add(false);
var page = doc.pages.item(pagePosition-1)
page.duplicate(LocationOptions.BEFORE, exportDoc.pages.firstItem());
exportDoc.pages.lastItem().remove();
exportDoc.save(File('~/test_extract_2.indd'));
exportDoc.close(SaveOptions.no);
}
@strarsis
Copy link

strarsis commented Mar 11, 2018

@bastien: Many thanks! This script is very helpful. Especially method 2 is very fast (and less intrusive than method 1).

For extracting all pages into a folder (plus progressbar from https://gist.github.com/kuyseng/2792080):

// https://gist.github.com/bastien/2964807
// https://gist.github.com/kuyseng/2792080

var doc = app.activeDocument;
if(doc.saved == true) {
  var pb = new index_progress_bar('Extracting...');
  pb.reset(doc.pages.length);

  // Extract each page (all pages)
  for(var i=0; i<doc.pages.length; i++) {
	pb.show('Pages', (i+1).toString() + ' of ' + doc.pages.length.toString(), (i+1));
    extract2(i);
  }

  pb.close();
} else {
  alert('Save you file.');
}

function extract2(pagePosition){
  var exportDoc = app.documents.add(false);
  var page = doc.pages.item(pagePosition-1)
  page.duplicate(LocationOptions.BEFORE, exportDoc.pages.firstItem());
  exportDoc.pages.lastItem().remove();
  var path = 'C:/some-path/extracted/' + (pagePosition+1).toString() + '.indd';
  exportDoc.save(File(path));
  exportDoc.close(SaveOptions.no);
}

function index_progress_bar(title) {
	var w = new  Window("palette", title, undefined);
		w.orientation = "column";
	var text_group = w.add("group");
		text_group.orientation = "column";
	var	file_name = text_group.add("statictext", [0,0,350,20], "calculating..");
	var remain = text_group.add("statictext", [0,0,350,20], "Remain: ..");
		text_group.alignChildren = "left";
	var progressbar = w.add("progressbar", undefined, 0, 100);
		progressbar.preferredSize = [350,20];  	

	this.reset = function(maxValue) {		
		progressbar.value = 0;
		progressbar.maxvalue = maxValue||0;
		progressbar.visible = !!maxValue;
		w.show();		
	};
	
	this.show = function(name, remain_string, value) {
		file_name.text = name;
		remain.text = remain_string;
		progressbar.value = value;
	};
	
	this.hide = function() {w.hide();};
	this.close = function() {w.close();};	
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment