🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #20200

Re: AS: finding elements in xml structure

#20200
From: Olav Kvern
Date: Mon, 05 Aug 2002 17:56
89 lines
4269 bytes
matt b2-- 

You're not talking to yourself--others are listening. It's just that it takes a bit to write an example.<g> 

How about this? 

set myXMLFile to choose file name with prompt "Save XML file as:" default name "myXMLFile.spt" 
--generate an example XML file 
my write_to_file("<document>", myXMLFile, false) 
repeat with myCounter from 1 to 10 
	my write_to_file("<section>", myXMLFile, true) 
	my write_to_file("<section_heading>Section Number " & (myCounter as string) & "</section_heading>" & return, myXMLFile, true) 
	my write_to_file("<body_text>This is some example text.</body_text>" & return, myXMLFile, true) 
	my write_to_file("</section>", myXMLFile, true) 
end repeat 
my write_to_file("</document>", myXMLFile, true) 
set myFoundXMLElements to {} 
tell application "InDesign 2.0.1" 
	set myDocument to make document 
	--import the XML file 
	tell myDocument 
		import XML from (myXMLFile as string) 
		--XML element 1 will be the "document" element 
		--find elements whose name is "section" 
		tell XML Element 1 
			--in a "real world" example, there would be xml elements other than "section" inside the "document" element 
			--you can't use "set myXMLElements to xml elements whose markup tag is xml tag "section", so iterate: 
			repeat with myCounter from 1 to (count XML Elements) 
				set myXMLElement to XML Element myCounter 
				if name of markup tag of myXMLElement is "section" then 
					--do something 
					copy myXMLElement to end of myFoundXMLElements 
				end if 
			end repeat 
		end tell 
		--set the number of pages in the document to the number of elements found 
		set pages per document of document preferences to (count myFoundXMLElements) 
		--set the ruler origin to page origin 
		set ruler origin of view preferences to page origin 
		repeat with myCounter from 1 to (count myFoundXMLElements) 
			tell page myCounter 
				--create a text frame on each page 
				set myBounds to my myGetBounds(page myCounter, myDocument) 
				set myTextFrame to make text frame with properties {geometric bounds:myBounds} 
				--tag the text frame with the element 
				set associated object of item myCounter of myFoundXMLElements to myTextFrame 
			end tell 
		end repeat 
	end tell 
end tell 

on myGetBounds(myPage, myDocument) 
	tell application "InDesign 2.0.1" 
		set myPageWidth to page width of document preferences of myDocument 
		set myPageHeight to page height of document preferences of myDocument 
		set myX1 to margin left of margin preferences of myPage 
		set myY1 to margin top of margin preferences of myPage 
		set myX2 to myPageWidth - (margin right of margin preferences of myPage) 
		set myY2 to myPageHeight - (margin bottom of margin preferences of myPage) 
		return {myY1, myX1, myY2, myX2} 
	end tell 
end myGetBounds 

--Write to file routine from Apple's AppleScript Guidebook module 
--"Essential subroutines." (I think.) 
on write_to_file(this_data, target_file, append_data) 
	try 
		set the target_file to the target_file as text 
		set the open_target_file to � 
			open for access file target_file with write permission 
		if append_data is false then � 
			set eof of the open_target_file to 0 
		write this_data to the open_target_file starting at eof 
		close access the open_target_file 
		return true 
	on error 
		try 
			close access file target_file 
		end try 
		return false 
	end try 
end write_to_file 
 

Does that help? 

Thanks, 

Ole
Technical Details
Message-ID: <ef9ff5b.2@WebX.aaQKaf7vad4>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-04!supernews.com!news.maxwell.syr.edu!bnewspeer01.bru.ops.eu.uu.net!lnewspeer00.lnd.ops.eu.uu.net!emea.uu.net!newsfeed01.sul.t-online.de!t-online.de!WebX
References: <ef9ff5b.-1@WebX.aaQKaf7vad4>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f939a,f9bb584fc7851688
X-Google-Attributes: gidf939a,public
X-Google-ArrivalTime: 2002-08-05 11:10:12 PST
From: "Olav Kvern" <okvern@adobe.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-04!supernews.com!news.maxwell.syr.edu!bnewspeer01.bru.ops.eu.uu.net!lnewspeer00.lnd.ops.eu.uu.net!emea.uu.net!newsfeed01.sul.t-online.de!t-online.de!WebX
Newsgroups: adobe.scripting.indesign
Subject: Re: AS: finding elements in xml structure
Message-ID: <ef9ff5b.2@WebX.aaQKaf7vad4>
Date: Mon, 5 Aug 2002 10:56:10 -0700
References: <ef9ff5b.-1@WebX.aaQKaf7vad4>
Lines: 101
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit