Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mrindia

macrumors newbie
Original poster
May 30, 2013
3
0
I have job.txt with contents:

Purolator Edmonton
607431612227

And I want job.xml with contents:

<tags>
<tag1>Purolator Edmonton</tag1>
<tag2>607431612227</tag2>
</tags>

Please help.
 

Mark FX

macrumors regular
Nov 18, 2011
159
17
West Sussex, UK
Firstly AppleScript is very poor for handling xml type data, and is much better for handling property list type data.
Apple's scripting addition called "System Events" has an XML suit, and property list suit with a scripting dictionary.
But the XML suite is more for reading in and parsing xml data, rather than creating new data and writing it to file.
I had a brief look and play with the "System Events" XML suite for you, but struggled to figure out how to use it.
And there is very little detailed information or documentation in the scripting dictionary, or any online resources.

So because of the above XML suite documentation problems, I have put together a very basic AppleScriptObjC script.
Which uses Mac OS's Cocoa "Foundation" framework instead, which is more complicated for a beginner to understand.
But if you copy and paste the code posted below into a new "Script Editor" file, and click the run button you will see how it works, and then you can always ask again in the forum for further help in modifying it to your needs.

You haven't said what version of Mac OS your running, so the posted code was tested only on Mojave, and may not run on other MacOS versions, so if it does not run as intended, tell us what Mac OS version you're running.

You also haven't said the location of the job.txt file, or where you want to save the job.xml file too.
So the code below lets you choose the text file, and choose the name and location for the xml file.

AppleScript:
use framework "Foundation"

property myApp : a reference to current application

try
    set textFilePath to choose file with prompt "Select the text file to make into XML file." of type {"txt"} default location path to desktop
    set textFileParagraphList to every paragraph of (read textFilePath) as list
on error
    return --User Canceled the text file selection OR the text file could not be read
end try

set AppleScript's text item delimiters to ":"
set textFileName to last item of (every text item of (textFilePath as text))
set AppleScript's text item delimiters to "."
set textFileTitle to first item of every text item of textFileName
set AppleScript's text item delimiters to ""

set XMLDataText to xmlHeader() & "<tags>" & return
repeat with indexNumber from 1 to count of textFileParagraphList
    set paragraphTextItem to item indexNumber of textFileParagraphList as text
    set XMLDataText to XMLDataText & xmlTag(indexNumber, paragraphTextItem)
end repeat
set XMLDataText to XMLDataText & "</tags>"

try
    set XMLDataFileName to choose file name with prompt "type a name for your new XML file." default name (textFileTitle & ".xml") default location path to desktop
    set XMLDataFilePath to POSIX path of XMLDataFileName as text
on error
    return --User Canceled the naming of the new XML file
end try

set {XMLDocument, theError} to myApp's NSXMLDocument's alloc()'s initWithXMLString:XMLDataText options:(myApp's NSXMLDocumentTidyXML) |error|:(reference)
if theError is not equal to missing value then
    display alert (theError's localizedDescription()) message (theError's localizedRecoverySuggestion()) buttons {"OK"} as warning giving up after 30
else
    set XMLDocumentData to myApp's NSData's dataWithData:(XMLDocument's XMLDataWithOptions:(myApp's NSXMLNodePrettyPrint))
end if

set XMLDocumentDataWrittenToFile to XMLDocumentData's writeToFile:XMLDataFilePath atomically:true
if XMLDocumentDataWrittenToFile is equal to false then
    display alert "An error occured writing the XML data file." message "Check XML data file writing code for errors." as warning buttons {"OK"} giving up after 30
else
    display dialog "Success writing and saving the new XML data to the file system." buttons {"OK"} with icon note giving up after 30
end if

on xmlHeader()
    return "<?xml version=\"1.0\" encoding=\"utf-8\"?>" & return
end xmlHeader

on xmlTag(tagNumber, tagText)
    return tab & "<tag" & (tagNumber as text) & ">" & tagText & "</tag" & (tagNumber as text) & ">" & return
end xmlTag

The above code is not meant to be a complete solution to your project, but is only meant to be a starting point.
And may need further work to achieve your desired goal, but more detail on what you want would need to be supplied.

Regards Mark
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.