Using Automator/Apple Script to send a table from Excel via email daily
I am trying to write an apple script to be run in Automator (which I then put in Calendar) to send a table from an Excel sheet each day via email
I have tried the screen grab route but this is not very consistent. This code used to work but now I get Parameter errors
- Define the file path where the screenshot will be saved
set desktopPath to path to desktop as text
set filePath to desktopPath & "screenshot.png"
-- Define the coordinates of the target area
set startX to 100
set startY to 325
set width to 550
set height to 225
tell application "Microsoft Excel"
activate
try
-- Get a reference to the workbook if it's open, or open it if not
set targetWorkbook to open workbook workbook file name "portfoliomaster.xlsm"
activate workbook "port.xlsm"
set targetSheet to sheet "Print" of targetWorkbook
select targetSheet
-- Maximize Excel window to cover the full screen
set zoom of window 1 of targetWorkbook to 2
on error errText
display dialog "Error: " & errText buttons {"OK"} default button "OK" with icon 0
end try
end tell
-- Delay for a moment to ensure the sheet is fully maximized
delay 1
-- Use System Events to capture the screenshot
do shell script "/usr/sbin/screencapture -R " & startX & "," & startY & "," & (startX + width) & "," & (startY + height) & " " & quoted form of POSIX path of filePath
-- Copy the screenshot to the clipboard
do shell script "echo " & quoted form of POSIX path of filePath & " | pbcopy"
-- Get today's date in the format "yyyy-MM-dd"
set today to current date
set dateString to (year of today as text) & "-" & text -2 thru -1 of ("0" & ((month of today) as integer)) & "-" & text -2 thru -1 of ("0" & (day of today))
-- Create a new email in Apple Mail with the title including today's date
tell application "Mail"
activate
set theSubject to "Daily with " & dateString
set newMessage to make new outgoing message with properties {subject:theSubject, visible:true}
set recipientAddress to "abc@gmail.com" --
make new to recipient at newMessage with properties {address:recipientAddress}
tell content of newMessage
make new attachment with properties {file name:filePath as alias} at after last paragraph
end tell
send newMessage -- Send the email
end tell
end run
MacBook Pro 13″, macOS 13.1