Skip navigation

Problem with JavaScript in Safari 5.1

12506 Views 13 Replies Latest reply: Dec 5, 2011 11:47 AM by Annapolitan RSS
caracos Level 1 Level 1 (0 points)
Currently Being Moderated
Jul 21, 2011 5:32 PM

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"

  activate

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

end tell

MacBook Pro, Mac OS X (10.7)
  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    Jul 21, 2011 6:01 PM (in response to caracos)

    Hi,

    I don't know, but try this

     

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

    or

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

  • darrenm Calculating status...
    Currently Being Moderated
    Jul 22, 2011 8:11 AM (in response to caracos)

    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"

      activate

          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

  • laurencefromwichita Calculating status...
    Currently Being Moderated
    Jul 22, 2011 1:37 PM (in response to caracos)

    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 (0 points)
    Currently Being Moderated
    Jul 22, 2011 1:45 PM (in response to caracos)

    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 (0 points)
    Currently Being Moderated
    Jul 22, 2011 1:49 PM (in response to laurencefromwichita)

    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?

     

    http://support.apple.com/kb/DL1421

     

    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 (795 points)
    Currently Being Moderated
    Aug 26, 2011 10:41 AM (in response to caracos)

    Does this problem exist also in Lion v. 10.7.1?

  • darrenm Level 1 Level 1 (0 points)
    Currently Being Moderated
    Aug 26, 2011 11:12 AM (in response to mistersquid)

    Yup, still broken.

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    Aug 30, 2011 3:27 PM (in response to caracos)

    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 Calculating status...
    Currently Being Moderated
    Sep 1, 2011 3:34 AM (in response to caracos)

    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"

      activate 

              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!

     

    Nic.

  • darrenm Level 1 Level 1 (0 points)
    Currently Being Moderated
    Sep 1, 2011 7:50 AM (in response to nShare)

    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 Calculating status...
    Currently Being Moderated
    Dec 5, 2011 11:47 AM (in response to caracos)

    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"

      activate

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

    end tell

Actions

More Like This

  • Retrieving data ...

Bookmarked By (1)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.