You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Problem with JavaScript in Safari 5.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"


activate

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

end tell

MacBook Pro, Mac OS X (10.7)

Posted on Jul 21, 2011 5:32 PM

Reply
13 replies

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

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.

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)

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.

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.

Problem with JavaScript in Safari 5.1

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.