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?
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.
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
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
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 !