Editing and saving XML

Note: I am pretty new to AppleScript, though I have been programming for many years, so should know fundamentally what I’m doing.

I am trying to automate a currently very manual and time consuming task at my work. It mostly involves copying one folder to a new place, renaming all the files, and changing file references in XML documents. Reading XML seems to work just fine, but modifying/writing that XML I cannot get to work at all. Since this script will live past my own employment and be used by others, I’d prefer to not use third party software, though if I get confirmation that it can ONLY be done that way, I’ll bite the bullet.

I’ve tracked the non-functional bit down to this:

log (new_reference as text)
set value of curAttribute to (new_reference as text)
log value of curAttribute as text

curAttribute is an XML attribute, and setting the value of it does not stick. The first log shows the value that I want, the second log shows the old value. I get no errors, it just doesn’t modify the contents of the XML as I would expect.

The parts surrounding the most relevant bit are below:

		tell application "System Events"
			set theXMLFile to XML file (tmpFile as string)
			tell theXMLFile
				log text of contents of theXMLFile
				set root to XML element "xsl:stylesheet" of theXMLFile
				set imports to XML element "xsl:import" of root
				repeat with curLine in imports
					set attributeValues to XML attributes of curLine
					repeat with curAttribute in attributeValues
						set attributeName to name of curAttribute
						if (attributeName = "href") then
							set AppleScript's text item delimiters to the currentName
							set the item_list to every text item of ((value of curAttribute) as string)
							set AppleScript's text item delimiters to the new_name
							set new_reference to the item_list as string
							set AppleScript's text item delimiters to ""
							log (new_reference as text)
							set value of curAttribute to (new_reference as text)
							log value of curAttribute as text
							-- set value of (item p of (XML attributes of (item i of imports))) to new_reference
						end if
					end repeat
				end repeat
			end tell


you have to save the file.
Unfortunately the XML Suite of System Events doesn’t support the save command.
I recommend to download the Scripting Addition XMLLib osax from Satimage and use it instead

If I need to use third party stuff, that’ll have to be the answer. That said, this answer feels wrong for a few reasons.

Why do my two log statements not agree? Even if it’s not saving the file properly to disk, I would have expected the in-memory content to change and for it to log correctly! Perhaps it’s just more broken than I thought, but the reason I went down the log statement route was basically to try to see if it’s just not saving or breaking at an earlier point, and I thought that indicated pretty clearly that it breaks earlier than the moment of saving.

don’t shoot the messenger :wink:

It seems that System Events can create XML and plist files instantly but can’t modify existing ones

Oh well. In my particular case treating the XML as just text and doing a string replace is not dangerous since we control the format and I know it’s not going to mess anything up. It still makes my programmer mind wince that I have to do it this way.

Thanks for your help!

If you’re running Mavericks or later you can use ApplescriptObjC, which gives you access to NSXMLDocument and friends.