creatiing new document in TextEdit and saving it to a folder

I'm just starting with applescript. I would like to write a script that will open an e-mail, copy the contents to a new TextEdit or TexShop file, and save the new document to a particular folder. Ultimately the name of the new document should depend on the content of the e-mail, but for now I'd be happy to just be able to create a new TextEdit document and save it in a specified location with a specified name.

I've tried a couple of things, including
tell application "TextEdit"
activate
make new document
set fileSpec to "Disk:Documents:Barbara:TestFile"
save document 1 in fileSpec
end tell
which creates a new untitled document and brings up a dialog, which isn't what I want. I also tried something that involved "with properties {name:TestFile} but that didn't work either.
I have Applescript the definitive guide and Applescript in a Nutshell but neither seems to describe this situation, although surely it's a common one.

I'd be most grateful for any help.



macbook Mac OS X (10.4.6)

Posted on Jul 3, 2006 5:39 AM

Reply
4 replies

Jul 3, 2006 9:22 AM in response to Ariadne

Hi Ariadne and welcome to Apple Discussions!

Your script works fine for me, so I wonder if you have the path set correctly.

Try it this way:

tell application "TextEdit"
activate
make new document
set fileSpec to (path to documents folder from user domain as string) & "Barbara"
save document 1 in fileSpec
close window 1 with saving
end tell


This will create a TextEdit file in your home Documents folder.

If you want a more fruitful environment than TextEdit for learning AppleScript, you might want to try Tex-Edit Plus. It's far more scriptable than TextEdit.

(Incidentally, if you just want to create text files and fill them with data, you don't need to use a separate application. You can use the file read/write commands in Standard Additions. See page 343 of AppleScript the Definitive Guide for a brief description, and post back here if you need more help.)

HTH

H

Jul 3, 2006 9:22 AM in response to Ariadne

Not all Macintosh e-Mail'er applications are scripted the same; thus, Apple's 'Mail' e-Mail'er application is assumed.

-----

-- Create dynamic path to the desired folder.
tell application "Finder" to set dFolder to ((startup disk) as string) & "Documents:Barbara:"

tell application "Mail" -- Utilize 'Mail's AppleScript dictionary.
set selected_Messages to selection -- Obtain a list of selected e-Mail messages.

repeat with i in selected_Messages -- Cycle through the selected e-Mail messages.
set tSubject to my Clean_String((subject of i) as string) -- Obtain each e-Mail message's 'Subject' line.
set tContent to (content of i) as string -- Obtain each e-Mail message's contents.

set fRef to open for access file (dFolder & tSubject) with write permission -- Create a new file at dFolder.
write tContent to fRef -- Write the e-Mail message to the new file.
close access fRef -- Close the new file.
end repeat
end tell

on Clean String(localString) -- 'Macintosh file names cannot contain a ':'. Here it is replaced by '_'.

if (local_String contains ":") then -- Check for presence of ':" in local_String
-- Standard 'AppleScript's text item delimiters' preparation.
set {oAStid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set local_String to text items of local_String -- Creates a list of items separated by ":'.
set AppleScript's text item delimiters to "_" -- Set a new delimiter character.
set local_String to local_String as string -- Creates a string from created list with '_' as the new delimiter.
set AppleScript's text item delimiters to oAStid -- Restore 'AppleScript's text item delimiters'.
end if

return local_String -- Return modified / non-modified string.
end Clean_String

-----

The created generic text file can be viewed and edited with any text editor.

Also, the 'repeat ... end repeat' portion can be rewritten as ...

-----

repeat with i in selected_Messages -- Cycle through the selected e-Mail messages.
set fRef to open for access file (dFolder & (my Clean_String((subject of i) as string))) with write permission -- Create a new file at dFolder.
write ((content of i) as string) to fRef -- Write the e-Mail message to the new file.
close access fRef -- Close the new file.
end repeatf

-----

Mac OS X (10.4.4)

Jul 3, 2006 9:31 AM in response to dev_sleidy

'end repeatf' is a typographical error. It should be 'end repeat'.

Thus, the optional code is:

repeat with i in selected_Messages -- Cycle through the selected e-Mail messages.
set fRef to open for access file (dFolder & (my Clean_String((subject of i) as string))) with write permission -- Create a new file at dFolder.
write ((content of i) as string) to fRef -- Write the e-Mail message to the new file.
close access fRef -- Close the new file.
end repeat

Mac OS X (10.4.4)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

creatiing new document in TextEdit and saving it to a folder

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.