Help with filtering

Hello and thank you in advance.

I’m trying to parse a KML file to later turn it into a CSV. Everything seems to work OK but my script keeps adding “” in front of everythign and I can’t figure out why. The AppleScript is below and the KML can be found here: http://www.nwcouncil.org/maps/power/pwr.kml

The only modification I made to the KML was to add returns to the end of the file to make my script recognize the

Right now the script is ment to take each … section and dump that into a list so I can later go back and pull the out of it.


global fileLines, fileContents, FolderChunkList, featureGroups, pointFeatures

set KMLfile to ""
set FolderChunkList to {}
set featureGroups to {}
set pointFeatures to {}

set KMLfile to choose file with prompt "Choose a KML file..." default location (path to home folder) without invisibles

on ReadFile(fileRef)
	set filePath to fileRef as string
	display dialog "KML File: " & filePath
	set fileID to open for access file filePath
	set fileContents to read fileID as text
	
	try
		set fileLines to paragraphs 1 thru -1 of fileContents
	on error
		display dialog "Can't set fileLines to paragraphs 1 thru -1 of fileContents"
	end try
	close access fileID
end ReadFile

on FilterGroupChunks()
	set GroupBeg to {}
	set GroupEnd to {}
	repeat with i from 1 to count items in fileLines
		if (item i in fileLines is "<Folder>") then
			set end of GroupBeg to i as integer
		else if (item i in fileLines is "</Folder>") then
			set end of GroupEnd to i as integer
		end if
	end repeat
	
	set GroupBegNum to count items in GroupBeg
	set GroupEndNum to count items in GroupEnd
	if (GroupBegNum) is equal to (GroupEndNum) then
		try
			repeat with i from 1 to GroupBegNum
				set paragraphBeg to (text item i of GroupBeg as integer) + 1
				set paragraphEnd to (text item i of GroupEnd as integer) - 1
				
				set end of FolderChunkList to paragraphs paragraphBeg thru paragraphEnd of fileContents
			end repeat
		on error
			display dialog "can't set chunk."
		end try
	else
		display dialog "Group counts don't match up"
	end if
end FilterGroupChunks

on WriteToFile(filePath, Chunks)
	-- Open File
	set filePath to (path to desktop) & filePath as string
	set fileID to open for access file filePath with write permission
	
	--ValueList
	repeat with i from 1 to count items in Chunks
		write "-----------------------" & return & return & (text item i of Chunks) & return & return to fileID
	end repeat
	close access fileID
end WriteToFile


ReadFile(KMLfile)
FilterGroupChunks()
WriteToFile("TestWriteChunk", FolderChunkList)

I suspect that you’re running OS X, so this should really be in the AppleScript | OS X forum rather than the Mac OS forum which is pre-X. I’ll move it for you if that’s the case; it will get a better response.

Thank you for moving it, I must have just blanked on where I was posting.

Use System Events’ XML suite or Satimage’s XMLLib osax to work with XML data - it’s faster, simpler and far more reliable.