失礼致します。
AppleScriptでも、System EventsのXML Suiteにあるクラスを使うことで、xmlファイルを扱うことができます。
→ Working with XML
しかしながら、これでヘルスケアのデータにアクセスするとエラー(タイムアウト)になってしまうので、AppleScriptObj-CでXPath Queryを使うことでSleep Cycleのエレメントを抽出できました。 以下のステートメンツでは、抽出したエレメンツをデスクトップに書き出します。
use AppleScript version "2.7"
use framework "Foundation"
use scripting additions
set thePath to POSIX path of (choose file)
set theQuery to "//Record[@sourceName='Sleep Cycle']"
set outputXML to (its extractFrom:thePathmatchingXPath:theQuery)
set aFilePath to (POSIX path of (path to desktop folder)) & "outputXML.xml"
set theString to current application'sNSString'sstringWithString:outputXML
set theResult to theString'swriteToFile:aFilePathatomically:trueencoding: (current application'sNSUTF8StringEncoding) |error|: (missing value)
on extractFrom:thePathmatchingXPath:theQuery
set aURL to current application'sclass "NSURL"'s fileURLWithPath:thePath-- make NSURL
set theXMLDoc to current application'sNSXMLDocument'salloc()'s initWithContentsOfURL:aURLoptions: (|error|: (missing value) -- make XMLDoc
set theXMLOutput to current application'sNSXMLElement'salloc()'s initWithName:"output" -- found nodes added to this
set {theResults, theError} to (theXMLDoc'snodesForXPath:theQuery|error|: (reference)) -- query
if theResults is missing value then error (theError'slocalizedDescription()) as text
repeat with i from 1 to count of theResults
set aNode to itemi of theResults
aNode'sdetach() -- need to detach first
(theXMLOutput'saddChild:aNode) -- add node
end repeat
return (theXMLOutput'sXMLStringWithOptions: (current application'sNSXMLNodePrettyPrint)) as text
end extractFrom:matchingXPath:
参照URL:https://macosxautomation.com/applescript/apps/everyday_book.html