on run
run script main
end run
script main
if appActive("Mail") then
findFlagged()
end if
end script
on findFlagged()
-- careful ANSI esc may not show in some editors
set flaggedIcons to {{0, "[31;1m • [32;0m "}, {1, "[31;1m [32;0m "}, {2, "[33;1m • [32;0m "}, {3, "[32;1m • [32;0m "}, ¬
{4, "[34;1m • [32;0m "}, {5, "[35;1m • [32;0m "}, {6, "[30;1m • [32;0m "}}
# ASCII character is deprecated
# this character is defined by AppleScript as linefeed
# set newline to ASCII character 10
set finalText to "Flagged Mail:" & linefeed # newline
tell application "Mail"
set target_account to name of every account
repeat with theAccount in target_account
set theMessages to (every message in mailbox "INBOX" of the account theAccount whose flagged status is true)
repeat with thisMessage in theMessages
set theFlag to (flag index of thisMessage)
# these two properties are already strings, no need to coerce them again
set fromMsg to (sender of thisMessage) # as string
set subjMsg to (subject of thisMessage) # as string
set theID to (message id of thisMessage)
repeat with x from 1 to number of items in flaggedIcons
if theFlag = item 1 of item x of flaggedIcons then
set theIcon to item 2 of item x of flaggedIcons
end if
end repeat
set finalText to finalText & theIcon & word 1 of fromMsg & ": " & subjMsg & linefeed # newline
end repeat
end repeat
end tell
-- set fPath to "Macintosh HD:Users:username:Library:Logs:"
set fPath to "Macintosh HD:Users:username:Desktop:" --file testing
set theFile to POSIX path of fPath & "flagged1.log"
open for access theFile with write permission
set eof theFile to 0
write finalText to theFile as text
close access theFile
end findFlagged
on appActive(appName)
tell application "System Events" to (name of processes) contains appName
end appActive