Re: Working with currently-selected text
Re: Working with currently-selected text
- Subject: Re: Working with currently-selected text
- From: kai <email@hidden>
- Date: Thu, 12 May 2005 10:56:43 +0100
On Thursday, May 12, 2005, at 03:32 am, I wrote:
set the clipboard to ""
tell application "System Events"
keystroke "c" using command down -- copy
set escTime to (current date) + 1 (* modify as required *)
repeat while (count (the clipboard)) is 0
if (current date) > escTime then error "Oops!" (* or whatever *)
end repeat
set theSelection to the clipboard
muckWith(theSelection)
set the clipboard to theSelection
keystroke "v" using command down -- paste
end tell
-------
The 'escTime' stuff is just a thought in case nothing gets selected
(and a perpetual loop ensues).
Incidentally Mark, since you're doing UI stuff anyway - and if you can
address the frontmost app by name, there should be another way of
checking whether or not copying is possible.
What I tried, some time back, was something like this:
----------------
tell application "System Events" to tell process "Mail" (* for example
*)
if enabled of menu item "Copy" of menu "Edit" of menu bar 1 then
keystroke "c" using command down -- copy
-- continue with script...
else
my errorHandler()
end if
end tell
----------------
Unfortunately, due to a bug [1], that particular approach doesn't work
here (regardless of the specific process). Of course, it may well have
been fixed in later systems. Might be worth a try. (AFAIK, most apps
that can select text should have this menu item in the same menu -
although the cynic in me is already shaking his head and cautioning
about the risk of tripping over exceptions...)
While on the subject, a 'neater' variation of the repeat loop that I
suggested earlier might go something like:
----------------
repeat with n from 1 to 6
if (count (the clipboard)) > 0 then exit repeat
if n > 5 then my errorHandler()
delay 0.2
end repeat
----------------
---
kai
[1] The details of the bug referred to above may no longer be relevant.
If the suggested menu stuff works for you (or if you're already bored
senseless), simply skip what follows. Nevertheless, just in case...
With the script constructed as shown above, the error returned is:
"NSCannotCreateScriptCommandError" (error 10).
I also tried getting the properties of the menu item first, like this:
----------------
tell application "System Events" to tell process "Mail"
set p to properties of menu item "Copy" of menu "Edit" of menu bar 1
--> {value:missing value, minimum:missing value, maximum:missing value,
title:"Copy", subrole:missing value, description:"menu item",
size:{113, 19}, focused:missing value, selected:missing value,
enabled:true, name:"Copy", role:"AXMenuItem", orientation:missing
value, help:missing value, class:menu item, position:{0, 69}}
----------------
However, when I then tried:
----------------
tell application "System Events" to tell process "Mail"
set p to properties of menu item "Copy" of menu "Edit" of menu bar 1
enabled of p
--> "Can't get enabled of {value:missing value, minimum:missing value,
maximum:missing value, title:"Copy", subrole:missing value,
description:"menu item", size:{113, 19}, focused:missing value,
selected:missing value, enabled:true, name:"Copy", role:"AXMenuItem",
orientation:missing value, help:missing value, class:menu item,
position:{0, 69}}."
----------------
The error returned was number -1728: errAENoSuchObject (which usually
occurs when the item requested is beyond the range of available items).
In fact, the only way I could avoid such an error was to copy/paste the
returned properties into Script Editor and check them again:
----------------
using terms from application "System Events"
set p to {value:missing value, minimum:missing value, maximum:missing
value, title:"Copy", subrole:missing value, description:"menu item",
size:{113, 19}, focused:missing value, selected:missing value,
enabled:true, name:"Copy", role:"AXMenuItem", orientation:missing
value, help:missing value, class:menu item, position:{0, 69}}
enabled of p
end using terms from
--> true
----------------
However, I digress - and I'd hope that this has now been fixed... :-)
---
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden