• 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: Can I speed up Mail content setting?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can I speed up Mail content setting?


  • Subject: Re: Can I speed up Mail content setting?
  • From: Shane Stanley <email@hidden>
  • Date: Thu, 28 May 2015 18:17:06 +1000

On 28 May 2015, at 3:52 pm, Brian Christmas <email@hidden> wrote:

Any one know of a way to speed this up? For instance, is it possible to format the data before adding it to Mail as content?

It should be. There's a bit of a trick to it: instead of creating the message via AppleScript the normal way, you use a class called NSSharingService. This is the class behind all those popup menus that offer to send stuff as emails, messages, tweets etc. So it's going to leave you with an open outgoing message; I presume you can then use GUI scripting to then send it.

Anyway, here's a version using your code to generate the original string, then ASObjC to turn it into an attributed string and add it to an email. On my Mac, it takes nearly a second for the email window to open with your sample, but a fair slice of that time is the animation.

use scripting additions
use framework "Foundation"
use framework "AppKit"

-- build Brian's string
set the_content to ""
set c to current date
copy c to cc
set year of cc to (year of cc) - 1
repeat with x from 1 to 355
set the_content to the_content & (year of cc & " " & month of cc as text) & " " & day of cc & tab & "●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆" & return as text
set cc to cc + days
end repeat
-- make string and attributed string
set anNSString to current application's NSString's stringWithString:the_content
set anNSMutableAttributedString to current application's NSMutableAttributedString's alloc()'s initWithString:anNSString attributes:(missing value)
-- get length of string
set theFullLength to anNSString's |length|()
-- get color and font; edit to taste
set theColor to (current application's NSColor's colorWithDeviceRed:0.9 green:0.05 blue:0.05 alpha:1.0)
set theFont to (current application's NSFont's fontWithName:"Helvetica" |size|:10.0)
-- do regex search for runs of bullets
set bulletRegex to current application's NSRegularExpression's regularExpressionWithPattern:"●+" options:0 |error|:(missing value)
set theMatches to (bulletRegex's matchesInString:anNSString options:0 range:{0, theFullLength}) as list
-- start editing the attributed string
anNSMutableAttributedString's beginEditing()
-- make change to bullets
repeat with i from 1 to count of theMatches
set theRange to (item i of theMatches)'s range()
(anNSMutableAttributedString's addAttribute:(current application's NSForegroundColorAttributeName) value:theColor range:theRange)
end repeat
-- do regex search for runs of diamonds
set diamondRegex to current application's NSRegularExpression's regularExpressionWithPattern:"◆+" options:0 |error|:(missing value)
set theMatches to (diamondRegex's matchesInString:anNSString options:0 range:{0, theFullLength}) as list
-- make change to diamonds
repeat with i from 1 to count of theMatches
set theRange to (item i of theMatches)'s range()
(anNSMutableAttributedString's addAttribute:(current application's NSFontAttributeName) value:theFont range:theRange)
end repeat
-- stop editing the attributed string
anNSMutableAttributedString's endEditing()
-- use NSSharingService to build the email; this is what all popup menus around the place use
set mailShare to current application's NSSharingService's sharingServiceNamed:(current application's NSSharingServiceNameComposeEmail)
mailShare's setRecipients:{"email@hidden"}
mailShare's setSubject:"I wish"
mailShare's performWithItems:{anNSMutableAttributedString}


-- 
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: Can I speed up Mail content setting?
      • From: Brian Christmas <email@hidden>
References: 
 >Can I speed up Mail content setting? (From: Brian Christmas <email@hidden>)

  • Prev by Date: Can I speed up Mail content setting?
  • Next by Date: clear all overrides in a document, including tables?
  • Previous by thread: Can I speed up Mail content setting?
  • Next by thread: Re: Can I speed up Mail content setting?
  • Index(es):
    • Date
    • Thread