Total beginner here - email contents of a textedit doc?

Hi there - I’m just starting to look into learning a little applescript because automator can’t seem to do what I want, which is eventually to email the contents of a text file to the same address on a schedule. I’ve got a smart folder with only one text document in it that changes, say, every week, so every week I would like to be able to paste the contents of that text document into an email and send it automatically. The bits I’m confused about are that I don’t know how to get the contents of a text document, and I don’t know how to define the content of a new mail message as what I’ve gotten from the previous step. (Basically I’m hopeless!) This is a lot to ask, but any tips on how to get started, or a script someone else has already written that could be altered, would be much appreciated!

Using what email client?

Hi Rose of Sharon

try the below script, it is set up to run with Apples Mail app, you will need to get the path to your text file, also you will need to add the email address of the person the mail is going to, you can save the script as a application run only and have say iCal fire it of once a week for you.

tell application “System Events” to tell process “Mail”
keystroke “n” using command down

---- Get text from your text file ----
set theFile to "Macintosh HD:Users:Budgie:Desktop:untitled folder:test.txt" as text --path to yor text file
open for access theFile
set fileContents to (read theFile)
close access theFile
tell application "Finder" to set fileName to name of alias theFile

---- Create your email ----
set theAddress to "email address here" --email address to whom mail is to be sent
set theSubject to "Scheduled Weeky Text     " & fileName --name of your text file
set TheBody to fileContents --contents of your text file
tell application "Mail"
	activate
	set newMessage to make new outgoing message with properties {subject:theSubject, content:TheBody}
	tell newMessage
		make new to recipient at end of to recipients with properties {address:theAddress}
		set visible to true -- edit out so you cant edit the message 
		--send newMessage  -- un edit so the email will send it's self without user interaction
	end tell
end tell

end tell

Budgie

Thanks so much budgie - this is so helpful. I’m having a little trouble though - I’ve set the path to the file but I’m getting the error message "system events got an error: can’t make “[item path]” into a type file. Then “read theFile” is highlighted.

sounds like the path to your text file isn’t quite right, cut and paste the below into script editor and save as a application, run only, then drag and drop your text file onto the application, the path to your text file will be copied to the clip board, paste the contents of the clipboard into the area: “PATH TO YOUR TEXT FILE” ie: set theFile to “PATH TO YOUR TEXT FILE” as text

on open _theseItems
set _location to _theseItems as text
set the clipboard to _location
display dialog "The path to the item has been copied " & return & ¬
“to the clipboard.” with icon note giving up after 5
end open

see how that goes…

I used the script you provided to make sure I had the path right. I know I do have it right because when I originally entered the path and did it incorrectly, I got a different error message (applescript couldn’t find the file). Unfortunately I’m still getting the error message “can’t make “[path to file]” into type file.” The document is just a plain .txt document…I tried directing it to a different text document and got the same thing. Sorry to be a bother! Don’t worry about it if you don’t know what’s going on and I appreciate the help!

hmm, I just got home and tried the script on a mac running 10.4.4, and I get the same result as you, the mac I built the script on is running 10.4.10 and works fine, im not sure what the correct syntax would be, sorry, ill do some nosing around, and see what I come up with, hopefully some one here will no the answer.

Hi, both.

I’m not totally sure if this is what’s causing the problem, but the ‘open for access’ and ‘read’, and ‘close access’ parameters in the script are strings. They should be file specifiers. That’s often caused problems in the past. Try using ‘file theFile’ or ‘alias theFile’. (More properly, if you use ‘open for access’, the parameters for the other two commands should be the reference number it returns. However, you don’t need to use ‘open for access’ for a single read of the file, so you can just use ‘read’ with a file specifier.)

set theFile to "Macintosh HD:Users:Budgie:Desktop:untitled folder:test.txt"
set fRef to (open for access file theFile)
set fileContents to (read fRef)
close access fRef

-- Or:
set theFile to "Macintosh HD:Users:Budgie:Desktop:untitled folder:test.txt"
set fileContents to (read file theFile)

Also, the System Events ‘tell’ block and the ‘keystroke’ command aren’t needed.

Fabulous! That change has it working now. Could someone just tell me how to set the from address to the same address each time I run the script? I have two accounts running in Mail. I’ve tried set theSender to “address@domain.com” but that doesn’t seem to work. Thank you both so much for the help!

EDIT: Never mind, I figured it out. Thanks so much! This will be an incredibly helpful script for me.

I just have one more question. If the text file in the folder changes from week to week, is there a way to handle that? Or to be more specific, is there a way to set theFile to the contents of the last folder in the path, or to set theFile to the file with the most recent created or modified date? Sorry to be asking so many questions – if there’s no easy way to do this, don’t worry about it.

Hi,

two options to read the file with the most recent modification date

with the Finder

set f to path to desktop as Unicode text
tell application "Finder" to set theFile to item 1 of (sort (get files of folder f) by modification date) as alias
set fileContents to read theFile

with a shell script

set f to path to desktop as Unicode text
set theFile to paragraph 1 of (do shell script "ls -tF " & quoted form of POSIX path of f & "| awk !/\\\\//")
set fileContents to read file (f & theFile)