open and save xml file

Hi,

i want to make a applescript to make some fix changes in a xml-file.
I thought I can use the program texteditor or smultron to do this.
I already could make my script to open the xml-file and apply some find and changes items.
Now I have to save the document on the same location an with the same name.
How do I have to do this?

hi

save active workbook
	close active workbook

This will save the workbook in its original place and save the changes
bills

Thanks :slight_smile:

I will try it tomorrow

Is your file an Office one.

workbook is meaningful in Excel, I’m not sure that it is meaningful in other applications.

Yvan KOENIG (VALLAURIS, France) dimanche 31 octobre 2010 17:54:50

The file is a file created by Easycatalog, a plug-in for Indesign.
The file includes field options. I want to make changes in the xml so it doesn’t take a lot of time doing it one by one in Indesign.

Normally I open the file and do the changes manually.

Isn’t TextEdit the write program to do this? Is Smultron better?

My script works in TextEdit and in Smultron. I could not let it save it on the same path and close the document.
Tomorrow I will be at my office and I can try to apply the suggestions.

“save workbook” doesn’t wordk :frowning:

This is my script

tell application "TextEdit"
	activate
	open "/Volumes/DATA/Users/evin/Documents/EasyCatalog Workspace/TEST/Data/Fields.xml"
	tell application "System Events"
		tell process "TextEdit"
			keystroke "f" using {command down}
		end tell
	end tell
	
	tell application "System Events"
		tell process "TextEdit"
			keystroke "type = \"numeric\""
			keystroke tab
			keystroke "type=\"alpha\""
		end tell
		
		tell process "TextEdit"
			keystroke "stripwhitespace = \"true\""
			keystroke tab
			keystroke "stripwhitespace = \"false\""
		end tell
		
		tell process "TextEdit"
			tell window "Zoeken"
				click button "Vervang alles"
				tell window "Find"
					keystroke "w" using {command down}
				end tell
			end tell
		end tell
	end tell
	save workbook
	close workbook
end tell


Does anyone know how I have to save my document?

Hi,

as a XML file is a plain text file, TextEdit is not needed to find and replace text.
Try this, the script assumes that the XML file is UTF-8 encoded


set xmlFilePath to "DATA:Users:Documents:EasyCatalog Workspace:TEST:Data:Fields.xml"

property searchList : {"type = \"numeric\"", "stripwhitespace = \"true\""}
property replaceList : {"type=\"alpha\"", "stripwhitespace = \"false\""}

try
	set datastream to open for access file xmlFilePath with write permission
	set xmlText to read datastream as «class utf8»
	repeat with i from 1 to (count searchList)
		set xmlText to findAndReplace(xmlText, item i of searchList, item i of replaceList)
	end repeat
	write xmlText to datastream as «class utf8»
	close access datastream
on error
	try
		close access file xmlFilePath
	end try
end try

on findAndReplace(theText, searchString, replaceString)
	set {TID, text item delimiters} to {text item delimiters, searchString}
	set theText to text items of theText
	set text item delimiters to replaceString
	set theText to theText as text
	set text item delimiters to TID
	return theText
end findAndReplace


Thanks Stefan!

If I open the xml after applying the script, it seems that there was nothing changed.

Am I doing something wrong?

the search and replace strings must match exactly.
Maybe they contain special characters like tabs

This is the text to change:

<field name = “sap_code” key = “true” type = “numeric” stripwhitespace = “true” prefix = “” suffix = “” language = “” stylesheet = “[No character style]” cleansing = “” tagged = “true” contenthastags = “false” htmltags = “true” preservelocalformatting = “false” ingorewhitespacechanges = “false” imagepath = “” imageextension = “” imagesubstitute = “” imagescalepercent = “100” imagealign = “centre” clipping = “centre” imagescale = “proportionally” locationtype = “folder” insertasinline = “false” inlineyoffset = “” imagexoffset = “” imageyoffset = “” scalefield = “sap_code” storeurlimagesinsinglefolder = “false” dbstatement = “Update Set ‘sap_code’ = {{VALUE}} where = “{{KEY}}”” dbupdate = “false” hyperlinklegend = “” hyperlinkdestination = “” importedtextpath = “” importedtextextensions = “” thousandsseparator = “” decimalseparator = “.” filldownonsync = “false” pagelevelformattingrules = “false” importusingfilter = “false” excludedocupdate = “false” excludedatasourceupdate = “false”/>

I had to place a \ before the ". Without the \ applescript gives a error. Does this cause my problem?

Yes, the backslashes are needed to escape the quotes.
I tried the script with your text and it worked. I just had to add spaces in the first item of replaceList (“type = "alpha"”)

Insert the set eof line before the write line to make sure that the text will be rewritten from the beginning


.
end repeat
set eof datastream to 0
write xmlText to datastream as «class utf8»
.

As mentioned above, the whole text must be UTF-8 encoded !

How can I check if the text is UTF-8 encoded?

I could resolve it :slight_smile:

for who is interested


tell application "TextEdit"
	activate
	open "/Volumes/DATA/Users/evin/Documents/EasyCatalog Workspace/TEST/Data/Fields.xml"
	tell application "System Events"
		tell process "TextEdit"
			keystroke "f" using {command down}
		end tell
	end tell
	
	tell application "System Events"
		tell process "TextEdit"
			keystroke "type = \"numeric\""
			keystroke tab
			keystroke "type = \"alpha\""
			tell window "Zoeken"
				click button "Vervang alles"
				tell window "Find"
					keystroke "w" using {command down}
				end tell
			end tell
		end tell
	end tell
	tell application "TextEdit"
		
		activate
		tell application "System Events"
			tell process "TextEdit"
				keystroke "f" using {command down}
			end tell
		end tell
		
		tell application "System Events"
			tell process "TextEdit"
				keystroke "stripwhitespace = \"true\""
				keystroke tab
				keystroke "stripwhitespace = \"false\""
				tell window "Zoeken"
					click button "Vervang alles"
					tell window "Find"
						keystroke "w" using {command down}
					end tell
				end tell
			end tell
			
			tell application "TextEdit"
				set thedoc to document 1
				save thedoc
				close thedoc
			end tell
		end tell
	end tell
end tell