G’day Shane, and thank you for your reply.
I think I’ve managed to add code that will color the main body text a different color foe each report. The color I was sending was for the text, the bullets just use a red.
However, I cannot work out how to address the Emails contents. As you can see from the attached code, I need to set the font color and size for different parts of the content, and also add up to three attachments. I’ve given up experimenting, and cannot find anything on the web, so I’m asking, once again, for advice please.
on eMailit(the_subject, the_content, theCCRecipientsStore, textcolor)
if (count of theCCRecipientsStore) = 0 then
my sayTheText("There are no Email recipients set")
if ("Hourly" is in my itemDetails) then
my sayTheText(" for hourly reports.")
else
my sayTheText(" for main reports.")
end if
return
end if
set item1Color to (item 1 of textcolor) / (256 * 256)
set item2Color to (item 2 of textcolor) / (256 * 256)
set item3Color to (item 3 of textcolor) / (256 * 256)
set theSendCompressedFile to 0
repeat with x from 1 to 8
set the_content to the_content & return
end repeat
# Set up Numbers Chart to be saved to desktop
try
set p to 7
if (my numbersGraphPrint) then
# set (my numbersGraphPrint) to false
set p to 8
my setUpTallyNumbersChartCreator() # Sets up & copies Chart
end if
end try
try
repeat
set p to 1
set theSendCompressedFile to theSendCompressedFile + 1
if theSendCompressedFile ≥ 3 then exit repeat
tell application "Finder"
try
set pathToMe to path to current application as text
set logoPath to pathToMe & "Contents:Resources:Report Logo.png" as text
set PosixLogoPath to POSIX path of logoPath
on error
set logoPath to ""
end try
set p to 2
if ((count of logoPath) as text) < 4 or not (exists file logoPath) then
try
set pathToMe to (path to applications folder) & "Mail Manager:Mail Manager.app:" as text
set logoPath to pathToMe & "Contents:Resources:Report Logo.png" as text
set PosixLogoPath to POSIX path of logoPath
end try
end if
end tell
set p to 3
if the_subject contains "Hourly" then set theCCRecipients to theCCRecipientsStore
if the_subject contains "Hourly" and theSendCompressedFile = 2 then exit repeat
set newListTrue to {}
set newListFalse to {}
set newListTotal to {}
if the_subject contains "Annual Report" or the_subject contains "Monthly Report" or the_subject contains "Weekly Report" or the_subject contains "Daily Report" then
repeat with eachAddress in theCCRecipientsStore
set end of newListTotal to item 2 of eachAddress as text
if (item 1 of eachAddress as text) is "true" then
set end of newListTrue to item 2 of eachAddress as text
else
set end of newListFalse to item 2 of eachAddress as text
end if
end repeat
if theSendCompressedFile = 1 then copy newListFalse to theCCRecipients
if newListFalse = {} then set theSendCompressedFile to 2
if theSendCompressedFile = 2 then
copy newListTrue to theCCRecipients
if newListTrue = {} then exit repeat
end if
# This will happen on first pass, if NewListFalse > {}
if newListTrue = {} then copy newListTotal to theCCRecipients
end if
try
set mailRecipients to ""
repeat with themailitem in theCCRecipients
set p to 20.5
set mailRecipients to mailRecipients & themailitem & ","
end repeat
set mailRecipients to characters 1 through -2 of mailRecipients as text
end try
try
-- 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 theMainTextColor to (current application's NSColor's colorWithDeviceRed:item1Color green:item2Color blue:item3Color alpha:1.0)
set theBulletColor to (current application's NSColor's colorWithDeviceRed:0.859710693359 green:0.037261962891 blue:0.009262084961 alpha:1.0)
set theFont to (current application's NSFont's fontWithName:"Helvetica" |size|:10.0)
# Will this set color of all the text?
{anNSMutableAttributedString's addAttribute:(current application's NSForegroundColorAttributeName) value:theMainTextColor range:{0, theFullLength}}
-- 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:theBulletColor 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:{mailRecipients}
mailShare's setSubject:the_subject
mailShare's performWithItems:{anNSMutableAttributedString}
tell application "Mail"
activate
set newMessage to outgoing message
set p to 4
# Crashes here
tell content of newMessage
try
set p to 5
set x to offset of my theBusinessName in the_content
set p to 5.1
set font of characters x thru (x + (count of my theBusinessName) - 1) to "Helvetica Bold"
set color of characters x thru (x + (count of my theBusinessName) - 1) to {56342, 2442, 607}
end try
try
set p to 6
set x to offset of (my hourCutOff & " Report" as rich text) in the_content
if x ≠ 0 then
set p to 6.1
set xx to count of ((my hourCutOff) & " Report" as rich text)
try
set font of characters x thru (x + xx - 1) to "Cochin"
end try
set size of characters x thru (x + xx - 1) to 20
set color of characters x thru (x + xx - 1) to {43 * 256, 0, 256 * 256 - 1}
end if
end try
try
set p to 7
set x to offset of "Daily Report" as rich text in the_content
if x ≠ 0 then
set p to 7.1
set xx to offset of "Total Mail items processed :" in the_content
try
set font of characters x thru (xx - 3) to "Cochin"
end try
set size of characters x thru (xx - 3) to 20
set color of characters x thru (xx - 3) to {43 * 256, 0, 256 * 256 - 1}
end if
end try
try
set p to 8
set x to offset of "Weekly" as rich text in the_content
if x ≠ 0 then
set p to 8.1
set xx to offset of "For the" in the_content
try
set font of characters x thru (xx - 3) to "Cochin"
end try
set size of characters x thru (xx - 3) to 20
set color of characters x thru (xx - 3) to {43 * 256, 0, 256 * 256 - 1}
end if
end try
try
set p to 9
set x to offset of "Monthly" as rich text in the_content
if x ≠ 0 then
set p to 9.1
set xx to offset of "For the" in the_content
try
set font of characters x thru (xx - 3) to "Cochin"
end try
set size of characters x thru (xx - 3) to 20
set color of characters x thru (xx - 3) to {43 * 256, 0, 256 * 256 - 1}
end if
end try
try
set p to 10
set x to offset of "Daily Average" as rich text in the_content
set p to 10.1
if x ≠ 0 then
set xx to offset of "For the" in the_content
try
set font of characters x thru (xx - 3) to "Cochin"
end try
set size of characters x thru (xx - 3) to 20
set color of characters x thru (xx - 3) to {43 * 256, 0, 256 * 256 - 1}
end if
end try
try
set p to 11
if "Annual" is in the_content and (("(Daily Tallies)" is in the_content or "(Monthly Tallies)" is in the_content)) and ("Monthly Report" is not in the_content and "Daily Report" is not in the_content) then
set p to 11.1
set x to offset of "Annual" in the_content
set y to offset of ")" in the_content
set font of characters x thru y to "Cochin"
set size of characters x thru y to 20
set color of characters x thru y to {181 * 256, 5 * 256, 143 * 256}
end if
end try
try
set p to 12
set PosixFilePath to ""
set PosixjpgFilePath to ""
set p to 12.1
if (my numbersGraphPrint) and ("Hourly" is not in my itemDetails) then
if (my numbersGraphIncludeCompressedFile) then
set p to 11
set PosixFilePath to POSIX path of my zipItFull(my saveTheFilePath)
end if
end if
end try
try
tell application "Finder"
set jpgFilePath to ((path to desktop) & "Numbers_Chart.jpg" as text)
set PosixjpgFilePath to POSIX path of (jpgFilePath as text)
end tell
end try
end tell # content
set p to 20
# Insert Attachments
tell newMessage
try
set p to 20.6
make new attachment with properties {file name:PosixLogoPath as POSIX file} at before first paragraph
on error errmsg number errnum
tell application "System Events" to display dialog "eMailIt setting Logo error " & errmsg & " number " & errnum
end try
set c to count of paragraphs
set p to 20.8
if PosixjpgFilePath ≠ "" and ((my displayHourlyJobsFlag) or ("Hourly" is in my itemDetails)) then
try
set p to 20.9
make new attachment with properties {file name:PosixjpgFilePath as POSIX file} at before paragraph (c - 4)
on error errmsg number errnum
tell application "System Events" to display dialog "eMailIt setting jpg Chart error " & errmsg & " number " & errnum
end try
end if
set p to 21
if PosixFilePath ≠ "" and (my displayHourlyJobsFlag) and theSendCompressedFile = 2 then
try
make new attachment with properties {file name:PosixFilePath as POSIX file} at after last paragraph
on error errmsg number errnum
tell application "System Events" to display dialog "eMailIt setting compressed File error " & errmsg & " number " & errnum
end try
set p to 22
end if
end tell # new message
end tell # Mail
set p to 23
tell application "Mail"
tell newMessage
send
tell current application to delay 0.2
try
close saving no
end try
end tell
end tell
on error errmsg number errnum
tell application "System Events" to display dialog "Second Main eMailIt error " & errmsg & " number " & errnum & " p = " & p
end try
end repeat
on error errmsg number errnum
tell application "System Events" to display dialog "First Main eMailIt error " & errmsg & " number " & errnum & " p = " & p
end try
try
tell application "Finder"
try
move file ((my saveTheFilePath) & ".zip" as text) to trash
end try
try
close every Finder window
end try
end tell
try
tell application "Numbers" to quit saving no
tell current application to delay 0.5
end try
set p to 23
try
tell application "Finder" to move file ((path to desktop) & "Hourly Tally" & ".numbers" as text) to trash
end try
on error errmsg number errnum
tell application "System Events" to display dialog "Last Main eMailIt error " & errmsg & " number " & errnum & " p = " & p
end try
end eMailit