• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: path to as string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: path to as string


  • Subject: Re: path to as string
  • From: Christopher Nebel <email@hidden>
  • Date: Fri, 1 Jul 2005 01:37:18 -0700

On Jun 29, 2005, at 10:08 AM, Chaim Kram wrote:

I'm trying to extract plain text (no style information) out of the clipboard. What I've been able to glean is that there are at least 3 disparate ways to do this, none of which are intuitive or discoverable in the usual AppleScript ways:

 (1)  set x to the string of (the clipboard as record)
 (2)  set x to (the clipboard as text) as Unicode text
 (3)  set x to «class ktxt» of ((the clipboard as text) as record)

Several variations of this also do not work, though it seems that they ought to:

 (1a)  set x to the Unicode text of (the clipboard as record)
 (2a)  set x to the clipboard as Unicode text
 (3a)  set x to «class ktxt» of (the clipboard as record)


Assuming the clipboard contains only styled text of the English variety (meaning I'm in a text editor like Smile, and I select styled text and choose "Copy"), and I'm running under 10.3.9 and not yet Tiger:


 (1a) returns an error because there is no "Unicode text" property
       in the record, only "string" (and it's supposedly deprecated?)

Yes, but if that's all you've got, then that's all you've got.

(2a) returns Chinese characters;

That's a bug; it's fixed in Tiger.

 (3a) returns an error because there is no «class ktxt» when the
       clipboard is coerced to a record without first coercing it
       to text  (and why is that?  why does the "string" object get
       changed to a «class ktxt» object when the clipboard is coerced
       to text?)

Essentially, because you're looking at two different data storage formats -- the clipboard on one hand, and Apple event descriptors on the other -- and while they correspond, they aren't the same. They both have the same two pieces, text and styles, but store them under different keys. (Gory details available on request.)


The "correct" thing to say depends on what you mean by "plain text". If you're trying to get highest-possible-fidelity unstyled text you can -- in other words, Unicode text -- then none of (1), (2), or (3) is correct, because they all transform through "string" first, which will destroy non-primary characters. (That is, characters that don't exist in the primary system encoding, such as Japanese on an English- primary system.) Ideally, you'd be able to say "the clipboard as Unicode text", but that doesn't work prior to Tiger. However, this will work:

    try
        set x to Unicode text of (the clipboard as record)
    on error
        set x to (the clipboard as string) as Unicode text
    end

If you're trying to get an old-school primary-encoding string (that is, something that will get sent as typeText and therefore work with the crufty old Unicode-ignorant application you have to use), then (3) is the most reliable way. Throwing away the style information will give you mangled text if the styles indicated non-primary encodings, but since the whole point is to transform into typeText, there's no way around that. _______________________________________________
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
  • Next by Date: Ever wanted to close a tab in Safari by Applescript? Here's how.. sometimes
  • Next by thread: Re: path to as string
  • Index(es):
    • Date
    • Thread