Unfortunately there was a very long pause for me, but now I try to get back into normal life.
I’m trying to create ITT XMLs. Fortunately I haven’t forgot the basics but there is one thing I haven’t figured our yet.
Here a very simplified description.
set someCue to "Line 1
Line 2
Line 3"
set txParList to every paragraph of someCue
set parCount to (count of txParList)
set parTx to (xmlNode's textWithStringValue:((item 1 of txParList) as string))
(theP's addChild:(parTx))
repeat with p from 2 to parCount
set brNode to (xmlNode's elementWithName:"br" stringValue:"")
(theP's addChild:(brNode))
set parTx to (item p of txParList) as string
set parTx to (xmlNode's textWithStringValue:(parTx))
(theP's addChild:(parTx))
end repeat
And I get this result:
<p begin="00:00:00:00" end="00:00:10:00">Line 1<br></br>
Line 2<br></br>
Line 3</p>
This is only partially what I expect, since “Line 2” and “Line 3” have extra white space.
Any help to point me in the right direction is really appreciated.
set brNode to current application's NSXMLNode's alloc()'s initWithKind:(current application's NSXMLElementKind) options:(current application's NSXMLNodeCompactEmptyElement)
brNode's setName:"br"
I don’t see the space here. It might have something to do with how you create theP, or some other option you’ve set elsewhere.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set someCue to "Line 1
Line 2
Line 3"
set theP to current application's NSXMLNode's elementWithName:"p"
set txParList to every paragraph of someCue
set parCount to (count of txParList)
set parTx to (current application's NSXMLNode's textWithStringValue:((item 1 of txParList) as string))
(theP's addChild:(parTx))
repeat with p from 2 to parCount
set brNode to (current application's NSXMLNode's alloc()'s initWithKind:(current application's NSXMLElementKind) options:(current application's NSXMLNodeCompactEmptyElement))
(brNode's setName:"br")
(theP's addChild:(brNode))
set parTx to (item p of txParList) as string
set parTx to (current application's NSXMLNode's textWithStringValue:(parTx))
(theP's addChild:(parTx))
end repeat
return theP
--> (NSXMLElement) <p>Line 1<br/>Line 2<br/>Line 3</p>
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property cA : a reference to current application
property xmlNode : a reference to cA's NSXMLNode
set theFilePath to POSIX path of (choose file)
set theURL to current application's |NSURL|'s fileURLWithPath:theFilePath
set {theXML, theError} to current application's NSXMLDocument's alloc()'s initWithContentsOfURL:theURL options:(current application's NSXMLNodePreserveWhitespace) |error|:(reference)
if theXML = missing value then error (theError's localizedDescription() as text)
set theRoot to (theXML's rootElement)
set {theNodeList, theError} to theXML's nodesForXPath:("//*:p") |error|:(reference)
if theNodeList = missing value then
set theNodeList to {}
end if
set theP to item 1 of theNodeList
set someCue to "Line 1
Line 2
Line 3"
set txParList to every paragraph of someCue
set parCount to (count of txParList)
(theP's setStringValue:((item 1 of txParList) as string) resolvingEntities:(true))
repeat with p from 2 to parCount
set brNode to (current application's NSXMLNode's alloc()'s initWithKind:(current application's NSXMLElementKind) options:(current application's NSXMLNodeCompactEmptyElement))
(brNode's setName:"br")
(theP's addChild:(brNode))
set parTx to (item p of txParList) as string
set parTx to (xmlNode's textWithStringValue:(parTx))
(theP's addChild:(parTx))
end repeat
--return theP --> <p>Line 1<br/>Line 2<br/>Line 3</p>
set check to (theRoot's XMLStringWithOptions:(cA's NSXMLNodePrettyPrint)) as string
set the clipboard to check
The “check” then unfortunately shows
...
<p begin="00:00:00:00" end="00:00:10:00">Line 1<br/>
Line 2<br/>
Line 3</p>
That just came into my mind.
So I replaced the NSXMLNodePrettyPrint with NSXMLNodePreserveAll.
That seems to work but instead of double quotes (") now only single quotes (’) are used in the XML.
I haven’t seen that before and don’t know about overall consequences.