Open Bug 490830 Opened 15 years ago Updated 3 years ago

Can't get content of selection from AppleScript

Categories

(Core :: Widget: Cocoa, defect, P5)

x86
macOS
defect

Tracking

()

People

(Reporter: elharo, Unassigned)

References

(Blocks 1 open bug)

Details

User-Agent:       Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10
Build Identifier: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.10) Gecko/2009042315 Firefox/3.0.10

This bit of AppleScript used to work circa Mozilla 1.0 and Firefox 1.0:

set theQuote to contents of selection as string

However it hasn't worked for years, including in Firefox 2 up through Firefox 3.5b4. Since there's no AppleScript dictionary anymore it's hard to tell if there's an alternative or not. 

Reproducible: Always

Steps to Reproduce:
1. Make selection in Firefox
2. Run this script in Script editor:

tell application "Firefox 3"
	set Link to get «class curl» of window 1 as text
	set theTitle to «class pTit» of window 1 as text
	set theQuote to contents of selection as string
	{Link, theTitle, theQuote}
end tell


3. Notice that result is {"http://en-us.www.mozilla.com/en-US/firefox/3.0.10/whatsnew/", "", ""} The last string should not be empty. 
Actual Results:  
{"http://en-us.www.mozilla.com/en-US/firefox/3.0.10/whatsnew/", "", ""}

Expected Results:  
{"http://en-us.www.mozilla.com/en-US/firefox/3.0.10/whatsnew/", "", "content of selection"}

This may be a failure to implement a text commands suite that was implemented in earlier versions of Firefox/Mozilla.
Component: General → Widget: Cocoa
Product: Firefox → Core
QA Contact: general → cocoa
Are you sure this is the right way to ask an application for its selection?  It doesn't work with Safari or TextEdit either.
The script in comment 0 doesn't work as far back as Firefox 1.0.8 ('get contents of selection' returns 'current application'), and the dupe implies it broke before Mozilla 1.2b1!  (In the old Carbon AE code, "selection" is still listed as a property that can be gotten, but I don't immediately see how it's connected to Gecko to do the getting; perhaps it's that code that went missing before 1.2b1.  I'm not going to dig through history and that annoying Carbon AR code to figure it out, though :P)

That is (or more probably was) the standard syntax for requesting the contents of a text selection (asking for just a selection returns the bounds, so to speak, of the selection, e.g. 'characters 6969363 thru 6972944 of text document 1 of application "TextWrangler"').

That command doesn't work in TextEdit because astonishingly for a text editor, TextEdit has no "selection" property! :P  In fact, the core Text Suite doesn't have a "selection" property, so if you're using the Cocoa text system and relying on the Text Suite to handle scripting of it for you, you have no "selection".

If you're not using the Cocoa text system or if you've decided "selection" is important to scripting your text, you've implemented support for some sort of "selection" in your own suite.  For instance,

tell application "TextWrangler"
	set foo to contents of selection of window 1
end tell

will work if you have Bare Bones's TextWrangler, and 

tell application "Coda"
	selected text of current split of front document
end tell

will work if you have Panic's Coda.

Similar to TextEdit, Safari uses the standard Text Suite (again, it has no "selection" property); you can fake getting the selected text in Safari with DOM methods via the massive security-hole that is 'do javascript', though.

I believe Camino is the only Mac browser that can currently get the selected text on a web page via the application's own AppleScript property; we have a different syntax, though, because we don't have the Text Suite ('selected text of browser window 1').  Coda uses the same property name ("selected text"), partly I'm sure to prevent collisions in case "selection" ever gets added (back) to the Text Suite in a future OS release, and partly perhaps because they might want "selection" to be able to also return something in their list view of site files/folders (like "selection" does in the Finder).

In short, I think this is a valid bug, particularly given the dupe had been confirmed back when this behavior regressed, though calling it a regression after all these years might be a stretch.  However, I think a summary that's not tied to a particular old syntax is better, e.g., "Can't get selected text via AppleScript".
The script in comment 0 used to work, an increasingly long time ago. That said, I'm not wedded to any particular syntax. There just needs to be some plausible way to get the contents of the selection as a string. In Safari I currently use this trick:

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

Given that selection is not part of the standard text suite, I think the BBEdit/TextWrangler approach is as good as any.
Depends on: 608049
Status: UNCONFIRMED → NEW
Ever confirmed: true
Summary: Can't get content of selection fropm AppleScript → Can't get content of selection from AppleScript
Blocks: 125419
Severity: major → S4
Priority: -- → P5
You need to log in before you can comment on or make changes to this bug.