View Single Post
Hi everybody-

I've been following this thread for awhile and re-wrote some of the code here to create my own version of this script. You run the script, find your sitemap.xml file, and it does the rest, I believe. Let me know if it works for you as well as it does for me.

Here's a link to the blog post. It contains instructions and the download.

Here's the code so you can see how I did it:


Code:
-- v0. works but includes http:// as a base node *duh*
-- v1. removes http:// baseline, improves performance
-- v2. is a minor re-write
-- v3. removes notion of folder vs. uri shapes
-- prompt user to select sitemap.xml file

-- function that draws each type of box
on makeShape(aTitle, aURL)
	tell application "OmniGraffle Professional 5"
		tell canvas of front window
			return make new shape with properties {text:aTitle, url:aURL, vertical padding:5, autosizing:full, side padding:10, magnets:{{1, 0}, {-1, 0}, {-1, 1}, {-1, -1}}, origin:{(x of canvasSize) / 2.0, (y of canvasSize) / 2.0}}
		end tell
	end tell
end makeShape

on findShape(shapeText)
	tell application "OmniGraffle Professional 5"
		set matchingShapes to shapes of canvas of front window whose url is shapeText
		if (count of matchingShapes) is greater than 0 then
			return item 1 of matchingShapes
		end if
	end tell
end findShape

on drawConnectingLine(aSource, aDestination)
	tell application "OmniGraffle Professional 5"
		tell aSource
			set aLine to connect to aDestination with properties {line type:straight}
		end tell
		set tail magnet of aLine to 1
	end tell
end drawConnectingLine

on makeNewSitemap()
	tell application "OmniGraffle Professional 5"
		make new document
		tell layout info of canvas of front window
			set type to left to right
			-- set children to back to front ordering
		end tell
	end tell
end makeNewSitemap

on cleanupSitemap()
	
	tell application "OmniGraffle Professional 5"
		
		tell canvas of front window
			layout
			page adjust
		end tell
		
	end tell
end cleanupSitemap

set sitemapFile to choose file with prompt "Please select a sitemap.xml file." of type {"XML"}
set sitemapXML to open for access sitemapFile

set uriList to {}

set isDone to false

-- got to be a better way here.
repeat until isDone -- end of sitemapXML
	try
		-- read up to a tag, and see what the tag is
		read sitemapXML before "<"
		set theTag to read sitemapXML before ">"
		
		-- if it is a <loc> tag, then read the contents
		if theTag is "loc" then
			-- jumps to next < start of tag
			set uriText to read sitemapXML before "<"
			-- strip the "http://" is 7 characters 
			set uriText to text 8 thru (length of uriText) of uriText
			copy uriText to the end of uriList
		end if
		
	on error errStr number errorNumber
		-- you'll want the next line if you want to see error messages
		-- display dialog errorNumber
		set isDone to true
	end try
end repeat
close access sitemapXML

makeNewSitemap()

set savedDelimiters to AppleScript's text item delimiters
set text item delimiters to "/"

set nodeList to {}

repeat with currentURI in uriList
	if last character of currentURI is "/" then
		set currentURI to text 1 thru ((length of currentURI) - 1) of currentURI
	end if
	
	set currentItems to text items of currentURI
	repeat with i from 1 to count of items in currentItems
		-- cItem in currentItems
		set cItem to item i of currentItems
		
		if contents of cItem is in nodeList then
			-- find the matching shape within the list, and make it the node parent node 
			-- do nothing
			-- log " cItem in nodeList "
		else
			-- add this item to the list of nodes
			copy contents of cItem to the end of nodeList
			-- create this shape
			set childNode to makeShape(cItem, cItem)
			
			if i is greater than 1 then
				-- this must be a child node, so we must find the previous node's shape
				
				set parentText to contents of item (i - 1) of currentItems
				set parentNode to findShape(parentText)
				drawConnectingLine(parentNode, childNode)
			end if
			
		end if
		--		makeShape(cItem, cItem)
		
	end repeat
end repeat

set text item delimiters to savedDelimiters

cleanupSitemap()