Re: how to get the text selection in an applescript
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: Mon, 19 Jan 2004 01:11:15 -0800
On Jan 18, 2004, at 11:50 PM, Michael Terry 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.
Alright, here's an example of how to get the selection in Panther based
on the comments from my last message. It searches on the selected text
at dict.leo.org using the default search engine. If you compile it as a
script and run it from Script Menu, it should work from most
applications. (However, I don't believe it will work from the Script
Menu in versions of OS X prior to Panther since you didn't used to be
able to get the true frontmost app. In that case, you'd just have to
hard code the handler call for whatever application you expect to use.)
I didn't know how you intended to retrieve the translation, so I don't
show you here how to replace the selection.
You're e-mail client will probably wrap the 'set urlPostfix to ..."
line, so make sure to fix that before you compile.
Mike
---------------------------------------------------
-- Search dict.leo.org From Selection
---------------------------------------------------
set urlPrefix to "
http://dict.leo.org/?search="
set urlPostfix to
"&searchLoc=0&relink=on&spellToler=standard§Hdr=on&tableBorder=1&cmp
Type=relaxed&lang=en"
set text item delimiters to "+"
set searchTerms to (words of copySelection(getFrontAppName())) as
Unicode text
if searchTerms is not "" then
set dictURL to urlPrefix & searchTerms & urlPostfix
open location dictURL
end if
on getFrontAppName()
return name of application (path to frontmost application as Unicode
text)
end getFrontAppName
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
_______________________________________________
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.