Skip to main content
Participating Frequently
April 2, 2024
Answered

Change all anchored objects to an object style

  • April 2, 2024
  • 1 reply
  • 481 views

Dear all!

I have a book document with multiple anchored images in the text.

I want to release all these anchored images and apply the object style ‘Crocodile’ to all of them.

At the moment they don't have an object style.

 

I found a lovely script that released all images, but that's only half of my problem, I cannot find any script what releases them and then gives them an object style.

 

Is there anyone there who knows a solution?

Thank you already!

Caroline

 

This topic has been closed for replies.
Correct answer Robert at ID-Tasker

 

/**
* Release All Anchored Objects.
* @discussion https://community.adobe.com/t5/indesign-discussions/change-all-anchored-objects-to-an-object-style/m-p/14529940#M568608
*/
function main() 
{
	var doc = app.activeDocument,
	items = doc.allPageItems,
	counter = 0,
	oversetCounter = 0;
	
	var myObjStyle = "ENTER_THE_NAME_OF_THE_OBJECT_STYLE";
	
	for (var i = items.length - 1; i >= 0; i--) 
	{
		if (items[i].parent.constructor.name == 'Character'
		&& items[i].hasOwnProperty('anchoredObjectSettings')) 
		{
			if (items[i].parent.parentTextFrames.length > 0) 
			{
				items[i].anchoredObjectSettings.anchoredPosition = AnchorPosition.ANCHORED;
				items[i].anchoredObjectSettings.releaseAnchoredObject();
				items[i].appliedObjectStyle = doc.objectStyles.itemByName(myObjStyle);
				counter++;
			}
			else 
			{
				oversetCounter++;
			}
		}
	};
	alert('Released ' + counter + ' anchored objects.' + (oversetCounter > 0 ? ' Skipped ' + oversetCounter + ' overset objects.' : ''));

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Release All Anchored Objects');

 

 

 

1 reply

Robert at ID-Tasker
Legend
April 2, 2024
quote

[...]

I found a lovely script that released all images, but that's only half of my problem, I cannot find any script what releases them and then gives them an object style.

[...]


By @Caro31217572wgdd

 

Can you post what you've found?

 

The change would be minimal - just one extra line.

 

Participating Frequently
April 2, 2024

Sure!

This is the script i found:quote


/**
* Release All Anchored Objects.
* @discussion https://community.adobe.com/t5/indesign-discussions/release-all-anchored-objects-at-once/m-p/13797933#M527123
*/
function main() {

var doc = app.activeDocument,
items = doc.allPageItems,
counter = 0,
oversetCounter = 0;

for (var i = items.length - 1; i >= 0; i--) {

if (
items[i].parent.constructor.name == 'Character'
&& items[i].hasOwnProperty('anchoredObjectSettings')
) {
if (items[i].parent.parentTextFrames.length > 0) {
items[i].anchoredObjectSettings.anchoredPosition = AnchorPosition.ANCHORED;
items[i].anchoredObjectSettings.releaseAnchoredObject();
counter++;
}

else {
oversetCounter++
}
}

};

alert('Released ' + counter + ' anchored objects.' + (oversetCounter > 0 ? ' Skipped ' + oversetCounter + ' overset objects.' : ''));

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Release All Anchored Objects');

Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Legend
April 2, 2024

 

/**
* Release All Anchored Objects.
* @discussion https://community.adobe.com/t5/indesign-discussions/change-all-anchored-objects-to-an-object-style/m-p/14529940#M568608
*/
function main() 
{
	var doc = app.activeDocument,
	items = doc.allPageItems,
	counter = 0,
	oversetCounter = 0;
	
	var myObjStyle = "ENTER_THE_NAME_OF_THE_OBJECT_STYLE";
	
	for (var i = items.length - 1; i >= 0; i--) 
	{
		if (items[i].parent.constructor.name == 'Character'
		&& items[i].hasOwnProperty('anchoredObjectSettings')) 
		{
			if (items[i].parent.parentTextFrames.length > 0) 
			{
				items[i].anchoredObjectSettings.anchoredPosition = AnchorPosition.ANCHORED;
				items[i].anchoredObjectSettings.releaseAnchoredObject();
				items[i].appliedObjectStyle = doc.objectStyles.itemByName(myObjStyle);
				counter++;
			}
			else 
			{
				oversetCounter++;
			}
		}
	};
	alert('Released ' + counter + ' anchored objects.' + (oversetCounter > 0 ? ' Skipped ' + oversetCounter + ' overset objects.' : ''));

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Release All Anchored Objects');