Automating InDesign CS6


Scripting New Document _ Create Document and Save

Tuesday, April 30, 2013

With this blog we will polish off our script to automate the creation of a new document for print. At this point, we have the following functionality:

  • Get a list of document presets that are for print intent and the name does not start with a specified string (getPresets subroutine). The specified string is defined at the top of the script and is currently set with the value "DPS".
  • Get a list of style files found in a specified folder path (getStyleFileList). The folder path is set to be a path inside the application folder as defined by the variable styleFolderStr.
  • Present the user with a dialog in which to choose the style sheet, and the style import options.

It is with the user dialog that we now will make some changes. As long as we have the user entering information into a dialog, it may be a good idea to get some other information for our document. We can use this information for creating and saving the document. For this, we have added the following fields:

  • Project Name: enter the name of the document
  • Author Name: enter the name of the document's creator
  • Choose Preset: drop down with list of document presets
  • Number Pages: integer edit box for entering the number of pages for the document

Using this new information we can add more functionality to the script.

Create the Document

Using the document preset chosen, create the document. Set the number of pages for the document using number of pages returned from the dialog. The createDocumentFromPreset subroutine requires both the name of the preset and the number of pages (numPages).

AppleScript

(*Creates document from preset chosen and number of pages set in custom dialog*)
on createDocumentFromPreset(presetName, numPages)
	tell application "Adobe InDesign CS6"
		set presetRef to document preset presetName
		set docRef to make document with properties {document preset:presetRef}
		tell document preferences of docRef
			set pages per document to numPages
		end tell
	end tell
	return docRef
end createDocumentFromPreset

ExtendScript

/*Creates document from preset chosen and number of pages set in custom dialog*/
function createDocumentFromPreset(isVisible, presetName, numPages) {
    var presetRef = app.documentPresets.item(presetName);
    var docRef = app.documents.add (isVisible, presetRef);
    docRef.documentPreferences.pagesPerDocument = numPages;
    return docRef;
}

Get Date of Publication

When the document is saved, we will append the date of publication to the name of the file. We will also use this date for creating the document's job name metadata.

AppleScript

(*Returns current date in form mm/dd/yyyy*)
on getPubDate()
	set dateStr to ""
	set d to current date
	set monthStr to text -1 thru -2 of ("0" & ((month of d) as integer))
	set dayStr to text -1 thru -2 of ("0" & day of d)
	set yearStr to "" & (year of d)
	return (monthStr & dayStr & text -1 thru -2 of yearStr)
end getPubDate

ExtendScript

//returns current date as MMDDYY
function getPubDate() {
    var d = new Date();
	var mstr = (d.getMonth() + 1).toString();
	var mm = ("00" + mstr).substr(-2);
	var dstr = d.getDate().toString();
	var dd = ("00" + dstr).substr(-2);
	var fullYear = (d.getYear().toString());
	var yy = fullYear.substr(-2);
	var dateStamp = mm+dd+yy;
	return dateStamp;
}

Add Metadata to the Document

Our script will add the author name and copyright information to the document prior to saving. Assuming that the copyright information will be consistent, this information is established at the top of the script.

AppleScript

property metaURL : "yourscriptdoctor.com" --add your copyright info URL
--supply copyright notice such as "Copyright 2013 [Name of Publisher]"
property copyrightNotice : "Copyright 2013 DTP Connection"
(*Adds metadata information to document. If copyrightNotice is not empty string, the copyright status is yes and copyright information added to metadata.*)
on addMetadata(docRef, projectName, pubDate, authorName)
	tell application "Adobe InDesign CS6"
		tell metadata preferences of docRef
			if copyrightNotice is not "" then
				set copyright status to yes
				set copyright notice to copyrightNotice
			end if
			if pubDate is not "" then set job name to projectName & " " & pubDate
			if projectName is not "" then set document title to projectName
			if authorName is not "" then set author to authorName
			if metaURL is not "" then set copyright info URL to metaURL
		end tell
	end tell
end addMetadata

ExtendScript

var metaURL = "yourscriptdoctor.com" //add your copyright info URL
 //supply copyright notice such as "Copyright 2013 [Name of Publisher]"
var copyrightNotice = "Copyright 2013 DTP Connection";
//Adds metadata to document
function addMetadata (docRef, projectName, pubDate, authorName) {
    if (pubDate != "") {
        var metaName = projectName + " " + pubDate;
    }
	var mNotice = "This document is copyrighted";
	var docTitle = projectName;
	metaPrefs = {copyrightNotice:mNotice, copyrightStatus:CopyrightStatus.yes, copyrightInfoURL:metaURL,documentTitle:docTitle};
	metaPrefs.jobName = metaName;
    metaPrefs.author = authorName;
    docRef.metadataPreferences.properties = metaPrefs;
}

Save the Document

For our workflow, we will save the document in the user's Documents folder. If a folder named for the project does not exist, the script will create the folder.

AppleScript

(*Saves document to users document folder in folder named with projectName variable*)
on saveDoc(projectName, docRef, pubDate)
	set userFolder to (path to documents folder from user domain) as string
	set folderPath to userFolder & projectName
	set filePath to folderPath & ":" & projectName & "_" + pubDate + ".indd"
	tell application "Finder"
		if not (exists folder folderPath) then
			make folder at userFolder with properties {name:projectName}
		end if
	end tell
	tell application "Adobe InDesign CS6"
		set label of docRef to projectName
		save docRef to filePath
	end tell
end saveDoc

ExtendScript

/*saves document to users document folder in folder named with projectName variable */
function saveDoc (projectName, docRef, pubDate) {
    var userFolder = (Folder.myDocuments);
    var folderPath = userFolder + "/" + projectName;
    var filePath = folderPath + "/" + projectName + "_" + pubDate + ".indd";
    if (Folder(filePath).exists == false) {
        Folder(folderPath).create();
    }
     docRef.label = projectName;
     docRef.save(new File(filePath));   
}

Now that we have all of the functionality, we can put the script together and test. Watch for the complete script, NewDoc_Print, to be posted to appropriate pages on our web site.

Trackback Link
http://www.yourscriptdoctor.com/BlogRetrieve.aspx?BlogID=15239&PostID=987835&A=Trackback
Trackbacks
Post has no trackbacks.