Le 21/05/2014 à 14:06, Shane Stanley < email@hidden> a écrit : On 21 May 2014, at 8:00 pm, Nigel Garvey < email@hidden> wrote: I don't know if this helps for your current purpose, but the following version of your example script works very well (on my machine) with text twice as long as that generated by Deivy's script:
tell application "TextEdit" to tell document 1's text make new character at end with data "=azertyuiop=" set properties to {font:"Menlo-Regular", size:11.0} end tell
And assuming the real string Yvan is appending is a long one, setting the properties first would probably be quicker.
FWIW, here's another way of editing a TextEdit .rtf document, using an ASObjC-based library rather than TextEdit:
use framework "Foundation" use framework "AppKit"
on testIt() -- make URL of file set aURL to current application's |NSURL|'s fileURLWithPath:"/Users/shane/Documents/ASObjC Explorer for Mavericks/ASObjCExtras_stuff/ASObjCExtras.rtf" -- get the text as an attributed string, as well as the document attributes set {theAttrText, docAttrs} to current application's NSAttributedString's alloc()'s initWithURL:aURL documentAttributes:(reference) -- make the text mutable set theAttrTextMutable to theAttrText's mutableCopy() -- add to the text --theAttrTextMutable's appendAttributedString:(current application's NSAttributedString's alloc()'s initWithString:"=azertyuiop=") repeat 6 times theAttrTextMutable's appendAttributedString:theAttrTextMutable end repeat -- get length of the text set theLength to theAttrTextMutable's |length|() log theLength --> (*925312*) -- make the font set theFont to current application's NSFont's fontWithName:"Menlo-Regular" |size|:11.0 -- add the attribute theAttrTextMutable's addAttribute:(current application's NSFontAttributeName) value:theFont |range|:({0, theLength}) -- convert the attributed string back to RTF data set theData to theAttrTextMutable's RTFFromRange:({0, theLength}) documentAttributes:docAttrs -- save the data to a new file theData's writeToFile:"/Users/shane/Documents/ASObjC Explorer for Mavericks/ASObjCExtras_stuff/ASObjCExtras-2.rtf" atomically:true end testIt
The final string in this case was 925312 characters, or just under 16000 lines. The script takes less than 0.25 seconds on my Mac. If I apply the styling before the appending, it drops to around 0.02 seconds. (And if I don't, and I keep adding more text, the time increases much more than linearly.) I suspect the fact that I'm running from an SSD helps keep the latter value low.
The posted script is just a bare one written to demonstrate the odd behaviour.
In the real life, the true script apply a lot of changes — more than 50 in some cases — to the doc contents. If I change the font at the very end, it's because at the beginning the text is in Menlo-Regular size 11. Setting the new text content change the properties to Helvetica 12 So I must reset Menlo-Regular 11 to retrieve the original setting. The size is not a problem but retrieving a monospaced font is useful because it helps me to scan the document in search of changes which can't be automated.
At this time, the enhanced script which rely upon GUI Scripting if the text contain more than 65530 characters fits my needs. It proved to be able to treat a 500000 characters document.
I have an alternate scheme: extract the text apply the required changes write the new contents in a new text file using Write close the original document delete it rename the newly created document according to the original one open it in TextEdit.
As most of the time the edited text is smaller than the original one, I just think that writing the document upon itself is better than creating a new one — but maybe I'm wrong.
Last not least, on a French site (MacBidouille) I was said :
Alors déjà, non, là à mon avis, le seul bug, il est dans ton script, qui, sans la routine de protection des Apple events, créerait un dead lock en raison d'une référence circulaire.
Yvan KOENIG (VALLAURIS, France) mercredi 21 mai 2014 14:25:15
|