Reorganize contents of a text file

I am trying to recompile a text file that contains xml tags within it.

I know how to read to a file, but I don’t know a good way to search through a file using a start and end tag as the search criteria, and then move/copy that string to either the end of a new file, or the end of the same file.

In other words, I am searching for a method to read through a XML text document and organize the XML tags within so they flow in a consistent structure. I also intend to strip the “” and " tags out from the file.

Unorganized contents of text file below: (subject2, qualifier2, subject1, qualifier1, offer2, offer1
------->>>>

other blah blah blah
other random textother blah blah blahother random text
blah blah blahoffer1


Below is the result I am trying to achieve. Notice how the text file below is organized: (offer1, subject1, qualifier1, offer2, subject2, qualifier2)
-------->>>>

blah blah blahother blah blah blahother random textblah blah blahother blah blah blahother random text

Any help or suggestions would be greatly appreciated.

Thanks in advance,
Jeff

StefanK will smack me for showing you this… but



set theText to "<Story><subject2>other blah blah blah</subject2></Story>
<Story><qualifier2>other random text</qualifier2><subject1>other blah blah blah</subject1></Story><Story><qualifier1>other random text</qualifier1></Story>
<Story><offer2>blah blah blah</offer2></Story><Story>offer1</Story>"


set theVar to text ((offset of "<subject2>" in theText) + 0) thru ((offset of "</subject2>" in theText) + 10) of theText


from there it’s just a matter of placing theVar where you want it. StefanK will tell you to use Text Item Delimiters, which is why he will smack me for showing you this. =)

You will need to edit the +0 and +10 to + or - however long the offset is.

Thank you aaon,

I suppose I spoke to soon and assumed I can read a file. I am not sure why your script doesn’t work when trying to read a .xml file whose contents include the text pasted below?

<?xml version="1.0" encoding="UTF-16" standalone="yes"?>

Celebrating 25 years!676-0332©7504 Creedmoor Rd.©(In Stonehenge Shopping Ctr)©Hours: Mon-Thurs & Sat 11am to 10pm©Fri 11am to 11pm Sun 11am to 9pm©www.buffaloscafe.comFamily Value©48 Wings©4 Soft Drinks©2 French Fries©$35.95©Mon - Thurs©Dine-In Only. Coupons may not be combined with any other offer. Expires 11/30/10$5 OFF©$25 Or More© Excludes alcohol©Mon - Thurs©Dine-In Only. Coupons may not be combined with any other offer. Expires 11/30/101/2 Price©Off Lunch Entree©With the purchase of (2) drinks©and Lunch Entree of equal or greater value.©Mon - Thurs©Dine-In Only. Coupons may not be combined with any other offer. Expires 11/30/101/2 Price©Off Dinner Entree©With the purchase of (2) drinks©and Dinner Entree of equal or greater value.©Mon - Thurs©Dine-In Only. Coupons may not be combined with any other offer. Expires 11/30/10

I also tried repeating this thru the 3 paragraphs in this file but that didn’t help either. The scrip I am using is as follows.

tell application "Finder"
	set userFile to ((path to current user folder as text) & "test.xml")
	try
		set userData to open for access file userFile with write permission
		set theText to (read userData)
		try
			set theVar to text ((offset of "<

Your applescript is missing then end

tell application “Finder”
set userFile to ((path to current user folder as text) & “test.xml”)
try
set userData to open for access file userFile with write permission
set theText to (read userData)
try
set theVar to text ((offset of x in theText) + 0) thru ((offset of y in theText) + 10) of theText
display dialog theVar as string
end try
close access userData
on error
try
close access file userFile
end try
end try
end tell

I have no idea why this isn’t compiling: But the x in the script above should be “” and the y should be "

I jsut added some error handeling. The only issue that came up for me was x and y not being declared, test.xml loaded just fine… Give this a run and see if you get an error.

property x : "<subject2>"
property y : "</subject2>"
tell application "Finder"
	set userFile to ((path to current user folder as text) & "test.xml")
	display dialog userFile
	try
		set userData to open for access file userFile with write permission
		set theText to (read userData)
		try
			set theVar to text ((offset of x in theText) + 0) thru ((offset of y in theText) + 10) of theText
			display dialog theVar as string
		on error emsg
			display dialog emsg
		end try
		close access userData
	on error emsg
		display dialog emsg
		try
			close access file userFile
		end try
	end try
end tell

I do receive an error after the first dialog that properly displays the file path:

This is the error:

display dialog "Can’t get text 0 thru 10 of "ˇ˛<

It seems like theText isn’t being set. Put
display dialog theText
underneath
set theText to (read userData)
Run it and lemme know. Like I said, this script works perfect on my MacBook Pro. I even copied the xml you posted and created a test.xml

It may be an text encoding issue?

This is what my dialog returns after putting “display dialog theText” in where you suggested:

display dialog "<?xml version=\"1.0\" encoding=\"UTF-16\" standalone=\"yes\"?>
Celebrating 25 years!676-0332šÃ„©7504 Creedmoor Rd.šÃ„©(In Stonehenge Shopping Ctr)šÃ„©Hours: Mon-Thurs & Sat 11am to 10pmšÃ„©Fri 11am to 11pm Sun 11am to 9pmšÃ„©www.buffaloscafe.comFamily ValuešÃ„©48 WingsšÃ„©4 Soft DrinksšÃ„©2 French FriesšÃ„©$35.95šÃ„©Mon - ThursšÃ„©Dine-In Only. Coupons may not be combined with any other offer. Expires 11/30/10$5 OFFšÃ„©$25 Or MorešÃ„© Excludes alcoholšÃ„©Mon - ThursšÃ„©Dine-In Only. Coupons may not be combined with any other offer. Expires 11/30/101/2 PricešÃ„©Off Lunch EntreešÃ„©With the purchase of (2) drinksšÃ„©and Lunch Entree of equal or greater value.šÃ„©Mon - ThursšÃ„©Dine-In Only. Coupons may not be combined with any other offer. Expires 11/30/101/2 PricešÃ„©Off Dinner EntreešÃ„©With the purchase of (2) drinksšÃ„©and Dinner Entree of equal or greater value.šÃ„©Mon - ThursšÃ„©Dine-In Only. Coupons may not be combined with any other offer. Expires 11/30/10<logo href="file:///Users/Admin/jkr/Ad/334700.9577/BuffalosLogo_4CTI.tif"><ImageTag2 href="file:///Users/Admin/jkr/Ad/334700.9577/BuggaloArtBkg_4CI.eps"><ImageTag3 href="file:///Users/Admin/jkr/Ad/334700.9577/BuffalosWings_4CI.eps">