Writing an XML file back to disk in sandboxed environment


Over the last few days, I learned from books and Macscripter how to:

read an XML file from disk using NSURLBookmarkCreationWithSecurityScope & NSXMLDocument
how to find a node of interest with nodesForXPath
how to change the text value of the node with Array’s objectAtIndex:0 and setStringValue

  • carefully avoiding the use of FINDER or SYSTEM EVENTS, so it could be accepted for the App Store.

However, now I am stuck for 1 1/2 days at trying to save the XMLDocument back:
I found this code, derived from a 2008 post: https://macscripter.net/viewtopic.php?id=27313

write theXMLContent to myFilePath as «class utf8»

But - I guess - because it uses extended ASCII characters, I got wrong Token errors on compiling.

I tried to change in Xcode preferences the encoding language to Western Roman, but to no avail.

I also tried to write the whole string: «class utf8» in ASCII characters, e.g. ((ASCII character xxx) + (ASCII character yyy)) - but again get an error.

What am I missing?

Thank you for any help or pointers!

It’s just without the characters

write theXMLContent to myFilePath as «class utf8»

Thank you very much Stefan for your prompt reply!

Yes, that one does not give a token error, but it also does not write the XMLdocument back to file. I ask the routine to log out “error” on error and it does do that but without printing in the debug console, what exactly the problem is.

I can see that the file path is correct as I have :

set eof of openfile to 0 -- will empty the file to avoid 'remains', if new file is shorter

… and the file is then shortened.

I also tried with and without the following lines from above macscripter link:

set BOM_UTF8 to ((ASCII character 239) & (ASCII character 187) & (ASCII character 191))
                write BOM_UTF8 to openfile

Maybe I cannot just write the changed XMLdocument back as it is, but need to convert it beforehand?

In a sandboxed app you have to call on the bookmarked URL startAccessingSecurityScopedResource() before and stopAccessingSecurityScopedResource() after using the URL.

And as you are using ApplScriptObjC anyway you are encouraged to write the data with the Cocoa API

Thanks again Stefan,

yes I have switched on the respective security bookmark and as the eof command does work, it seems that I have the correct permission.

I also now checked, whether my node content changes corrupted the XML document array somehow. I just took the array that I initially created and tried to write it back - same (error) result.

I will read now into respective Cocoa API routines to test an alternative.

Thank you!



ok, this time I just read the file as class utf8 and writing it back again as class utf8 and that works fine.

So, making the XMLDocument first and then not changing it back to utf8 before writing seems to be the issue. I need to read more into it, what exactly the XMLDocument format is.

Hallelujah or UPDATE 2

Changing the XMLDocument into a data object with XMLDataWithOptions let’s me write it out.

Now I have 2 ways to write the XMLfile back and can choose :slight_smile: – and can continue with the rest of the proof of principle work

Thank you for all the pointers and help Stefan!