• 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: Scriptable multi-clipboard utility?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Scriptable multi-clipboard utility?


  • Subject: Re: Scriptable multi-clipboard utility?
  • From: Michael Grant <email@hidden>
  • Date: Sun, 09 Mar 2014 13:15:08 -0500

With BBEdit as the active app and running the script from Butler, if I leave out the activate line, the choose from list dialog doesn't get focus regardless of whether I'm telling System Events or SystemUIServer to display it. That in turn means I need to re-activate the front app even if the user cancels, so in the bottom section of the script it's more convenient to leave the if block inside the tell block rather than vice versa.

Turns out I was being dumb about the boolean test — the test wasn't actually failing at all, I was just forgetting to coerce myChoice (a single-item list) to text before setting the clipboard. Duh.

To Chris's suggestion of having a default item in the choose from list dialog, I can type-select without it. Nice to know that's there, though — before I was just arrowing through the whole list.

So, no ready-made ideas for fully restoring the clipboard if it starts out as something other than plain text but coerceable to text? (Like I said before, it's not worth the effort of finding/developing a solution if there's not already a drop-in solution.)

So here's my latest version of the script:


tell application "System Events"

set frontApp to (file of first process whose frontmost is true) as text

try

set fullClipText to (the clipboard as text)

activate

set myChoice to (choose from list (paragraphs of fullClipText))

on error

set myChoice to false

end try

end tell


tell application frontApp

activate

if myChoice is not false then

set the clipboard to (myChoice as text)

tell application "System Events" to keystroke "v" using {command down}

delay 0.1

set the clipboard to fullClipText

end if

end tell



Michael



On Sun, Mar 9, 2014 at 5:16 AM, koenig.yvan <email@hidden> wrote:

Le 09/03/2014 à 04:54, Michael Grant <email@hidden> a écrit :

For whatever reason, my version was failing when I tried to use the test 'if myChoice is false', i.e. the simple boolean. I wasn't getting consistent results trying to use that weird chevron class notation {«class fals»:false} either. And for my purposes, I'd rather not have the complication at runtime of allowing multiple selections, so for me the only reason not to use the text coercion would be that my version will fail if the chosen paragraph really *is* the text "false". 

As for the try block, this morning I was focused on text, but of course the choose from list line does throw an error if the original clipboard content can't be coerced to text. So in what I've got now, I restored the try block and also placed the line that sets the clipboard text to a variable inside it.

I also adopted Yvan's 'file of first process' to get the active app reference.

Chris, you mentioned the risk of leaving the dialog hanging if the user clicks away or switches applications, but not activating System Events doesn't actually prevent that, does it?

One more remaining flaw is that the original clipboard content is not fully restored if it starts out as data other than plain text but that can be coerced to text. I did a little bit of playing around with storing the uncoerced content to a variable before getting the text, but that failed with a file copied in the Finder and with the styled text of the compiled script itself from AppleScript Editor. Since the multi-clipboard utilities that started the whole thread do exist, there must be some way to restore the full content, at least via ASObjC if not plain AS, but for me I don't think it's worth the time investment to look for a solution.

For reference, here's my current version (which omits Yvan's multiple selection option and Chris's more complete error code):

tell application "System Events"
set frontApp to (file of first process whose frontmost is true) as text


try
set fullClipText to (the clipboard as text)
activate
set myChoice to ((choose from list (paragraphs of fullClipText)) as text)
on error
set myChoice to "false"
end try
end tell

tell application frontApp
activate
if myChoice is not "false" then
set the clipboard to myChoice
tell application "System Events" to keystroke "v" using {command down}
delay 1
set the clipboard to fullClipText
end if
end tell


Michael

I'm really puzzled because I never got any kind of problem with the test upon the boolean false.
In your code, it's the instruction 
set fullClipText to (the clipboard as text)
which may issue an error if the clipboard doesn't contain text data.
I tested with :

try
set maybe to true


choose from list {"azerty", "qsdfg", "wxcvb"}
on error
set maybe to false
end try
maybe

Clicking the Cancel button or hitting the Escape key changes nothing, maybe remains true which means that doing that doesn't issue an error.

Keeping you choice of coercing as text, I would use an edited version :

tell application "System Events"
set frontApp to (file of first process whose frontmost is true) as text
end tell
try
set fullClipText to (the clipboard as text)
tell application "SystemUIServer" to set myChoice to ((choose from list (paragraphs of fullClipText)) as text)
on error
set myChoice to "false"
end try

if myChoice is not "false" then
tell application frontApp
activate
set the clipboard to myChoice
tell application "System Events" to keystroke "v" using {command down}
delay .1
set the clipboard to fullClipText
end tell
end if


I guess that fullClipText can't be huge to fit in a choose from list dialog so the delay may be reduced to delay.1 with no problem.


Yvan KOENIG (VALLAURIS, France) dimanche 9 mars 2014 11:16:43



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden



--
There's really no such thing as translating. It's all a scam. We basically just make stuff up and try to make it sound plausible.
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Scriptable multi-clipboard utility?
      • From: "koenig.yvan" <email@hidden>
References: 
 >Scriptable multi-clipboard utility? (From: Michael Grant <email@hidden>)
 >Re: Scriptable multi-clipboard utility? (From: Michael Grant <email@hidden>)
 >Re: Scriptable multi-clipboard utility? (From: "koenig.yvan" <email@hidden>)
 >Re: Scriptable multi-clipboard utility? (From: "koenig.yvan" <email@hidden>)
 >Re: Scriptable multi-clipboard utility? (From: Michael Grant <email@hidden>)
 >Re: Scriptable multi-clipboard utility? (From: "koenig.yvan" <email@hidden>)
 >Re: Scriptable multi-clipboard utility? (From: Michael Grant <email@hidden>)
 >Re: Scriptable multi-clipboard utility? (From: "koenig.yvan" <email@hidden>)

  • Prev by Date: Re: Scriptable multi-clipboard utility?
  • Next by Date: Re: Scriptable multi-clipboard utility?
  • Previous by thread: Re: Scriptable multi-clipboard utility?
  • Next by thread: Re: Scriptable multi-clipboard utility?
  • Index(es):
    • Date
    • Thread