13 Replies Latest reply: Dec 5, 2011 11:47 AM by Annapolitan
caracos Level 1 Level 1

Hi, I'm having problem running Javascript command in Applescript after upgrading to Safari 5.1

Never had problem running the script below in previous version of Safari. The script suppose to copy selected text in Safari but somehow after upgrading to Safari 5.1, it keeps returning "missing value" instead of the text that I've selected in Safari.

Any solution for this problem? Thanks in advance.



tell application "Safari"


          set selectedText to (do JavaScript "getSelection()" in document 1)

end tell

MacBook Pro, Mac OS X (10.7)
  • Jacques Rioux Level 4 Level 4
    Mac OS X


    I don't know, but try this


    set selectedText to (do JavaScript "window.getSelection();" in document 1)


    set selectedText to (do JavaScript "document.getSelection();" in document 1)

  • caracos Level 1 Level 1

    Hi, I've tried both command as suggested but still getting "missing value"

    Any other suggestion that I could try? Thanks.

  • darrenm Level 1 Level 1

    I, too am having frustration with this. I've basically given up until someone inside can explain either new nomenclature for this in Safari, or a fix.


    In the mean time, I've had to resort to this, which I find a little more consistent than using "keystroke "c" using command down":


    tell application "Safari"


          tell application "System Events"

                        tell process "Safari"

                                  click menu item "Copy" of menu "Edit" of menu bar item "Edit" of menu bar 1

                                  delay 0.3

                        end tell

              end tell

              set clipText to (the clipboard) as text

    end tell

    An FYI, since upgrading to Lion I've had to tweak many scripts as things seem to run too fast sometimes and null results occur. I've had 100% success by adding/modifying the "delay" command.


    Message was edited by: darrenm

  • caracos Level 1 Level 1

    Hi darrenm, thank you for your help. I've used your technique to temporarily fixed the problem until Apple can come out with some explanation.

  • laurencefromwichita Level 1 Level 1

    1). restart system with original start up disc, holding down "c" key.


    2). using time machine restore from non "lion" operating system. this will kill p.o.s. lion.


    3). once back in non 10.7 OS be careful not to install via software updates, sarafi 5.1


    4). sarafi 5.1 like lion *****.


    5). if you have sarafi 5.1, degrade by typing in this: support.apple.com/kb/dl1939


         a). a very large software update that degraded my sarafi 5.1 back to sarafi 5.05


    6). streetsmart.com and java worked fine for me after this fix.


    7), also you can download firefox, and though slower than sarafi 5.05 it will load java, and stock screens.


    GOOD LUCK. my first microsoft moment with apple in 8 years of chronic love for their products. time to

    sell AAPL shares.

  • darrenm Level 1 Level 1

    You're very welcome. I've emailed back and forth with an expert who says it is indeed a bug and he's filed a report. So we'll hopefully see a fix soon.

  • darrenm Level 1 Level 1

    Really sorry you're having a "bear" of a time with Lion. There are definitely some bugs that need ironing out, that's for sure. Hey, you've gone 8 years w/o a Microsoft moment, better than every day WITH one!


    I'm assuming you ran the Java update for 10.7?




    From the release notes: "This release installs Java SE 6 version 1.6.0_26. OS X Lion does not provide a Java runtime by default."


    Hope things get fixed so you can get back to "normal" with your java and stock screens on Lion (eventually)

  • mistersquid Level 3 Level 3

    Does this problem exist also in Lion v. 10.7.1?

  • darrenm Level 1 Level 1

    Yup, still broken.

  • twtwtw Level 5 Level 5

    Just to clarify on this problem, It seems that do JavaScript is only broken in that it will no longer return nodes or node arrays to applescript - those are now missing values.  The javascript still works - for instance, even though window.getSelection() returns nothing, window.getSelection().anchorOffset returns the correct value and

    window.getSelection().toString() returns the string contents of the selection.


    It seems to me that most do JavaScript routines could be rewritten to avoid returning DOM elements, either by returning names/ids/values of nodes and working with those in applescript or by writing more extensive javascript code to process nodes internally to javascript. 


    Hope that's helpful.

  • nShare Level 1 Level 1

    Here is the answer to the first question in this thread. I found it somewhere last week on Github. I don't know about other JavaScript issues, but for selected text you do:


    tell application "Safari"


              set selectedText to (do JavaScript "var x = ''+document.getSelection();x" in document 1) as string

    end tell


    I've used this now in a number of Safari scripts that were broken and it works a treat!



  • darrenm Level 1 Level 1

    That works nicely. I'm sure Apple will still fix the other issue, but this is a fine work around/solution.


    Thanks for posting.

  • Annapolitan Level 1 Level 1

    After reviewing other post, I also found that simply adding (''+) to the original script works.  The parenthsis separate the quotes, but are not required. Example:


    tell application "Safari"


              set selectedText to (do JavaScript "(''+getSelection())" in document 1)

    end tell