Working With Word Import

In our previous blog post we used a script to produce a list of the embedded images imported as part of a Microsoft Word document. This would be given to the person responsible for resampling and resizing the images along with the images that have been unembedded from the document.

It is a simple matter to unembed images from an InDesign document. Select the items from within the Links panel, right-click on the selection, and select Unembed Link from the menu presented..

The user is then presented a dialog giving the option of placing the files in a folder to be selected (or created new). Select NO to have the images placed in a designated folder.

The report created by the script is then used by the person responsible for resizing/resampling the images. (A copy of the original images should be retained for safe keeping).

Once the images are processed, they will be relinked to the InDesign document when it is reopened. (That is, of course, if the processed images files are retained in the folder chosen.)

On occasion the designer may want the freedom to move the images around to conform to a more artistic page design. For this you can use a script to release the image anchors. Additionally, as part of the process, an object style can be used to give the image container a text wrap.

The following scripts can do the job.

Unanchor Spline Items – JavaScript

try {
var docRef = getDocRef();
    var objStyleRef = getObjStyle (docRef, styleName, wrapAmt);
    var sList = docRef.stories;
     for (var j = 0; j < sList.length;j++) {
      var storyRef = docRef.stories[j];
      var sItems = storyRef.splineItems;
      if (sItems.length > 0) {
         for (var i = sItems.length - 1; i >= 0; i--) {
             cnt ++;
             myItem = sItems[i];
             // releaseMe(myItem);
             myItem.anchoredObjectSettings.anchoredPosition = AnchorPosition.anchored;  
             myItem.anchoredObjectSettings.releaseAnchoredObject();  
              //apply object style
              myItem.appliedObjectStyle = objStyleRef;
          }  //ends for i loop
     } //ends if
    }// ends for j loop
      alert ("Anchored Items released: " + cnt);
} catch (e) {
    alert ("Error: " + e);
}
//returns reference to active document; otherwise throws error
function getDocRef() {
 if (app.documents.count() > 0 ){
    return app.activeDocument;  
 } else {
 throw ("Requires active document");
 }
}
//returns reference to object style; if it doesn't exist, the style is created
function getObjStyle (docRef, styleName, wrapAmt) {
    var test = docRef.objectStyles.itemByName(styleName).isValid;
   if (test){
        var objStyleRef =  docRef.objectStyles.itemByName(styleName);
   } else {
        var basedRef = docRef.objectStyles[0];
        //var wrapPref = TextWrapPreferences = {textWrapMode:TextWrapModes.BOUNDING_BOX_TEXT_WRAP, textWrapOffset:wrapAmt};
         var objStyleRef = docRef.objectStyles.add({name:styleName, basedOn:basedRef});
             objStyleRef.enableFill = true;
             objStyleRef.enableStroke = true;
             objStyleRef.enableTextWrapAndOthers= true;
         objStyleRef.fillColor = "None";
         objStyleRef.strokeWeight = 0;
         objStyleRef.textWrapPreferences.textWrapMode = TextWrapModes.BOUNDING_BOX_TEXT_WRAP;
         objStyleRef.textWrapPreferences.textWrapOffset = wrapAmt;
     }
 return objStyleRef;
}

Unanchor Spline Items – AppleScript

set styleName to "Image"
set wrapAmt to {"1p0", "1p0", "0p6", "1P0"}
set testList to {}
tell application "Adobe InDesign CC 2017"
   try
	set docRef to my getDocRef()
	set cnt to 0
	set objStyleRef to my getObjStyle(docRef, styleName, wrapAmt)
	tell docRef
	   set sList to stories
	end tell
	repeat with i from 1 to length of sList
	   set storyRef to item i of sList
	   set sItems to spline items of storyRef
		if (length of sItems is greater than 0) then
		   repeat with i from (length of sItems) to 1 by -1
		   try
			set cnt to cnt + 1
			set myItem to item i of sItems
			tell anchored object settings of myItem
			   set anchored position to anchored
			   release anchored object
			end tell
			set applied object style of myItem to objStyleRef
		   end try
		end repeat
	   end if
      end repeat
      activate
	display alert ("Anchored items released " & cnt)
      on error errStr
	 activate
	  display alert ("Error " & errStr)
      end try
end tell
(*returns reference to active document; otherwise throws error*)
on getDocRef()
   tell application "Adobe InDesign CC 2017"
	if (count of documents) > 0 then
	   set docRef to active document
	else
	   activate
	   error ("Requires active document")
	end if
   end tell
end getDocRef
(*returns reference to object style; if it doesn't exist, the style is created*)
on getObjStyle(docRef, styleName, wrapAmt)
   tell application "Adobe InDesign CC 2017"
	tell docRef
	   if (exists object style styleName) then
		set objStyleRef to object style styleName
	   else
		set basedRef to object style 1 of docRef
		set objStyleRef to make object style with properties {name:styleName, based on:basedRef}
		tell objStyleRef
		   set enable fill to true
		   set enable stroke to true
		   set enable text wrap and others to true
		   set fill color to "None"
		  set stroke weight to 0
		  tell text wrap preferences
		     set text wrap mode to bounding box text wrap
		     set text wrap offset to wrapAmt
		  end tell
	       end tell
	   end if
      end tell
   end tell
   return objStyleRef
end getObjStyle

…Dialog reports success with number of items released

ONWARD AND UPWARD

To make these scripts even more user friendly, add a custom dialog for the user to choose whether to use an existing object style or create a new one with the amount of text wrap to be applied.

Even if you never have the occasion to need to release anchored objects from a document, having the code for creating an object style may prove helpful for other scripts.

Disclaimer: Code samples are to be used as a guide for users to create their own real world scripts and is provided “AS IS”. No representation is made as to the accuracy or completeness of the code. Use of the code and its operation is at the discretion and responsibility of the user.