Get the text selection from any app?

Hi everybody. I am still sort of new to actually WRITING my own AppleScripts, so forgive me if this is a FAQ or something. I couldn’t find anything searching the archives.

Something that seems like it should be automatically supported for any app is the ability to grab just the “selected text” of whatever text field is being edited at the time. For example, I would like to be able to select a word in whatever app I’m running, then go up to the script menu and select a script for “Find in Google.”

It seems that getting the selected text of an application is very specific to the application’s particular scripting dictionary, and often is not possible using only the built in scripting additions and the app’s own dictionary.

So is there a popular osax that provides this functionality? I couldn’t find anything on osaxen.com, but it was a bit overwhelming the number of onesy-twosy modules that are available. Should I go ahead and write my own or is there something I’m missing?

Thanks!
Daniel

If you copy after select the text, you can pick it from the clipboard (“the clipboard” command).
Otherwise, you may do it through GUI scripting: selecting the menu edit>copy or via keystroke apple+c.

You can try both Extra Suites http://www.kanzu.com/ and Apple’s GUI scripting software (you must download’n install the latest beta of system events at http://www.apple.com/applescript/GUI/).

I totally agree with your first comment. Unfortunately, your second comment as the actual situation. One possibility that covers many (but not all) applications in OS X is to use a scriptable Service. Services can operate on selected text, so if you could script a service to run from a given app and get the result, you may have what you want. Unfortunately, I don’t know whether you can tell a Service to run in given app. I also don’t know about scriptability of Services. Anyone with more info out there?

At least I know now what the challenge is! I will post here if I find any especially successful solutions!

Daniel