Re: beginner question
Re: beginner question
- Subject: Re: beginner question
- From: Paul Berkowitz <email@hidden>
- Date: Tue, 31 Jan 2006 09:45:37 -0800
- Thread-topic: beginner question
On 1/31/06 8:11 AM, "R C Knight" <email@hidden> wrote:
>
> i'm pretty new to applescript, and i've got a bit stuck with one
> thing that i have been trying to make work.
>
> i want a script that can take the text from a paragraph in a textedit
> document and paste it into a specific row/column of a specific table
> in a word document.
>
> Is this possible? If so, i'm pretty stuck, can anybody help?
Not really a beginner question. Not many people have scripted Word 2004 yet,
which is pretty complicated and atypical. I really recommend getting the
Word (and Excel and PowerPoint) AppleScript Reference from
http://www.microfot.com/mac/ . Follow the links to
Resources/Developer/AppleScript. (If you are using an earlier version of
Word, such as Word X, you won't be able to do this at all except by using
Visual Basic via the 'do Visual Basic' command. It will be similar to what I
describe below, since Word 2004's scripting uses the same object model as
Word VBA, but you'll have to learn some VBA, and it can get a bit contorted.
I recommend that you instead upgrade to Word 2004 if you plan to do much
scripting of Word, so you can do it in more or less proper AppleScript.)
Well, you're not really asking to _paste_ after inserting your cursor in the
correct cell of the table in Word, are you? That would be quite easy.
Assuming you have already selected and copied some text in TextEdit, this
will do it:
tell application "Microsoft Word"
--activate
paste object selection
end tell
(Remove the -- before 'activate' if you want Word to come to the front.)
'paste object' is the command for pasting, and 'selection' tells it to do
the pasting wherever who have selected text or inserted the cursor -
anywhere - including somewhere in a table. 'paste object' command in the
Microsoft Word Suite gives the direct object to be of 'selection-object'
type, and the 'application' class, like most applications, has a 'selection'
property of that 'selection-object' type, so that's all you need. (Word,
unusually, can have multiple selections in multiple documents all at the
same time, but the application's selection will be whatever is selected in
the front, active document. If you have nothing selected but the cursor is
inserted anywhere, including in a table cell, that's where the paste will be
inserted.)
If you want to insert the clipboard text at a specific cell in a table
without first inserting your cursor there, that's a little trickier. 'paste
object' is also a command in the Text Suite, but here the direct parameter
is of 'text range' type. That allows you to specify any text range,
including the text object of a specific table cell. So you need to traverse
the object model for 'table'. I'm going to assume that you're dealing with
the only, or first, table in your front document, so it will be table 1 of
active document. Otherwise you'd have to specify which table first of all.
If you check the dictionary's Table Suite and the Reference for 'table',
you'll see that 'cell' is an element of 'row', but not of 'table' or of
'column'. So you need to specify the cell you want within the row you want.
Let's assume you want to paste into the 4th cell of the 2nd row. That's the
right way to locate it - in 'row' class, 'cell' can be specified by index
(e.g. 4), or as relative to another cell (e.g. 'after' o 'before') - not
much use here, or by a 'whose' filter - which could be useful if you wanted
to locate the first cell with no text, for example. 'row' is an element of
table, and can be found by name (if you'd ever given the row a name ;-),
index, or whose filter. So index again (e.g. 2) is the usual way. The cell
we want is:
cell 2 of row 1 of table 1 of active document
('active document' is a property of 'application' and refers to the document
in the front.) It might be neater to "tell" everything to table 1 of active
document if we're going to be doing a lot of operations in the table instead
of dragging 'of table 1 of active document' around with us everywhere.
Checking 'cell' in the dictionary, we can see that it has a single 'text
object' property of type 'text range': so that could be the text range we're
looking for, where 'paste object' can operate.
Try it. Select some text in TextEdit, then
tell application "Microsoft Word"
--activate
set theCell to cell 4 of row 2 of table 1 of active document
paste object (text object of theCell)
end tell
It works. I set a variable 'theCell' to simplify the script a bit, but you
could do it all in one long line without variables if you prefer. Or direct
everything to the table if you're going to pick other cells later.
And Word gets the most complex type from the clipboard that it can. So if
you pasted formatted text from an .rtf document in TextEdit (e.g. the text
you selected was blue italics Lucinda Grande size 12) it will paste using
the same formatting. If you want instead to paste just the text content but
use the text formatting you've previously set for the table, the simple way
should be to use 'paste special' with 'paste text' as the 'data type'
parameter:
paste special (text object of theCell) data type paste text
But it still seems to be pasting formatted text in this situation. I'll
report that as a bug. You can still do it, but it's more complicated. One
way would be to get the 'font object' of the cell, or table, before you do
any pasting, get all the properties of that font object (name, color, size,
italics, etc. - there are about 30 of them) and then reset all those
properties after you paste. A simpler way would be to get just the text, not
the formatting, from the selection in TextEdit, and just set that text
content in Word. The trouble is that TextEdit doesn't have 'selection'
(which is rather incredible), you can only get the text of the entire
document, then specify paragraphs by number, etc. So it has to be done in
Word. If you need to restore formatting in the table, I'll see if maybe
there's some easier way than resetting all the font properties, or can show
you how to do that if you need it.
--
Paul Berkowitz
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden