Help manipulating a plist file

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
doobox
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 284
Joined: Tue May 24, 2011 11:47 pm

Help manipulating a plist file

Post by doobox » Thu Jun 16, 2011 5:11 pm

Hi there,

Here is a section of the file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>2d955670-b357-4848-8a27-b0c8eaeaf367</key>
	<dict>
		<key>AccountURL</key>
		<string>pop://sales%40doobox.co.uk@mail.doobox.co.uk/</string>
		<key>Signatures</key>
		<array>
			<string>950E3D7D-234D-4DA7-9345-957635200E39</string>
			<string>1dooboxsig</string>
		</array>
	</dict>
	<key>AllSignaturesKey</key>
	<array>
		<dict>
			<key>SignatureIsRich</key>
			<false/>
			<key>SignatureName</key>
			<string>Signature #1</string>
			<key>SignatureUniqueId</key>
			
So i have the file contents, and i currently have them displayed in a field, visible on the stack.
The file content is stored in a variable called: filecontent


I think i could explain that better in plain English.. Soory this is mixed with livecode, but i guess you guys that know, will know what is and what is not:

Code: Select all

open file specialFolderPath("Home") & "/Library/Mail/Signatures/SignaturesByAccount.plist" for read
   read from file specialFolderPath("Home") & "/Library/Mail/Signatures/SignaturesByAccount.plist" until EOF
   local filecontent
   put it into filecontent

  search filecontent for all instances of an email address between the string values of
"<key>AccountURL</key>" and "<key>Signatures</key>"

put search result into theemailaddresses

put theemailaddresses into field emailaccounts // i am ok with this

upon clicking any of the addresses do this: // i am ok with this line

local mynewarray ="<array>
			<string>950E3D7D-234D-4DA7-9345-957635200E39</string>
			<string>newsigniture</string>
		</array>"

put mynewarray into the file at the specific point
just before closing </dict> on the <dict> that the clicked email address resides in.


Any help with any stage of the above would be appreciated.
Kind Regards
Gary

https://www.doobox.co.uk

doobox
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 284
Joined: Tue May 24, 2011 11:47 pm

Re: Help manipulating a plist file

Post by doobox » Thu Jun 16, 2011 8:33 pm

I just cant get the basics of this.

I am trying some tests, here is an example:

Code: Select all

on mouseUp
   local thetext
   put field "Shakespeare" into thetext
// thetext now contains the complete plist file
   replaceText(field "Shakespeare","<key>AccountURL</key>","replacementtext")
   // error: button "Write": execution error at line 5 (Handler: can't find handler) near "replaceText", char 1
end mouseUp
Why is that generating an error..?
Kind Regards
Gary

https://www.doobox.co.uk

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Help manipulating a plist file

Post by Mark » Thu Jun 16, 2011 8:45 pm

Hi,

A function returns a value, which needs to be put into something. You always need to get or put a function: put replaceText(...).

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

doobox
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 284
Joined: Tue May 24, 2011 11:47 pm

Re: Help manipulating a plist file

Post by doobox » Thu Jun 16, 2011 8:51 pm

Gotcha :-)

Thank you.
Kind Regards
Gary

https://www.doobox.co.uk

doobox
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 284
Joined: Tue May 24, 2011 11:47 pm

Re: Help manipulating a plist file

Post by doobox » Fri Jun 17, 2011 1:34 pm

Hi there,

I did manage to put together something that will modify the plist file to my needs:

Code: Select all

on mouseUp
   local thetext, myreg
   put field "editor" into thetext
  put "(?<=\<key>Account)((?:.|\s)*?)(?=\</array>)" into myreg
   put replaceText(field "editor",myreg," The replacement text ") into field "preview" 
end mouseUp
However i have been warned off this approach, using regex on html..!

What are my alternatives in live code..?

Kind regards
Gary
Kind Regards
Gary

https://www.doobox.co.uk

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Help manipulating a plist file

Post by Mark » Fri Jun 17, 2011 2:01 pm

Hi Gary,

I think your regex will find only one instance of an e-mail address or URL (it isn't entirely clear to me what you're looking for). It might be more effective to use a repeat loop to find all key tags and check if they contain an e-mail address or URL. For example, you could use offset("<key",myData,myOldOffset). The third parameter in the offset function is very useful, although it can be somewhat complicated to get it to work correctly.

Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

doobox
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 284
Joined: Tue May 24, 2011 11:47 pm

Re: Help manipulating a plist file

Post by doobox » Fri Jun 17, 2011 2:26 pm

I am at different times looking for or to replace different things. I should focus on the one in hand.

There are potentially multiple instances of:

Code: Select all

<key>AccountURL</key>
		<string>pop://sales%40doobox.co.uk@mail.doobox.co.uk/</string>
		<key>Signatures</key>
		<array>
			<string>950E3D7D-234D-4DA7-9345-957635200E39</string>
			<string>1dooboxsig</string>
		</array>
That regex in the above post was my testing / trying to locate all instances of this block.

I guess i would put each instance into an array. Then loop over that array running a regex on each block to extract the email addresses.
I will use these addresses in the app.

At a later time i want to write another array back to that file in the correct location.
So the code at the top there ^.. will become:

Code: Select all

<key>AccountURL</key>
		<string>pop://sales%40doobox.co.uk@mail.doobox.co.uk/</string>
		<key>Signatures</key>
		<array>
			<string>950E3D7D-234D-4DA7-9345-957635200E39</string>
			<string>1dooboxsig</string>
		</array>
               <array>
			<string>MyNewIdHere</string>
			<string>1newIdentifierHere</string>
		</array>
I can easily get the info from the app and construct the new array ready to inject into the file.
It is the all the regex that is my failing. :-(
Kind Regards
Gary

https://www.doobox.co.uk

doobox
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 284
Joined: Tue May 24, 2011 11:47 pm

Re: Help manipulating a plist file

Post by doobox » Sat Jun 18, 2011 8:43 am

Who knows what the hokey cokey looks like..?
Because i am doing it right now :-) Success, i have the email addresses parsed.

Code: Select all

on mouseUp
   repeat for each line theLine in field "editor"
      if theLine contains "%40" then
         put theLine & return after tList
      end if
   end repeat
   
   put replaceText(tList,"<string>(.*?)//",empty) into tList
   put replaceText(tList,"@(.*)",empty) into tList
   put replaceText(tList,"%40","@") into tList
   
   put tList into field "result"
end mouseUp
Kind Regards
Gary

https://www.doobox.co.uk

Post Reply