more details (was Re: how to get the text selection in an applescript)
more details (was Re: how to get the text selection in an applescript)
- Subject: more details (was Re: how to get the text selection in an applescript)
- From: "leo" <email@hidden>
- Date: Tue, 20 Jan 2004 13:40:26 +1100
hi again
at first thanks for all the suggestions. great jobs!
the reason why i wasn't very specific about the apps _providing_ the
selection is,that i thought it is stored somewhere centrally in the os:
coming from windows i have the clipboard concept in mind.
and as well coming from windows, emerges the idea for the little translator
tool: " Lion" is a little windows app, just incorporating an browser window,
which displays a url like
http://dict.leo.org/?search=laugh&searchLoc=0&relink=on&spellToler=standard&
sectHdr=on&tableBorder=1&cmpType=relaxed&lang=en
where "laugh" is the term it grabs from the current application.
so the work flow is: using your favourite text app (like word, notepad,
emacs, or so), selecting the word which you want to translate (eg. laugh"
;-), hit your "lion" hot key and a browser window pops up with the mentioned
url. furthermore in lion you can then "option click" of one of the
translations, so that the window closes and the selection in your text app
is replaced by the translation.
well, now i thought i could rebuild this very handy tool on my iMac with
applescript (for finding the selection and composing the url), safari (for
viewing the url) and iKey (for the hot key).
i guess url composing, safari and iKey are straight forward. what's missing
is a preferably general way how to find the selected text and how to replace
the selection with another text.
hope that makes it a little bit clearer,
leo
----- Original Message -----
From: "Michael Terry" <email@hidden>
To: "Applescript Users" <email@hidden>
Sent: Monday, January 19, 2004 6:50 PM
Subject: Re: how to get the text selection in an applescript
>
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.
_______________________________________________
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.