• 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: Script Menu or FastScript and paste
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Script Menu or FastScript and paste


  • Subject: Re: Script Menu or FastScript and paste
  • From: Shane Stanley <email@hidden>
  • Date: Mon, 03 Aug 2015 10:44:55 +1000

On 3 Aug 2015, at 2:08 am, Yvan KOENIG <email@hidden> wrote:

Is it any chance that using ASObjC to fill the pasteboard will give what I want ?

We can try. But first, some other code in your script:

set oldClipBoard to the clipboard as record # save it to reset it on exit
<snip>
set the clipboard to oldClipBoard # reset the original content

Although that appears to work in simple cases, it's not reliable. Try this test: in the Finder, select, say, three files, and copy. You will see the the paste command on the Edit menu says Paste 3 Items. Now run this code:

set oldClipBoard to the clipboard as record # save it to reset it on exit
set the clipboard to "blah blah blah"
set the clipboard to oldClipBoard # reset the original content

Look at the Edit menu in the Finder: it now says Paste Item. You've lost two items, although you may not notice because their names are still in the string on the clipboard. The problems is that AppleScript's clipboard command wasn't updated when multiple clipboard items were introduced. Someone should log a bug, although I can't imagine it being a high priority.

So if you want to store the clipboard's contents and restore it reliably, you probably need to use something like this, which is adapted from some code on Stackoverflow:

use scripting additions
use framework "Foundation"
use framework "AppKit" -- for NSPasteboard

on fetchStorableClipboard()
set aMutableArray to current application's NSMutableArray's array() -- used to store contents
-- get the pasteboard and then its pasteboard items
set thePasteboard to current application's NSPasteboard's generalPasteboard()
set theItems to thePasteboard's pasteboardItems()
-- loop through pasteboard items
repeat with i from 1 to count of theItems
-- make a new pasteboard item to store existing item's stuff
set newPBItem to current application's NSPasteboardItem's alloc()'s init()
-- get the types of data stored on the pasteboard item
set theTypes to (item i of theItems)'s |types|()
-- for each type, get the corresponding data and store it all in the new pasteboard item
repeat with j from 1 to count of theTypes
set theData to ((item i of theItems)'s dataForType:(item j of theTypes))'s mutableCopy() -- mutableCopy makes deep copy
if theData is not missing value then
(newPBItem's setData:theData forType:(item j of theTypes))
end if
end repeat
-- add new pasteboard item to array
(aMutableArray's addObject:newPBItem)
end repeat
return aMutableArray
end fetchStorableClipboard

on restoreClipboard:theArray
-- get pasteboard
set thePasteboard to current application's NSPasteboard's generalPasteboard()
-- clear it, then write new contents
thePasteboard's clearContents()
thePasteboard's writeObjects:theArray
end restoreClipboard:

set theClip to my fetchStorableClipboard()
set the clipboard to "blah blah blah"
my restoreClipboard:theClip

Try the Finder file-copy test and you should get the right result this time. Good fodder for a script library.

Now you can change this:

set the clipboard to leMessage

To this:

my restoreClipboard:{leMessage}

I don't know if that will solve your problem, though -- it sounds to me awfully like a timing issue. You could try adding a delay after the "set frontmost to true". 

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
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: Script Menu or FastScript and paste
      • From: Yvan KOENIG <email@hidden>
References: 
 >Script Menu or FastScript and paste (From: Yvan KOENIG <email@hidden>)

  • Prev by Date: Re: Script Menu or FastScript and paste
  • Next by Date: Re: Script Menu or FastScript and paste
  • Previous by thread: Re: Script Menu or FastScript and paste
  • Next by thread: Re: Script Menu or FastScript and paste
  • Index(es):
    • Date
    • Thread