[InDesign CS2] Applying specific XML content to tagged frames

Howdy–

I have a script created that does the following

  1. Creates a document
  2. Creates styles that match a DTD
  3. Import the DTD
  4. Map the tags to styles
  5. Import an XML document
  6. create a text frame

but what i absolutly cannot figure out is how to get the XML content into the text frame. I need to do it two ways because of two different template types I’m creating.

  1. Set up tagged frames and have the XML content fill those frames accordingly
  2. Set up a general text frame and just have all the XML content flow in.

I think I’ve been to every page on the Adobe website and read every example, but these are the two things that I’m not able to find anywhere. It’s mind-boggling. I’ve included a shortened version of the script below my sig. i can do all of this by hand, but i cannot find any of the Applescript commands to do it all automatically.

Any help anyone can give would be greaty appreciated. Thanks!
Philip
lists (insert “at” symbol here) oatmeal and coffee dot com


tell application "Adobe InDesign CS2"
	--preset check
	set presetCheck to (get name of every document preset)
	if "MKTG_AUTO_Generic" is not in presetCheck then
		set GenericDocPreset to make new document preset with properties {bottom:36, document bleed bottom offset:0, document bleed outside or right offset:0, document bleed inside or left offset:0, document bleed top offset:0, facing pages:false, left:36, name:"MKTG_AUTO_Generic", page height:792, page orientation:portrait, page width:612, right:36, top:36}
	end if
	set GenericDoc to make new document with properties {document preset:"MKTG_AUTO_Generic"}
	
	tell GenericDoc
		--creates all the character and paragraph styles
		make new paragraph style with properties {name:"story", applied font:"Times", font style:"Roman", point size:18, space after:6} --1
		
		make new paragraph style with properties {name:"book", applied font:"Times", font style:"Roman", point size:18, space after:6} --11
		
		make new paragraph style with properties {name:"book_title", applied font:"Times", font style:"Roman", point size:18, space after:6} --14
		
		make new character style with properties {applied font:"Times", name:"book_title_article", font style:"Roman", size:12.0} --15
		make new character style with properties {applied font:"Times", name:"book_title_title", font style:"Roman", size:12.0} --16
		make new character style with properties {applied font:"Times", name:"book_title_subtitle", font style:"Roman", size:12.0} --17
		make new character style with properties {applied font:"Times", name:"book_title_edition", font style:"Roman", size:12.0} --18
		make new character style with properties {applied font:"Times", name:"book_title_version", font style:"Roman", size:12.0} --19
		
		make new paragraph style with properties {name:"book_biblio", applied font:"Times", font style:"Roman", point size:18, space after:6} --35
		
		make new character style with properties {applied font:"Times", name:"book_isbn_code", font style:"Roman", size:12.0} --39
		make new character style with properties {applied font:"Times", name:"book_isbn_10", font style:"Roman", size:12.0} --40
		make new character style with properties {applied font:"Times", name:"book_isbn_13", font style:"Roman", size:12.0} --41
		
		--technical prep
		set ruler origin of view preferences of GenericDoc to page origin --very important for ease of placing elements according to Adobe
		
		--sets color variables
		set noColor to swatch "None" of GenericDoc --document 1
		set blackColor to swatch "Black" of GenericDoc --document 1
		set blueColor to swatch "C=100 M=0 Y=0 K=0" of GenericDoc --document 1
		set whiteColor to swatch "Paper" of GenericDoc --document 1		
		
		--import the DTD and map accordingly
		set theDTDDoc to "Macintosh HD:Users:Shared:AutoMarketing_0_1:System:XML:MKTG_DTD_Parsing_1_4_b_SLSA_0_4.dtd"
		import dtd from theDTDDoc
		
		set PStyleCount to (get count of paragraph styles)
		repeat with p from 1 to PStyleCount
			set thisPStyle to paragraph style p
			set thisPStyleObj to object reference of thisPStyle
			set thisPStyleName to name of thisPStyleObj as string
			if thisPStyleName is not "[Basic Paragraph]" and thisPStyleName is not "[No Paragraph Style]" then --Avoids this error: The tag you are trying to create has an invalid name.
				set XMLImportMap to make XML import map with properties {mapped style:thisPStyle, markup tag:thisPStyleName}
			end if
		end repeat
		
		set CStyleCount to (get count of character styles)
		repeat with p from 1 to CStyleCount
			set thisCStyle to character style p
			set thisCStyleObj to object reference of thisCStyle
			set thisCStyleName to name of thisCStyleObj as string
			if thisCStyleName is not "[None]" and thisCStyleName is not "[No Character Style]" then --Avoids this error: The tag you are trying to create has an invalid name.
				set XMLImportMap to make XML import map with properties {mapped style:thisCStyle, markup tag:thisCStyleName}
			end if
		end repeat
		
		--import the XML
		set theXMLDoc to "Macintosh HD:Users:Shared:AutoMarketing_0_1:System:XMLQueue:MKTG_DTD_Parsing_1_4_b_SLSA_0_4.xml"
		import XML from theXMLDoc
		
		--create the page elements
		set TitleFrame to make new text frame with properties {geometric bounds:{54, 54, 154, 558}}
		
		--flow in the XML into the appropriate text elements 
		set contents of TitleFrame to contents of XML element "book_title"
		(* THIS IS WHERE I'M STUMPED *)
		--STOP ERROR: Invalid value for set property 'contents'. Expected string, placeholder text or auto page number/...
		
		
	end tell --document
end tell --application

you’ve come to the right place… Frankly - the Adobe forums are horrible… anyway - here’s what I do.

adjust to suit your scripot as nessesary:

tell application "Adobe InDesign CS2"
	tell document 1
		tell XML import preferences
			set import style to merge import
			set ignore whitespace to true
			set ignore unmatched incoming to true
			set import text into tables to true
			set create link to XML to true
			set import to selected to true
			set remove unmatched existing to false
			set repeat text elements to true
		end tell
		try
			set mytextframe to object reference of selection
		on error
			display dialog "Please select a text frame"
			return
		end try
		set myXMLFile to choose file
		
		select associated XML element of mytextframe
		
		tell selection
			import XML from myXMLFile
		end tell
	end tell
end tell

Phillip - on a related note - are you sucessful in getting the tags to map to the styles? - I can’t seem to get that to work in Applescript- it runs ok, but it doesn’t function like the “Map Tags to Styles” option does in the pulldown menu, which actually applies the correct style sheets to the appropriately tagged XML text…which is what I need it to do.

Thanks
Chris

Hi, Chris–

Yes, I was actually. If you look in my sample script, you’ll see two loops:


set PStyleCount to (get count of paragraph styles)
       repeat with p from 1 to PStyleCount
           set thisPStyle to paragraph style p
           set thisPStyleObj to object reference of thisPStyle
           set thisPStyleName to name of thisPStyleObj as string
           if thisPStyleName is not "[Basic Paragraph]" and thisPStyleName is not "[No Paragraph Style]" then --Avoids this error: The tag you are trying to create has an invalid name.
               set XMLImportMap to make XML import map with properties {mapped style:thisPStyle, markup tag:thisPStyleName}
           end if
       end repeat
       
       set CStyleCount to (get count of character styles)
       repeat with p from 1 to CStyleCount
           set thisCStyle to character style p
           set thisCStyleObj to object reference of thisCStyle
           set thisCStyleName to name of thisCStyleObj as string
           if thisCStyleName is not "[None]" and thisCStyleName is not "[No Character Style]" then --Avoids this error: The tag you are trying to create has an invalid name.
               set XMLImportMap to make XML import map with properties {mapped style:thisCStyle, markup tag:thisCStyleName}
           end if
       end repeat

As long as the tags match the style names, this will get the styles and tags to map to each other. It’s very confusing, and I found the solution in Applescripting InDesign (ISBN: 09701726511, Page 130). the specific line of code you’re loking for is


set XMLImportMap to make XML import map with properties {mapped style:thisCStyle, markup tag:thisCStyleName}

This way, you can map any style to any tag (and vice versa). But since my solution needs to be completely automated, I’m making sure the DTD and the XML all match the style names.

Thanks for the script, BTW. I’m to the point now where the DTD and the XML all import, but the styles are displaying incorrectly and I still need to tackle how to drill down to the specific data points I want to place. I was hoping CS2 would be better than this, but I’m muddling my way through it.

cheers