Re: writing text to MS Word
Re: writing text to MS Word
- Subject: Re: writing text to MS Word
- From: Paul Berkowitz <email@hidden>
- Date: Wed, 01 Jun 2005 11:48:37 -0700
- Thread-topic: writing text to MS Word
But Word X does not require ASCII text. Word X uses the very same Mac
"International Text" that 'string' means to AppleScript, and that you get
when you coerce Unicode to string. There are thousands of characters you can
type in Word X that are not ASCII. That's no way to being Unicode (which is
what Word 2004 can do) - but anything that's returned by 'as string' from
Unicode in AppleScript will go into Word X. If it happens to contain a
genuine Unicode character that has no 'international text' equivalent or
pseudo-equivalent, you'd just see a rectangle in Word. That will never
happen with standard MacRoman characters, nor with Cyrillic nor about 15,000
Asian characters. If there's still a problem in Word X (as well there might
be - its AppleScript implementation is a disaster), it's got nothing to do
with this.
There _are_ places that do require ASCII, or at least MacRoman, plain text.
You can use Arthur Knapp's AsPlainText routine for those:
on AsPlainText(textual)
(* Where 'textual' values may include plain text,'international text',
styled text, Pascal string, C string, Unicode text, and «class utf8».
Unicode characters not having a single-byte "mapping" come back as a
question mark.
*)
set {text:plainText} to textual as text
return plainText
end AsPlainText
in lieu of writing to and reading from a text file, which could have its own
problems.
--
Paul Berkowitz
> From: Hudson Barton <email@hidden>
> Date: Wed, 1 Jun 2005 14:32:13 -0400
> To: <email@hidden>
> Subject: Re: writing text to MS Word
>
> I have no doubt you are correct in saying that the text result of a
> shell script is often a unicode string rather than an ascii string.
> Most text editors don't care what it is, but Word X does care. I am
> satisfied that my problem is solved as soon as I have a way to coerce
> unicode strings to ascii strings efficiently. Unfortunately, "set
> blah to result as string" does not coerce from unicode to ascii. The
> "as string" coercion works for lists, but it's not for unicode.
>
> I do have one way of coercing that's not too bad...if you have a
> sense of humor. I write the result of the shell script to a POSIX
> file and then read back the text. By "washing" it through the POSIX
> file, I get an ascii string.
>
> H.
>
>
> At 10:27 AM -0700 6/1/05, Paul Berkowitz wrote:
>> On 6/1/05 10:14 AM, "Andrew Oliver" <email@hidden> wrote:
>>
>>> On 6/1/05 10:04 AM, "Hudson Barton" <email@hidden> wrote:
>>>
>>>> I have some output from a shell script that is supposed to be regular
>>>> text and if I move it into most text editors, it looks, feels and
>>>> acts just like regular text. But apparently it's not. I need to
>>>> move the text into a window of Microsoft Word (version 10), and it's
>>>> producing errors like "Can't set every text of window 1 to ..."
>>>> What's going on here?
>>>>
>>>> If I convert each character first to an ascii number and then back to
>>>> an ascii character, then it works fine, but that's not practical.
>>>> Is there an easier way to coerce this string to a regular ascii
>>>> string?
>>>>
>>>> My script says:
>>>>
>>>> set str to result ---result of shell script
>>>> tell application "Microsoft Word" to set text of window 1 to str
>>>
>>> Despite what the dictionary says, 'do shell script' will return unicode
>>> text, not 'regular text' (whatever that may be).
>>>
>>> My guess is that you may need to coerce the shell script result to type
>>> 'string' before Word will take it, either:
>>>
>>> set str to (do shell script "blah" as string)
>>> tell application "Microsoft Word" to set text of window 1 to str
>>>
>>> or:
>>>
>>> set str to do shell script "blah"
>>> tell application "Microsoft Word" to set text of window 1 to (str as
>>> string)
>>>
>>
>>>
>> I would not count on being able to script Word X to do anything like this
>> reliably. Although its dictionary may claim to do things like this it
>> usually won't work. Or it will crash. You'd have to do it via 'do Visual
>> Basic', which is much more complicated. Word 2004 is another matter - it
>> will be scriptable, but not like that. Its object hierarchy is more
>> complicated (mirroring the 'do Visual Basic' method, but with the dictionary
>> and an AppleScript Reference available).
>>
>> Here's a reference to getting 'do Visual Basic' working for Word X:
>>
>> <http://www.word.mvps.org/MacWordNew/WordAppleScript.htm>
>>
>> In Safari, you have to refresh the page a few times to get it to show up.
>> You have to use the Visual Basic Editor Help (in Word) to learn the VBA code
>> needed.
>>
>> And in Word X, you do have to use string as Andrew says (although I'd expect
>> the Unicode to coerce to string OK, even implicitly). For Word 2004, Unicode
>> is in fact what's expected (although string works OK too).
>>
>>
>> --
>> 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
>
>
> _______________________________________________
> 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
>
_______________________________________________
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