Thursday, May 28, 2020

#1 2020-02-19 12:41:36 pm

Uwe
Member
Registered: 2018-03-29
Posts: 24

Writing an XML file back to disk in sandboxed environment

Hello,

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

Applescript:

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!
Uwe

Last edited by Uwe (2020-02-19 12:47:43 pm)


Filed under: xml, applescript, xcode

Offline

 

#2 2020-02-19 01:44:13 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11686
Website

Re: Writing an XML file back to disk in sandboxed environment

It's just without the  characters

Applescript:

write theXMLContent to myFilePath as «class utf8»


regards

Stefan

Offline

 

#3 2020-02-20 02:34:58 am

Uwe
Member
Registered: 2018-03-29
Posts: 24

Re: Writing an XML file back to disk in sandboxed environment

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 :

Applescript:

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:

Applescript:

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?

Offline

 

#4 2020-02-20 02:44:26 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11686
Website

Re: Writing an XML file back to disk in sandboxed environment

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


regards

Stefan

Offline

 

#5 2020-02-20 11:07:03 am

Uwe
Member
Registered: 2018-03-29
Posts: 24

Re: Writing an XML file back to disk in sandboxed environment

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!

Uwe

UPDATE

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 smile -- and can continue with the rest of the proof of principle work

Thank you for all the pointers and help Stefan!

Last edited by Uwe (2020-02-20 12:10:57 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)