• 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: TextEdit oddity
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: TextEdit oddity


  • Subject: Re: TextEdit oddity
  • From: Shane Stanley <email@hidden>
  • Date: Wed, 21 May 2014 22:06:22 +1000

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.

-- 
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: TextEdit oddity
      • From: "koenig.yvan" <email@hidden>
References: 
 >Re: TextEdit oddity (From: Nigel Garvey <email@hidden>)

  • Prev by Date: Re: TextEdit oddity
  • Next by Date: Re: TextEdit oddity
  • Previous by thread: Re: TextEdit oddity
  • Next by thread: Re: TextEdit oddity
  • Index(es):
    • Date
    • Thread