[InDesign CS4] - Retrieving an object by its Style Name...

Hi All!
I’ve been googling around enough, I think, to just give up and ask my trivial question:
How can I possibly select/get/retrieve/(…) an InDesign “Object” (be it a Text Frame, or anything else) by its Style Name?

I have a 300 pages document with a Text frame for the Title to which I assigned an Object Style named… “TITLE”.
Now I’m asked to move each one “a little bit up”.

Don’t know if it makes any difference, but the “TITLE” Text Frames are placed on a Layer named “TEXT” and the document is on “spreads” mode.

This is what I come up with:


tell application "Adobe InDesign CS4"
	set myDocument to active document
	tell myDocument
		set myLayer to layer "TEXT"
                set myObject to item of myLayer whose style is "TITLE"
               --OR: set myObject to item of myLayer whose applied object style = "TITLE"
               --OR:.....???!
		move myObject by [0, -30]
	end tell
end tell

But it throws an error like: "Adobe Indesign CS4 has found an error: impossible to obtain item of layer id 1605 of document “(Name_of_the_document)” whose style = “TITLE”.

What am I supposed to write up there?

TIA

_

I don’t do any scripting in ID, but it seems to me that there may be something wrong in the reference form.

set myLayer to layer “TEXT”
set myObject to item of myLayer whose style is “TITLE”.

What are the properties of a layer and a style?
set myLayer to layer name “TEXT”
set myObject to item of myLayer whose style name is “TITLE”.
Or something like that.
I’m just guessing, but you may want to look there.

Thanks for your reply, mdudek!
I don’t know… Isn’t the fact that the error itself reports the ID of the Layer kind of a “proof” that the reference is correct?
(I mean, seriously, “I don’t know”).

Btw…
Now the script looks like this:


tell application "Adobe InDesign CS4"
    set myDocument to active document
    tell myDocument
     set myLayer to layer "TEXT"
     set myObject to object style "TITLE"
     move myObject by [0, -30]
    end tell
end tell

And it doesn’t throw anymore the error I mentioned in the first post, until it reaches the “move” line, at which point it does throw an(other) error which sounds like "Adobe Indesign CS4 has found an error: missing parameter ‘to’ for the event ‘move’. "
(I tried replacing “by” with “to”, but it still misses this ‘to’ parameter. I have no idea of what it’s asking for!).

But at the same time adding a “display dialog(myObject)” throws an error saying it can’t convert the ID of the object (“object style id 87327”) into a string type.

So, again, if retreiving the ID of an object can be considered a consequence of a well referenced syntax, I can’t figure where’s the problem.

I think I need more hints :wink:

TNX
_

Try something like this:

tell application "Adobe InDesign CS4"
	tell document 1
		set myStyle to object style "Title"
		set myLayer to layer "Text"
		set myObjects to every text frame whose applied object style is myStyle and item layer is myLayer
		--returns a list of text frames that meet the requirements
		repeat with anObject in myObjects
			move anObject by {1, 10}
		end repeat
	end tell
end tell

I set variables to the object reference for the style and layer because ID did not want to accept using the style name in the set myObjects line. This should build a list of every text frame whose object style is “Title” and is on layer “Text,” and ignore any text frames with the object style is “Title” but are on different layers.

In your script you set myObject to object style “TITLE” but you cannot move an object style. The “to” portion of the move command is to designate a new spread, page, or layer.

tell application "Adobe InDesign CS4"
	tell document 1
		move text frame 2 to layer 1
	end tell
end tell

myObject is an object reference and you cannot coerce this to a string. You could display the label of the object, ID, or index.

tell application "Adobe InDesign CS4"
	tell document 1
		display dialog (id of text frame 1 as string)
	end tell
end tell

WOW!
The solution AND the explanation!
I’m really touched by your kindness, Jerome; thank you very, very much :slight_smile:

ciÃ
ale

Okay…
We’re not done, yet :confused:

Looks like I have a bunch of those “objects” locked.
I’ve seen in ID’s dictionary it’s all a matter of “allow(ing) overrides” but I don’t have any clue on how and where to use 'em in Jerome’s script.

I mean; I need to override only the object styles which are already defined in the “myObjects” list.
(Only the ones I’m moving, to make it shorter).

tell application "Adobe InDesign CS4"
   tell document 1
       set myStyle to object style "Title"
       set myLayer to layer "Text"
       set myObjects to every text frame whose applied object style is myStyle and item layer is myLayer
       repeat with anObject in myObjects
           move anObject by {1, 10}
       end repeat
   end tell
end tell

Where (and how) am I supposed to place the override instruction?

Thanks again for your patience…

ale

There are two ways that an item might be “locked.” One is if it is on a master page the other is if the position of the object is locked.

This script should unlock the position of the text frames that meet the test and move them if they are on a standard page or a master page.

tell application "Adobe InDesign CS4"
	tell document 1
		set myStyle to object style "Title"
		set myLayer to layer "Text"
		set myObjects to every text frame whose applied object style is myStyle and item layer is myLayer
		repeat with anObject in myObjects
			set x to name of parent of anObject
			set locked of anObject to false
			move anObject by {1, 10}
		end repeat
	end tell
end tell

If you do not want to move them on the master page, but want to override the master page item for a specific page and move the item then additional work needs to be done to check if it is a master page item, override the master page item on the page in question and then move the item.

In this case, both ways matched.
The item was locked on the master page, hence locked on every single page (when it wasn’t unlocked manually on some pages where/when needed).

I worked this out by simply unlocking (cmd-alt-L) the object on the master page (manually).
Not an elegant solution, I know, but I was running out of time :wink:

Man… How dumb can AS make me feel!
“Set locked of anObject to false”… Couldn’t be more simple than that and still I could have never guessed it! :confused:

Btw… what’s the string

set x to name of parent of anObject

doing there?
I can’t see any occurrence of the variable “x” after its declaration…
Is there any part of the code missing, or it’s just part of something you were trying and forgot to delete? (Or am I just gone blind)?
In any case, what would the value of “x” be? (If I had to guess I’d say “Text”, 'cause the nearest parent of each “myObject” should be its layer; but I’m not sure at all).

Well… This was not the case, but I’d really like to know how do that, if you don’t mind and have some spare time…
I’m pretty sure that’s a situation I’ll have to face, sooner or later.

Thank you once again, Jerome! :slight_smile:

ciÃ
ale

Hey Alphabreeze:

I know I’m a day late and a dollar short on this one, but wasn’t the header a frame on your master page and couldn’t you have just changed the position of the frame manually on the master page to have that reflected on all document pages to which that master page is applied?

Hi, harges!
You’re right. At least… in theory.
That’s the first thing I’ve tried but it worked only for a couple of pages (still have to investigate why! Thanks for remembering me :wink:
Gotta dig pretty deep inside the logic (?) of masterpages in InDesign. It looks like if you unlock (cmd-alt-L) and modify certain items in the document they miss their ‘linkage’ with their masterpage.
Maybe I had to unlock and modify them directly on the masterpage and re-lock… ? …

Btw… The fact is that I had all my items disconnected from their masterpages, hence the need for a script.
Thank you for your hint and interest :wink:

ciÃ
ale

The master page update thing is still a bit fuzzy to me as well. As I understand it, any time you override an attribute of an item on a document page that was placed there as a master page item, you lose the update ability for that attribute.

So, if you override the master and move the header on page 2 and on no other page, then a move to the header on the master will update every header except the one on page 2. Note that you can still update that page 2 header from the master for other attributes such as color, etc. Just not position, because you overrode that on the doc page.

Buon Natale,

The line you questioned was put in for testing a property and not used, I just forgot to take it out before posting. I had part of the master page overrides worked out but didn’t save the file. After playing around with it a little here is a script that will override a specific master page item on every page (if it exists, the try statement traps any errors if there is no item or if the item has been overridden already). It will then get the locked state of the object, unlock it and move it toward the gutter by 4 (of the current document measurement settings), and finally reset the locked state of the item to where it was before the move.

tell application "Adobe InDesign CS4"
	activate
	tell document 1
		set myStyle to object style "Title"
		set myLayer to layer "Text"
		repeat with i from 1 to count of pages
			try
				if side of page i is left hand then
					set masterObjects to (every text frame of page 1 of applied master of page i whose applied object style is myStyle and item layer is myLayer)
					repeat with anObject in masterObjects
						set myObject to override anObject destination page page i
						set lockedState to locked of myObject
						set locked of myObject to false
						move myObject by {4, 0}
						set locked of myObject to lockedState
					end repeat
				else
					set masterObjects to (every text frame of page 2 of applied master of page i whose applied object style is myStyle and item layer is myLayer)
					repeat with anObject in masterObjects
						set myObject to override anObject destination page page i
						set lockedState to locked of myObject
						set locked of myObject to false
						move myObject by {-4, 0}
						set locked of myObject to lockedState
					end repeat
				end if
			on error
				--no master items found
			end try
		end repeat
	end tell
end tell

A slightly different option would be to move only those items that fit the test but are not master page items or have been overridden from the master page. You can do this by getting the class of the spread of the item. The parent of the text frame is the page, the parent of the page is the spread so the class of the parent of the parent of the item is either master spread or spread.

tell application "Adobe InDesign CS4"
	tell document 1
		set myStyle to object style "Title"
		set myLayer to layer "Text"
		set x to properties of spread 1
		set myObjects to every text frame whose applied object style is myStyle and item layer is myLayer
		repeat with anObject in myObjects
			if class of parent of parent of anObject is not master spread then
				set locked of anObject to false
				move anObject by {1, 10}
			end if
		end repeat
	end tell
end tell

Buon Natale anche a te! (… “to you too” :wink:

BIG Thanks for your patience, Happy Holidays and… here I go studying your scripts! :slight_smile:

ciÃ
ale

Buongiorno Ale,

I still remember a bit of Italian from when I worked for NATO in Naples. I was never really good at speaking it but loved the country and the people, not to mention the food. Since coming back I am rarely satisfied with the bread I can get here in the States, it’s odd that such a simple thing can be so different from one country to the next. Then again when I was there I liked American deep dish Pizza better than the Neapolitan pizza, they didn’t put enough “stuff” on it for my American taste.

Let me know if you have any questions on what is going on with the code.

Ciao,
Jerome