• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: XMLlib library - somewhat.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: XMLlib library - somewhat.


  • Subject: Re: XMLlib library - somewhat.
  • From: Shane Stanley <email@hidden>
  • Date: Sat, 03 May 2014 14:08:44 +1000

On 3 May 2014, at 12:50 am, Alex Zavatone <email@hidden> wrote:

It seems that we have three main data file formats that it would be nice to be able to read and write and convert back and forth that can be represented in memory as NSArrays, NSDictionaries in Objective-C and in AppleScript as lists and records.

JSON
XML
Plist

Though many times complicated XML files require a defining schema to be properly read and serialized, the plist structure is self defining.

I'm messing around with creation of some rapid development tools for iOS and after seeing the internal memory structures and disk based file formats and seeing this response to the thread, it sure seems that a library for AppleScript and for Cocoa to convert between the differing file formats would be a terribly useful thing to have.

In Objective-C Cocoa, writing or reading an NSDictionary or NSArray to a pList is 1 line of code.  
JSON can also be read and serialized to a dict without too much fuss using NSJSONSerialization.
Simple XML (that doesn't require a schema) can also be handled easily as Shane shows below.

I don't know if I'll have the time to put into all of this, but if anyone is inclined to start some Objective-C and some AppleScript and/or some ASOC, I'd be glad to help.  It sure would be useful for everyone.

I can't see that you can do much generic with most XML; you need to know the structure to do anything useful. But converting between property list/JSON/AppleScript is a frequent enough request. So here's a starter ASObjC-based library:

use framework "Foundation"

-- pass a string, list, record or number, and either a path to save the result to, or missing value to have it returned as text
on convertASToJSON:someASThing saveTo:posixPath
--convert to JSON data
set {theData, theError} to current application's NSJSONSerialization's dataWithJSONObject:someASThing options:0 |error|:(reference)
if theData is missing value then error (theError's localizedDescription() as text) number -10000
if posixPath is missing value then -- return string
-- convert data to a UTF8 string
set someString to current application's NSString's alloc()'s initWithData:theData encoding:(current application's NSUTF8StringEncoding)
return someString as text
else
-- write data to file
theData's writeToFile:posixPath atomically:true
return result as boolean -- returns false if save failed
end if
end convertASToJSON:saveTo:

-- pass either a POSIX path to the JSON file, or a JSON string; isPath is a boolean value to tell which
on convertJSONToAS:jsonStringOrPath isPath:isPath
if isPath then -- read file as data
set theData to current application's NSData's dataWithContentsOfFile:jsonStringOrPath
else -- it's a string, convert to data
set aString to current application's NSString's stringWithString:jsonStringOrPath
set theData to aString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
end if
-- convert to Cocoa object
set {theThing, theError} to current application's NSJSONSerialization's JSONObjectWithData:theData options:0 |error|:(reference)
if theThing is missing value then error (theError's localizedDescription() as text) number -10000
-- we don't know the class of theThing for coercion, so...
if (theThing's isKindOfClass:(current application's NSArray)) as integer = 1 then
return theThing as list
else
return item 1 of (theThing as list)
end if
end convertJSONToAS:isPath:

-- pass a string, list, record or number, and either a path to save the result to, or missing value to have it returned as text
on convertASToPlist:someASThing saveTo:posixPath
if posixPath is missing value then -- return string
-- convert to property list data
set {theData, theError} to current application's NSPropertyListSerialization's dataWithPropertyList:someASThing |format|:(current application's NSPropertyListXMLFormat_v1_0) options:0 |error|:(reference) -- don't use binary format
if theData is missing value then error (theError's localizedDescription() as text) number -10000
-- convert data to UTF8 string
set someString to current application's NSString's alloc()'s initWithData:theData encoding:(current application's NSUTF8StringEncoding)
return someString as text
else -- saving to file
-- convert to property list data
set {theData, theError} to current application's NSPropertyListSerialization's dataWithPropertyList:someASThing |format|:(current application's NSPropertyListBinaryFormat_v1_0) options:0 |error|:(reference) -- might as well use binary format
if theData is missing value then error (theError's localizedDescription() as text) number -10000
-- write data to file
theData's writeToFile:posixPath atomically:true
return result as boolean -- returns false if save failed
end if
end convertASToPlist:saveTo:

-- pass either a POSIX path to the .plist file, or a property list string; isPath is a boolean value to tell which
on convertPlistToAS:plistStringOrPath isPath:isPath
if isPath then -- read file as data
set theData to current application's NSData's dataWithContentsOfFile:plistStringOrPath
else -- it's a string, convert to data
set aString to current application's NSString's stringWithString:plistStringOrPath
set theData to aString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
end if
-- convert to Cocoa object
set {theThing, theError} to current application's NSPropertyListSerialization's propertyListWithData:theData options:0 |format|:(missing value) |error|:(reference)
if theThing is missing value then error (theError's localizedDescription() as text) number -10000
-- we don't know the class of theThing for coercion, so...
if (theThing's isKindOfClass:(current application's NSArray)) as integer = 1 then
return theThing as list
else
return item 1 of (theThing as list)
end if
end convertPlistToAS:isPath:

-- pass either a POSIX path to the JSON file, or a JSON string; isPath is a boolean value to tell which. saveTo is either a path to save the result to, or missing value to have it returned as text
on convertJSONToPlist:jsonStringOrPath isPath:isPath saveTo:posixPath
if isPath then -- read file as data
set theData to current application's NSData's dataWithContentsOfFile:jsonStringOrPath
else -- it's a string, convert to data
set aString to current application's NSString's stringWithString:jsonStringOrPath
set theData to aString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
end if
-- convert to Cocoa object
set {theThing, theError} to current application's NSJSONSerialization's JSONObjectWithData:theData options:0 |error|:(reference)
if theThing is missing value then error (theError's localizedDescription() as text) number -10000
if posixPath is missing value then -- return string
-- convert to property list data
set {theData, theError} to current application's NSPropertyListSerialization's dataWithPropertyList:theThing |format|:(current application's NSPropertyListXMLFormat_v1_0) options:0 |error|:(reference) -- don't use binary format
if theData is missing value then error (theError's localizedDescription() as text) number -10000
-- convert data to UTF8 string
set someString to current application's NSString's alloc()'s initWithData:theData encoding:(current application's NSUTF8StringEncoding)
return someString as text
else -- saving to file
-- convert to property list data
set {theData, theError} to current application's NSPropertyListSerialization's dataWithPropertyList:theThing |format|:(current application's NSPropertyListBinaryFormat_v1_0) options:0 |error|:(reference) -- might as well use binary format
if theData is missing value then error (theError's localizedDescription() as text) number -10000
-- write data to file
theData's writeToFile:posixPath atomically:true
return result as boolean -- returns false if save failed
end if
end convertJSONToPlist:isPath:saveTo:

-- pass either a POSIX path to the .plist file, or a property list string; isPath is a boolean value to tell which. saveTo is either a path to save the result to, or missing value to have it returned as text
on convertPlistToJSON:plistStringOrPath isPath:isPath saveTo:posixPath
if isPath then -- read file as data
set theData to current application's NSData's dataWithContentsOfFile:plistStringOrPath
else -- it's a string, convert to data
set aString to current application's NSString's stringWithString:plistStringOrPath
set theData to aString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
end if
-- convert to Cocoa object
set {theThing, theError} to current application's NSPropertyListSerialization's propertyListWithData:theData options:0 |format|:(missing value) |error|:(reference)
if theThing is missing value then error (theError's localizedDescription() as text) number -10000
--convert to JSON data
set {theData, theError} to current application's NSJSONSerialization's dataWithJSONObject:theThing options:0 |error|:(reference)
if theData is missing value then error (theError's localizedDescription() as text) number -10000
if posixPath is missing value then -- return string
-- convert data to a UTF8 string
set someString to current application's NSString's alloc()'s initWithData:theData encoding:(current application's NSUTF8StringEncoding)
return someString as text
else
-- write data to file
theData's writeToFile:posixPath atomically:true
return result as boolean -- returns false if save failed
end if
end convertPlistToJSON:isPath:saveTo:

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: XMLlib library - somewhat.
      • From: Shane Stanley <email@hidden>
References: 
 >Re: XMLlib library - somewhat. (From: Alex Zavatone <email@hidden>)

  • Prev by Date: Re: Quicktime export permissions.
  • Next by Date: Re: XMLlib library - somewhat.
  • Previous by thread: Re: XMLlib library - somewhat.
  • Next by thread: Re: XMLlib library - somewhat.
  • Index(es):
    • Date
    • Thread