Archive for the 'NSPasteboardItem' Category

2015/08/04 ASOCでクリップボード(Pasteboard)の内容を操作


コピーした内容が入る概念的な場所をMac OSでは「クリップボード」と呼んでいましたが、OS Xの元となったNeXTstepでは「ペーストボード」と呼んでいました。このため、呼称が入り混じっていますが同じものです。

▲clipboard in Classic Mac OS (8.6)

▲clipboard in OS X 10.10

–Created 2015-08-03 by Shane Stanley

use AppleScript version “2.4″
use scripting additions
use framework “Foundation”
use framework “AppKit” – for NSPasteboard

set theClip to my fetchStorableClipboard()
–>  (NSArray) {​​​​​(NSPasteboardItem) ​​​}

set the clipboard to “blah blah blah”
my restoreClipboard:theClip
–>  true

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:

★Click Here to Open This Script