Move text to a layer

The challenge: Need batch ID docs. & get all instances where the paragraph style “Codes” was applied, create a new layer “PR” & move text frames containing that paragraph style to a layer “PR”. This what I have so far. Any help is appreciated.

set source_folder to choose folder with prompt "Select folder containing Indesign Documents to move codes to a diffrent layer"
tell application "Finder" to set theFiles to files of source_folder whose name extension is "indd"

if (count of theFiles) is 0 then
	display dialog "No InDesign files to process" buttons "Cancel" default button "Cancel" with icon 0 giving up after 6
	return
end if
end

--loop the selected folder
repeat with oneFile in theFiles
	
	tell application "Adobe InDesign CS4"
		--ignore dialogs
		set user interaction level of script preferences to never interact
		activate
		
		set myDocument to open (oneFile as alias)
		tell myDocument
			
			--find pstyle
			set myPstyle to paragraph style "CODES"
			-- if the page is a spread
			set x to properties of spread 1
			
			set LayerName to "PR"
			try
				set myLayer to layer LayerName
				set the properties of myLayer to {locked:false}
			on error
				--create layer if does not exist
				set myLayer to make layer with properties {name:LayerName, layer color:red, visible:true}
			end try
			
			--get all instances where paragraph style CODES was applied only & move text frames to a PR layer
			set myObjects to (every text frame whose applied paragraph style is myPstyle and item layer is myLayer)
			move (every text frame whose applied paragraph style is myPstyle) to layer "PR"
			
			
			close myDocument saving yes
			display alert "Opening next document..." giving up after 2
			
			--put the prefs the way it was
			tell application "Adobe InDesign CS4"
				set user interaction level of script preferences to interact with all
			end tell
		
			--==============	
		end tell --myDoc
	end tell --Indesign
end repeat

Model: iMac intel
AppleScript: 2.3
Browser: Safari 537.11
Operating System: Mac OS X (10.6)

I’m getting this error:
error “Adobe InDesign CS4 got an error: Can’t get every text frame of document "15_007.indd" whose applied paragraph style = paragraph style id 23543 of document "15_007.indd" of application "Adobe InDesign CS4".” number -1728 from every text frame of document “15_007.indd” whose applied paragraph style = paragraph style id 23543 of document “15_007.indd”

This is my only source for help with scripts.

Hi. Text frames don’t have a paragraph style property. Each frame can contain text with varied styles. Test the paragraphs.

Thanks Marc. I was thinking to use find grep to get the paragraph style but that route will find the content on the text frame. that’s the thing I don’t know how do get those specific text frames. Sorry for my ignorance but What do you mean by:

Model: iMac intel
AppleScript: 2.3
Browser: Safari 537.11
Operating System: Mac OS X (10.6)

hi
try accessing the paragraph

set myObjects to (every paragraph of every story whose (applied paragraph style is myPstyle))

Hello, again. I was out of pocket for a couple days, but Budgie explained the gist. You compare the individual paragraphs against your style, and then you can relocate the positive results.

tell application "Adobe InDesign CS3"'s document 1
	repeat with aPara in (stories's paragraphs)'s object reference
		if aPara's applied paragraph style's name is {"CODES"} then move (aPara's parent's text containers) to layer "PR"
	end repeat
end tell

:smiley:
Exactly what I needed. I’m trying to understand how is this working. I just want to learn.

every paragraph of every story – why do I need every story?
object reference – the dictionary says: An object reference for the object. how do I figure it out that was needed
parent’s text containers-- I see in the dictionary parent text frames but not parent text containers.

A brief explanation will help me to understand. No way I would be able to do this without you. Thank you guys for your help: Budgie & Marc.

You don’t really need to target the story; I could’ve just as easily used text frames, however, text ultimately descends from stories, so targeting those ensures you get even deeply nested items. Using the frame method, the brackets around “CODES” must be eliminated, where they are mandatory for the story method.

The object reference bit is needed because stories’ paragraphs would otherwise literally be the text, rather than an address. Parent text frames is a single property in the dictionary, while parent’s text containers is two; the parent (of aPara) is its story, while text containers are the text frames it inhabits, which is useful if it straddles frames.

Wonderful. Thank you for your help