• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
10

How can I find/GREP objects that have been rotated?

Community Beginner ,
Mar 07, 2024 Mar 07, 2024

Copy link to clipboard

Copied

I want to search a document for objects that have been rotated by 1 to 5 degrees. How can I do this?

TOPICS
Scripting

Views

243

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

Community Expert , Mar 07, 2024 Mar 07, 2024

Hi @gregd, I don't know how to do this from the normal UI, but since you tagged "scripting" here's a script I wrote to collect page items with a range of rotations. You can see in the script where I've set the range -5 to 5 degrees. The page items are collected in the array `myRotatedPageItems`.

 

I think you have done a bit of scripting... are you able to use this? Let me know if you need more help.

- Mark

 

/**
 * Collect Rotated Page Items.js
 * 
 * Example usage of `doSomethingToThings`
 * t
...

Votes

Translate

Translate
Community Expert ,
Mar 07, 2024 Mar 07, 2024

Copy link to clipboard

Copied

Hi @gregd, I don't know how to do this from the normal UI, but since you tagged "scripting" here's a script I wrote to collect page items with a range of rotations. You can see in the script where I've set the range -5 to 5 degrees. The page items are collected in the array `myRotatedPageItems`.

 

I think you have done a bit of scripting... are you able to use this? Let me know if you need more help.

- Mark

 

/**
 * Collect Rotated Page Items.js
 * 
 * Example usage of `doSomethingToThings`
 * that collects page items having a
 * range of rotation.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-can-i-find-grep-objects-that-have-been-rotated/m-p/14474764
 */
function main() {

    // your items will be collected here
    var myRotatedPageItems = [],

        // adjust these to suit
        minRotationDegrees = -5,
        maxRotationDegrees = 5;

    // search every page item
    doSomethingToThings({
        target: app.activeDocument,
        thingPlural: 'pageItems',

        // collect rotated items
        doToThing: function collectRotatedItems(thing, doc) {

            if (
                undefined == thing
                || !thing.isValid
                || !thing.hasOwnProperty('rotationAngle')
                || 0 === thing.rotationAngle
            )
                return;

            if (
                thing.rotationAngle >= minRotationDegrees
                && thing.rotationAngle <= maxRotationDegrees
            )
                myRotatedPageItems.push(thing);

        },

    });

    alert('Collected ' + myRotatedPageItems.length + ' rotated page items.');

    // now you need to decide what to do with the rotation items!
    // -- your code here --

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Collect rotated items');


/**
 * Executes a function for every thing
 * in the document or documents.
 * `thingPlural` is a Document property name,
 * for example 'formFields' or 'swatches'.
 * @author m1b
 * @version 2024-02-29
 * @param {Object} options
 * @param {String} options.thingPlural - collective property of Document eg. 'checkBoxes'.
 * @param {Document|Documents} [options.target] - an Indesign Document or Documents (default: active document).
 * @param {Function} [options.doToDocument] - a function to be executed for each document (default: do nothing).
 * @param {Function} [options.doToThing] - a function to be executed for each thing (default: do nothing).
 */
function doSomethingToThings(options) {

    options = options || {};

    var doc = options.target || app.activeDocument,
        thingPlural = options.thingPlural,
        doToDocument = options.doToDocument,
        doToThing = options.doToThing;

    if ('Documents' === doc.constructor.name) {

        for (var i = 0; i < doc.length; i++) {
            options.target = doc[i];
            doSomethingToThings(options);
        }

        return;

    }

    // do something to document
    if (undefined != doToDocument)
        if (!doToDocument(doc))
            return;

    var things = getThingsFromDocument(doc, thingPlural) || [];

    // do something to things
    for (var i = things.length - 1; i >= 0; i--)
        if (undefined != doToThing)
            doToThing(things[i], doc);

};


/**
 * Returns array of things found in `doc`.
 * `thingPlural` is a Document property name,
 * for example 'formFields' or 'swatches'.
 * @author m1b
 * @version 2024-02-29
 * @param {Document} doc - an Indesign Document.
 * @param {String} thingPlural - collective property of Document eg. 'checkBoxes'.
 * @returns {?Array<*>} - the things.
 */
function getThingsFromDocument(doc, thingPlural) {

    if (
        !doc.hasOwnProperty(thingPlural)
        || 'function' !== typeof doc[thingPlural].everyItem
    )
        return;

    var things = doc[thingPlural].everyItem().getElements();

    if (doc.groups.length > 0)
        // also collect things in groups
        things = things.concat(doc.groups.everyItem()[thingPlural].everyItem().getElements());

    if (doc.stories.length > 0) {

        // also collect any anchored things
        things = things.concat(doc.stories.everyItem()[thingPlural].everyItem().getElements());

        if (doc.stories.everyItem().tables.length > 0)
            // also collect any thing in tables
            things = things.concat(doc.stories.everyItem().tables.everyItem().cells.everyItem()[thingPlural].everyItem().getElements());
    }

    return things;

};


/**
 * Returns a thing with matching property.
 * @param {Array|collection} things - the things to look through, eg. PageItems.
 * @param {String} key - the property name, eg. 'name'.
 * @param {*} value - the value to match.
 * @returns {*} - the thing.
 */
function getThing(things, key, value) {

    for (var i = 0; i < things.length; i++)
        if (things[i][key] == value)
            return things[i];

};

 

Votes

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
Community Beginner ,
Mar 07, 2024 Mar 07, 2024

Copy link to clipboard

Copied

Thanks, this is very helpful! I just want to identify them to deal with each individually later, so I added this:

 

	var pageNumbers = '';

	for (var i = 0; i < myRotatedPageItems.length; i++) {
		// Apply red stroke
		myRotatedPageItems[i].strokeColor = app.activeDocument.colors.itemByName("C=15 M=100 Y=100 K=0");
		myRotatedPageItems[i].strokeWeight = 1;

		// Get page number and append it to the string
		pageNumbers += "Page " + myRotatedPageItems[i].parentPage.name + "\n";
	}

	alert(pageNumbers);

 

Votes

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
Community Expert ,
Mar 07, 2024 Mar 07, 2024

Copy link to clipboard

Copied

Very nice! 😊 

Votes

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
Community Expert ,
Mar 08, 2024 Mar 08, 2024

Copy link to clipboard

Copied

@gregd

 

If I may - your code will work much faster - on bigger documents - if you declare a new variable for your color outside of the loop and then use it in the loop instead of referencing to the color in the document for every item:

 

 

var myColor = app.activeDocument.colors.itemByName("C=15 M=100 Y=100 K=0");

for (var i = 0; i < myRotatedPageItems.length; i++) {
		// Apply red stroke
		myRotatedPageItems[i].strokeColor = myColor;

 

 

Votes

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
Community Expert ,
Mar 08, 2024 Mar 08, 2024

Copy link to clipboard

Copied

@gregd Robert makes a great point. Also have a look at the version below (just the main function). It doesn't greatly matter which way, but if you see what I did differently it will give you food for thought I hope. This is actually how I intended the `doSomethingToThings` function should be used.

- Mark

function main() {

    // your vars
    var minRotationDegrees = -5,
        maxRotationDegrees = 5,
        red = app.activeDocument.colors.itemByName("C=15 M=100 Y=100 K=0"),
        pageNumbers = '';


    // search every page item
    doSomethingToThings({
        target: app.activeDocument,
        thingPlural: 'pageItems',

        // collect rotated items
        doToThing: function collectRotatedItems(thing, doc) {

            if (
                undefined == thing
                || !thing.isValid
                || !thing.hasOwnProperty('rotationAngle')
                || 0 === thing.rotationAngle
                || thing.rotationAngle < minRotationDegrees
                || thing.rotationAngle > maxRotationDegrees
            )
                return;

            // Apply red stroke
            thing.strokeColor = red;
            thing.strokeWeight = 1;

            // Get page number and append it to the string
            pageNumbers += "Page " + thing.parentPage.name + "\n";

        },

    });

    alert(pageNumbers);

};

Votes

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
Community Expert ,
Mar 08, 2024 Mar 08, 2024

Copy link to clipboard

Copied

@m1b

 

I wouldn't comment - if it wouldn't affect the OP - your new code compares <> instead of <= >=.

 

Votes

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
Community Expert ,
Mar 08, 2024 Mar 08, 2024

Copy link to clipboard

Copied

It is because this time I am excluding not including.

- Mark

Votes

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
Community Expert ,
Mar 08, 2024 Mar 08, 2024

Copy link to clipboard

Copied

quote

It is because this time I am excluding not including.

- Mark


By @m1b

 

I know what is the difference - but why?

 

Votes

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
Community Expert ,
Mar 08, 2024 Mar 08, 2024

Copy link to clipboard

Copied

I just combined all the exclusions. It's a little neater I guess. No big deal; same result. Did you prefer the other way?

- Mark

Votes

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
Community Expert ,
Mar 08, 2024 Mar 08, 2024

Copy link to clipboard

Copied

quote

I just combined all the exclusions. It's a little neater I guess. No big deal; same result. Did you prefer the other way?

- Mark


By @m1b

 

??

 

I'm not talking about how you check - but what you check.

 

OP want's "1 to 5" so "including" - like in the 1st version of your code - so it's a typo in the 2nd version??

 

Votes

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
Community Expert ,
Mar 08, 2024 Mar 08, 2024

Copy link to clipboard

Copied

It isn't a typo. If the rotation amount is less than min or greater than max, then don't paint it red.

Votes

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
Community Expert ,
Mar 08, 2024 Mar 08, 2024

Copy link to clipboard

Copied

LATEST

@m1b 

 

You are right, sorry. Need to change my phone asap.

 

Votes

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
Community Expert ,
Mar 07, 2024 Mar 07, 2024

Copy link to clipboard

Copied

@gregd

 

If you work on a PC - you could use free version of my tool.

 

After you get list of objects - all from the active document, from all open documents or just selected - you can sort & filter any way you want. 

 

Then, you can do whatever you want with those objects. 

 

Votes

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