r/applescript • • Oct 16 '23

Script to send automated email - running in the background?

before starting, just want to point out that I'm still very new to this;

I have this very repetitive task to send a project update to people. The script itself is very manual in that it opens chrome, loads my work gmail, uses gmail shortcuts to create a new email, then opens a new tab to get info from a gsheet to copy it back in the mail and press send.

tell application "Google Chrome"
	activate
	open location "https://mail.google.com" -- Opens Gmail
end tell

delay 15 -- Adjust the delay to allow Gmail to load

tell application "System Events"
	set myContent to "mycontenttext" & linefeed & linefeed
	set currentDate to current date
	set mySubject to "mysubjecttext " & (short date string of currentDate)
	set myToAddresses to "addresses here"
	keystroke "c" -- Compose new email (Cmd+C)
	delay 1
	set the clipboard to myToAddresses
	keystroke "v" using {command down}
	delay 1
	keystroke tab
	delay 1
	set the clipboard to mySubject
	keystroke "v" using {command down}
	delay 1
	keystroke tab
	delay 1
	set the clipboard to myContent
	keystroke "v" using {command down}
	delay 1
end tell

tell application "Google Chrome"
	activate
	open location "gsheet url" 
end tell

delay 5 -- Adjust the delay to allow the Google Sheet to load

tell application "System Events"
	keystroke "a" using {command down} -- Select all (Cmd+A)
	delay 1
	keystroke "c" using {command down} -- Copy (Cmd+C)
	delay 1
	keystroke tab using {control down, shift down}
	delay 1
	keystroke "v" using {command down}
	delay 1
	key code 125 -- 125 is the key code for the down arrow key
	set mySubject2 to linefeed & "some more text " & linefeed & linefeed & "Best Regards;"
	set the clipboard to mySubject2
	keystroke "v" using {command down}
	delay 3
	keystroke return using {command down}
end tell

Is there a way I can get this run in the background?

4 Upvotes

3 comments sorted by

1

u/tas509 Feb 27 '24

Hi,

total respect for the fact that you've managed to make applescript do what you have.

But you know, if your data is in a Google Spreadsheet then you can use AppsScript (Google's Javascript based scripting language) to do this and it will be a LOT easier and more sensible.

Here's a tutorial to get you started....

https://developers.google.com/apps-script/samples/automations/mail-merge

You can even then make a Trigger and have it fire off once a week / month automatically or whatever...

Regards

Tom

1

u/electric-sheep Feb 28 '24

I couldn't go down that route as there's a corporate policy to disallow scripting unfortunately, so I had to run it locally.

1

u/everythingabili Feb 28 '24

Ah.

But AppleScript is scripting :-)

You could also do it with Python locally.

https://realpython.com/python-send-email/

If you're dead set on AppleScript, rather than trying to drive a browser, using Mail.app might be a better approach.

https://discussions.apple.com/thread/252855659?sortBy=best

Good luck.