🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: adobe.scripting.indesign
Article #19881

Re: Mapping Character Styles to XMLTags

#19881
From: Olav Kvern
Date: Wed, 20 Nov 2002 10:12
88 lines
4513 bytes
Adam-- 

I thought I sent this to you earlier, but I guess I sent it to myself instead. Here's a new script. This one works perfectly with your example document. I hadn't anticipated a couple of things. 



tell application "InDesign 2.0.1" 
	set myDocument to active document 
	if (count XML Elements) of myDocument > 0 then 
		repeat with myCounter from 1 to (count character styles of myDocument) 
			if name of character style myCounter of myDocument is not "[No character style]" then 
				my myMapCharStyle(myDocument, name of character style myCounter of myDocument) 
			end if 
		end repeat 
	else 
		display dialog "Document contains no XML elements." 
	end if 
end tell 
--Handler for creating XML Elements from text tagged with a specific character style. 
on myMapCharStyle(myDocument, myCharStyleName) 
	tell application "InDesign 2.0.1" 
		--Create the XML tag, if necessary. 
		tell myDocument 
			--Replace any spaces in the character style name with underscores. 
			set myTagName to my replace_chars(myCharStyleName, " ", "_") 
			try 
				set myXMLTag to XML Tag myTagName 
			on error 
				--XML tag did not exist, so create it. 
				set myXMLTag to make XML Tag with properties {name:myTagName} 
			end try 
		end tell 
		--Clear the change preferences 
		set change preferences to nothing 
		--Add the target character style to the find preferences 
		set applied character style of find preferences to character style myCharStyleName of myDocument 
		set myFoundItems to search myDocument for "" 
		--Iterate backwards through the list of found items. Why? The items are all text references from character x to y-- 
		--XML tag markers count as characters, so iterating forwards throws the character count references off as you tag the text. 
		repeat with myCounter from (count myFoundItems) to 1 by -1 
			set myItem to item myCounter of myFoundItems 
			--Check for XML tag markers in the found item. 
			if contents of character 1 of myItem is "<FEFF>" or contents of character -1 of myItem is "<FEFF>" then 
				set myItem to my myStripMarkers(myItem) 
			end if 
			--get a reference to the XML element containing the item 
			set myXMLElement to item 1 of associated XML elements of myItem 
			tell myXMLElement to make XML Element with properties {markup tag:myXMLTag, associated object:myItem} 
		end repeat 
	end tell 
end myMapCharStyle 

--Handler for stripping the XML element marker charactes out of a text object. 
on myStripMarkers(myItem) 
	tell application "InDesign 2.0.1" 
		repeat while contents of character 1 of myItem is "<FEFF>" 
			set myStart to index of character 1 of myItem 
			set myEnd to index of character -1 of myItem 
			set myItem to object reference of characters (myStart + 1) through myEnd of parent story of myItem 
		end repeat 
		repeat while contents of character -1 of myItem is "<FEFF>" 
			set myStart to index of character 1 of myItem 
			set myEnd to index of character -1 of myItem 
			set myItem to object reference of characters myStart through (myEnd - 1) of parent story of myItem 
		end repeat 
	end tell 
	return myItem 
end myStripMarkers 

--Find/change routine from Apple's AppleScript Guidebook 
--module "Essential sub-routines." 
on replace_chars(this_text, search_string, replacement_string) 
	set AppleScript's text item delimiters to the search_string 
	set the item_list to every text item of this_text 
	set AppleScript's text item delimiters to the replacement_string 
	set this_text to the item_list as string 
	set AppleScript's text item delimiters to "" 
	return this_text 
end replace_chars 




Please let me know if this works for you! Try it with the same test document you sent me. 

Thanks, 

Ole
Technical Details
Message-ID: <1de6a436.7@WebX.la2eafNXanI>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-06!sn-xit-08!supernews.com!newspump.sol.net!news.maxwell.syr.edu!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!WebX
References: <1de6a436.-1@WebX.la2eafNXanI>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f939a,1d3166bf8ca06c6
X-Google-Attributes: gidf939a,public
X-Google-ArrivalTime: 2002-11-20 02:37:28 PST
From: "Olav Kvern" <okvern@adobe.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-06!sn-xit-08!supernews.com!newspump.sol.net!news.maxwell.syr.edu!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!WebX
Newsgroups: adobe.scripting.indesign
Subject: Re: Mapping Character Styles to XMLTags
Message-ID: <1de6a436.7@WebX.la2eafNXanI>
Date: Wed, 20 Nov 2002 02:12:31 -0800
References: <1de6a436.-1@WebX.la2eafNXanI>
Lines: 99
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit