-- MICROSOFT WORD
-- ====== SETTINGS ======
property targetRecipient : "someone@example.com"
property defaultSubject : "Table from Word"
property introText : "" -- e.g., "Hi — here's the table:"; leave "" for none
-- Optional: set to "" to use your default signature
property mailSignatureName : "" -- e.g., "My Signature"
-- ======================
on run {input, parameters}
-- 1) Copy current selection from Word (rich)
try
tell application "Microsoft Word"
if not (exists document 1) then error "No Word document is open."
set sel to selection
if sel is missing value then error "Select a table (or any content) in Word first."
copy object sel -- preserves table formatting
end tell
on error errMsg
display alert "Word copy failed" message errMsg
return input
end try
-- 2) Create a new email in Mail
try
tell application "Mail"
set newMsg to make new outgoing message with properties {subject:defaultSubject, visible:true}
tell newMsg
make new to recipient at end of to recipients with properties {address:targetRecipient}
if introText is not "" then set content to introText & return & return
if mailSignatureName is not "" then
try
set message signature to signature mailSignatureName
end try
end if
end tell
activate
end tell
on error errMsg
display alert "Mail compose failed" message errMsg
return input
end try
-- 3) Paste into the message body (robust focus targeting)
tell application "System Events"
tell process "Mail"
set frontmost to true
delay 0.7
set pasted to false
-- Most layouts: window -> splitter group -> scroll area -> text area
repeat with grp in {1, 2}
try
set theArea to text area 1 of scroll area 1 of splitter group grp of window 1
set focused of theArea to true
keystroke "v" using command down
set pasted to true
exit repeat
end try
end repeat
-- Fallback path without splitter group
if pasted is false then
try
set theArea to text area 1 of scroll area 1 of window 1
set focused of theArea to true
keystroke "v" using command down
set pasted to true
end try
end if
-- Last-ditch: tab through fields, then paste
if pasted is false then
repeat 6 times
keystroke tab
delay 0.1
end repeat
keystroke "v" using command down
end if
end tell
end tell
return input
end run