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

Discussion in 'Javascript' started by David, Dec 26, 2004.

  1. David

    David Guest

    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
     
    David, Dec 26, 2004
    #1
    1. Advertising

  2. 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/default.asp?url=/workshop/author/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.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 26, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    1,534
  2. yogesh
    Replies:
    1
    Views:
    399
    Victor Bazarov
    Mar 14, 2007
  3. HopfZ
    Replies:
    0
    Views:
    629
    HopfZ
    Aug 28, 2006
  4. wolis

    Safari and the Enter key

    wolis, Oct 10, 2006, in forum: Javascript
    Replies:
    0
    Views:
    96
    wolis
    Oct 10, 2006
  5. Tim Streater

    Safari enter/return bypassing my button script

    Tim Streater, Jun 12, 2007, in forum: Javascript
    Replies:
    7
    Views:
    123
    Randy Webb
    Jun 13, 2007
Loading...

Share This Page