Copy .RTF file contents to a new .RFT file

I’m a 60+ beginner with AppleScript so please be gentle with me.

I’m trying to understand how to open an .rtf file created in TextEdit and copy its contents to the clipboard and then open a different TextEdit file and paste the clipboard contents of the first into the second and retail the formatting.

I can manage the file manipulation but can’t get my head around the cut’n’paste operation.

I would be very grateful for any suggestions.

bill in ch

Model: iMac
Browser: Internet Explorer 6.0
Operating System: Mac OS X (10.4)

Hi, Bill.

Do you want to paste the contents of the first file somewhere into another document that already contains text, or do you simply want to create another copy or version of the first file?

To make your life easier, may be it’s worth to take a look over here: http://tex-edit.com/
It’s a simple tex-editor with dozens of samplescripts on the site.

To duplicate the document for formatting just use this:

tell application "Tex-Edit Plus"

	if not (window 1 exists) then
		beep
		return
	end if
	
	activate
	duplicate window 1 to front
	
	--To copy text you can also use a line like this:
	--set x to contents of window 1
	
end tell

Good luck!

I’d like to paste the contents fo the first file into another document that already contains text and also has a different name.

bill in ch

. Well. TextEdit isn’t very scriptable in that regard. I can’t think of a way to insert text at a particular point within a document, but it’s possible to insert text at the end, which may be what you want. I say “insert text at the end”, but in fact the method below only allows text to be inserted at the beginning of a document. So instead of inserting the text from document 1 at the end of document 2, the text from document 2 is inserted at the beginning of document 1. Document 1 is then saved to document 2’s file. Still with me? :rolleyes:

Edit: I’ve just discovered how to move the insertion cursor to the end of a document, so the sensible method is possible after all. I’ve edited the script below and it now appends document 1’s text to the bottom of document 2. :slight_smile:

It’s possible to do the task by scripting the commands in TextEdit’s AppleScript dictionary, but it’s v-e-r-y slow. I’ve used GUI Scripting here, which effectually works TextEdit’s user interface controls. GUI Scripting has to be enabled on your machine, either by using “AppleScript Utility.app” in the AppleScript folder in your Applications folder, or by checking “Enable access for assistive devices” in the “Universal Access” pane in “System Preferences”.

You say you’re OK with the file work, so hopefully you’ll be able to adapt the following:

-- This opens two RTF files that currently happen to be on my own desktop.
set doc1 to "Bless.rtf" -- The name of the file to be appended.
set doc2 to "Scripts2Text Read me.rtf" -- The name of the file to be appended to.
tell application "TextEdit"
	open file ((path to desktop as Unicode text) & doc1)
	open file ((path to desktop as Unicode text) & doc2)
end tell

-- Use GUI Scripting. (Must be enabled.)
tell application "System Events"
	tell application process "TextEdit"
		-- TextEdit must be frontmost to use the clipboard.
		set frontmost to true
		
		set windowMenu to menu 1 of menu bar item "Window" of menu bar 1
		-- Bring the document to be "appended" to the front.
		click menu item doc1 of windowMenu
		-- Type Command-a and Command-c to select and copy its entire text.
		keystroke "ac" using command down
		-- Bring the document to be "appended to" to the front.
		click menu item doc2 of windowMenu
		-- Move the cursor to the end of it.
		keystroke (ASCII character 31) using command down
		-- Insert a couple of returns to separate the entries.
		keystroke return
		keystroke return
		-- Type Command-v and Command-s to paste the copied text and to save the document.
		keystroke "vs" using command down
	end tell
end tell

tell application "TextEdit" to quit saving no

Here’s another approach that doesn’t require GUI Scripting and is about twice as fast: editing the RTF code directly. It reads the RTF text directly from the document 1 file, cuts out the page format line (so that it doesn’t affect the format of document 2), prepends a couple of RTF-format returns, and splices the result onto the end of the document 2 file. It then opens document 2 in TextEdit and resaves it from there, which causes Text Edit to combine the two RTF headers into one. :slight_smile:

-- Paths to two RTF files that currently happen to be on my desktop.
set doc1 to ((path to desktop as Unicode text) & "Bless.rtf")
set doc2 to ((path to desktop as Unicode text) & "Scripts2Text Read me.rtf")

-- Read the RTF text to be appended directly from the file.
set pasteText to (read file doc1 from 1)

-- Cut anything up to and including the first "{".
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "{"
set pasteText to text from text item 2 to -1 of pasteText

-- Cut the page format line and prepend two RTF returns.
set AppleScript's text item delimiters to "\\marg"
set part1 to text item 1 of pasteText
set part2 to text from text item 2 to -1 of pasteText
set AppleScript's text item delimiters to astid
set RTFreturn to "\\" & return
set pasteText to RTFreturn & RTFreturn & part1 & text from paragraph 2 to -1 of part2

-- Open the other document for write access.
set doc2access to (open for access file doc2 with write permission)
try
	-- Locate the final "}" in the file.
	set z to -1
	repeat until ((read doc2access from z for 1) is "}")
		set z to z - 1
	end repeat
	-- Overwrite the "}" with the edited paste text.
	write pasteText to doc2access starting at z
on error msg
	display dialog msg buttons {"OK"} default button 1 with icon stop
end try
close access doc2access

-- Open the doctored file in TextEdit and resave it to optimise the RTF code.
tell application "TextEdit"
	open {alias doc2}
	save front document
	close front document
end tell