Re: Scripting TextEdit
Re: Scripting TextEdit
- Subject: Re: Scripting TextEdit
- From: kai <email@hidden>
- Date: Sun, 7 May 2006 10:42:14 +0100
On 6 May 2006, at 01:39, Nelson Byrne wrote:
What's wrong with this?
tell application "TextEdit"
--TextEdit has two elements of its own: documents and windows. No
properties.
get front document returning theDoc
tell theDoc
--a TextEdit Suite document has a text property, part of the Text
Suite
get text of theDoc returning theText -->"Recitative"
--Text Suite texts have a font property
font of theText -->"Can't get font of \"Recitative\"."
end tell
end tell
I realise that you've already had a couple of good replies to this,
Nelson - but perhaps I could add my 2¢...
This probably just boils down to a bit of confusion between
references and the effect of evaluation. A TextEdit document's text
elements (attribute runs, characters, paragraphs and words) and text
properties (size, color, font and class) can be extracted by using a
reference. For example:
----------------
tell application "TextEdit" to font of text of front document
--> "Helvetica" (* or whatever *)
----------------
The same elements and properties can also be obtained directly from
the document itself (although the class property will obviously
differ). So this shorter syntax works equally well:
----------------
tell application "TextEdit" to font of front document
--> "Helvetica" (* or whatever *)
----------------
It therefore follows that the value of selected elements/properties
from a document's text could be assigned to different variables with
something like this:
----------------
tell application "TextEdit" to front document returning ¬
{word:theWords, text:theText, size:theSize} (* etc. *)
----------------
However, as you've discovered, neither of the following forms
(applied either individually or in combination) will work:
----------------
tell application "TextEdit" to font of (get text of front document)
----------------
tell application "TextEdit"
set theText to text of front document
(* or: text of front document returning theText *)
font of theText
end tell
----------------
--> (in each case:) error number -1728 [errAENoSuchObject]: Can't get
font of "some text".
----------------
The reason for the error is that the 'get' and 'set'/'returning'
commands evaluate the target object. In other words, rather than a
reference to the text of the document (from which text elements and
properties could be obtained) - they return the value of that text
(which is of class Unicode text). Any subsequent attempt to evaluate
an application-specific element or property from this value will
therefore fail.
I had hoped to change the size of certain characters in a document,
but I didn't get far. Can someone help please?
This little demo might help:
----------------
tell application "TextEdit"
activate (* just to show the results *)
tell (make new document with properties {text:"Mmm… Doughnuts!"})
tell (first word where it is "Doughnuts") to repeat 20 times
set size to size + 2
end repeat
set size of last character to size of last word
end tell
end tell
----------------
---
kai
_______________________________________________
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