• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: how to get the text selection in an applescript
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to get the text selection in an applescript


  • Subject: Re: how to get the text selection in an applescript
  • From: Michael Terry <email@hidden>
  • Date: Sun, 18 Jan 2004 23:50:58 -0800

On Jan 18, 2004, at 7:47 PM, leo wrote:

i want to write a little script which retrieves an url incorporating the
selected text. (dict.leo.org offers great english-german translation on url
basis.) so, how do i find out the selected text in the current application?

and furthermore: is there a way to modify the text in a selection? i'd like
to give back a "result" (the translated word) in the same selection.


Like Gary said, it depends on the application. A regrettably large number of apps don't provide for getting the selection at all (well, no without help--see below). For instance, you can get the selected text from Script Editor 2 with:

tell front document to get contents of selection

You can set the selection like this:

tell front document to set contents of selection to "whatever"

You can get the selection Safari with:

tell application "Safari" to do JavaScript "getSelection()" in front document

It doesn't work on text in input boxes, though. I don't know enough of JavaScript to know if there's a way to get a input box's selection or set the selection, either.

If your OS is sufficiently advanced, there's a way to get and set the selection by scripting the user interface. In Panther you just need to make sure to check off "Enable access for assistive devices" in the Universal Access pane of System Preferences. Before Panther, you also need to install a beta version of the System Events application, though I don't know if this is still an available download from Apple. In any event, if you qualify, you can use the following handlers:


on copySelection(appProcName)
try
set epoch to date "Monday, January 1, 2001 12:00:00 AM"
set lastApp to path to frontmost application as Unicode text
set lastClip to the clipboard as record
set clipSig to (current date) - epoch
set the clipboard to ({integer:clipSig} & lastClip)
tell application "System Events" to tell application process appProcName
set frontmost to true
click menu item "Copy" of menu "Edit" of menu bar 1
end tell
delay 0.2
set returnVal to the clipboard
try
if (the clipboard as record)'s integer is clipSig then set returnVal to ""
end try
set the clipboard to lastClip
tell application lastApp to activate
return returnVal as Unicode text
on error eMsg number eNum
tell application lastApp to activate
error "Couldn't copySelection: " & eMsg number eNum
end try
end copySelection

on setSelection(val, appProcName)
try
set lastApp to path to frontmost application as text
set lastClip to the clipboard as record
set the clipboard to val
tell application "System Events" to tell application process appProcName
set frontmost to true
click menu item "Paste" of menu "Edit" of menu bar 1
end tell
delay 0.02
set the clipboard to lastClip
tell application lastApp to activate
return
on error eMsg number eNum
tell application lastApp to activate
error "Can't setSelection: " & eMsg number eNum
end try
end setSelection

Note that AS purists consider this approach gauche, but you gotta do what you gotta do. In a pinch, it's been effective for me. Depending on the responsiveness of your system and your tolerance for the responsiveness of your script, you may experiment with adusting the 'delay 0.2' lines.


Cheers,
Mike
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • more details (was Re: how to get the text selection in an applescript)
      • From: "leo" <email@hidden>
    • Re: how to get the text selection in an applescript
      • From: Michael Terry <email@hidden>
References: 
 >how to get the text selection in an applescript (From: "leo" <email@hidden>)

  • Prev by Date: Re: Standard Additions 'read' command - basic questions
  • Next by Date: Re: "Dynamic binding" of applications/class-names
  • Previous by thread: Re: how to get the text selection in an applescript
  • Next by thread: Re: how to get the text selection in an applescript
  • Index(es):
    • Date
    • Thread