How do I use Javascript to tell Safari to enter text?

D

David

Safari under Mac OSX accepts a "Execute Javascript" apple event. Thus,
you can send an event to if from another application (or from the
Scripts menu).

I would like an event that does the equivalent of "set selection", so I
can fill in some text in a text entry box. In particular, I have a visa
card number stored in the Keychain database. I retrieve it with an
Applescript, then would like to execute the equivalent of "do
javascript "document.setSelection('1234-3456-4567-5678');"

I do not know Javascript and have not tripped over anything that looks
promising to do this. What I need is the syntax (if this is even
possible!) for the document.setSelection" command. What I want this to
do is output the text where the cursor currently is. [I can do this now
with as outputting keystrokes, but that is just so ugly!]

Any help would be greatly appreciated.

David

PS: if you want to contact me directly, I'm on mac.com with user name
dhoerl
 
M

Martin Honnen

David wrote:

I do not know Javascript and have not tripped over anything that looks
promising to do this. What I need is the syntax (if this is even
possible!) for the document.setSelection" command.

Well if the document object has a method named setSelection then the
syntax to call it is
document.setSelection()
if the method takes a string argument then you have the right syntax
already e.g.
document.setSelection('1234-3456-4567-5678');

But I don't think that is a syntax problem, it seems what you are after is
What I want this to
do is output the text where the cursor currently is.

the right object and the right method name. Those depend on the browser,
at least for some non-standardized stuff like text selection and its
manipulation.
As far as I know Safari so far has no API exposed to JavaScript to
manipulate the selection.
If they have one chances are they have tried to be compatible with an
existing API, MSIE/Win has one documented here:
<http://msdn.microsoft.com/library/d...hor/dhtml/reference/objects/obj_selection.asp>
So there you could do
if (document.selection && document.selection.createRange) {
document.selection.createRange().text = '1234-3456-4567-5678';
}
I do not have access to a Mac currently so you would need to try that
yourself.

Mozilla also has an API to manipulate the text selection but there are
different APIs there really, one exposed on text controls and one for
text rendered on the page. So with Mozilla you would need to know which
control you want to insert text into.
Example code is here:
<http://www.faqts.com/knowledge_base/view.phtml/aid/13562/fid/130>
Again you would need to test that with Safari yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top