Re: Keychain and text returned
Re: Keychain and text returned
- Subject: Re: Keychain and text returned
- From: kai <email@hidden>
- Date: Wed, 30 Mar 2005 19:56:05 +0100
On Wed, 30 Mar 2005 21:37:37 +1000, John Cochrane wrote:
Many thanks Neil and Kai
the shortest solution is Kai's:
set {text:keyName} to text returned of (display dialog "What is the
name of the key?" default answer "server" buttons {"OK"}) as text
tell application "Keychain Scripting"
tell the current keychain
set theKey to (key 1 whose name is keyName)
end tell
end tell
I had previously unsuccessfully tried the first line as:
set keyName to the text returned of (display dialog "What is the name
of the key?" default answer "server" buttons {"OK"}) as text
which as Neil explained will still fail.
I'm still don't understand why simply coercing to text does fail but I
will seek enlightenment from the ASS archives as Neil suggested.
Basically, if the original text is encoded in some way, the result of a
regular 'to text' coercion will still retain some encoding - in spite
of its 'string' class (which isn't necessarily synonymous with 'plain
text'). So the result is really styled text.
Styled text, which contains both text and style information, can be
coerced to a record - while plain text can't. The result of such a
coercion would look something like:
{«class ktxt»:"Hello world", «class ksty»:«data
styl0001000000000010000E00030000000C000000000000»}
The following script demonstrates the differences between various types
of text (as well showing about the only method of accurately testing
for plain text):
--------
on isPlainText(t)
try
t as record
false
on error number -1700 (* Apple event manager: coercion failure *)
true
end try
end isPlainText
set pTxt to "t"
set iTxt to pTxt as international text
set uTxt to pTxt as Unicode text
set ciTxt to iTxt as text
set cuTxt to uTxt as text
{pTxt:{class:pTxt's class, plainText:isPlainText(pTxt)},
iTxt:{class:iTxt's class, plainText:isPlainText(iTxt)},
uTxt:{class:uTxt's class, plainText:isPlainText(uTxt)},
ciTxt:{class:ciTxt's class, plainText:isPlainText(ciTxt)},
cuTxt:{class:cuTxt's class, plainText:isPlainText(cuTxt)}}
--> {pTxt:{class:string, plainText:true}, iTxt:{class:string,
plainText:false}, uTxt:{class:Unicode text, plainText:false},
ciTxt:{class:string, plainText:false}, cuTxt:{class:string,
plainText:false}}
--------
There are various ways to extract the plain text from a record, the
most obvious (given the earlier example record) being:
--------
set t to "Hello world" as Unicode text
(t as record)'s «class ktxt»
--> "Hello world"
--------
However, most extraction methods will result in an error if the
original string is already plain text - normally requiring a try block
to trap such an eventuality.
Arthur Knapp's very neat hack* is based on the ability to assign values
to variables from certain properties of an object, by using a record.
For example:
--------
set {class:c, length:l, text:t} to "some text"
{c, l, t}
--> {string, 9, "some text"}
--------
It's slightly more complicated than that, and probably exploits a small
bug (though, thankfully, a long-standing one). The point here is that
it works when fed both styled and plain text - and is the shortest,
fastest method that I know for performing this type of coercion.
---
kai
* And if you ever get to read this, Arthur, do please pop in - 'cos we
miss your ingenuity and sparkling humour... :-)
_______________________________________________
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