Copying in all iWorks apps often gives No Text in Clipboard

Hey James,

If you use the Clipboard Viewer utility to see what Numbers puts on the clipboard in the first place, you'll find an almost unbelievable number of data types.

It's not that surprising (to me) that your macro is choking on something here and there, although it looks like it might be a bug @peternlewis needs to address – as there is a simple NSSringPboardType available and a public.utf8-plain-text type available.

I'm curious about what data-types are in the worksheets that are failing.

Probably you're better off to extract your selection with AppleScript, process it in AppleScript, and then export to a Keyboard Maestro variable (or whatever).

Here's a basic script to put the values of the selected cells on the clipboard:

-------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/02/04 14:00
# dMod: 2017/02/04 14:31
# Appl: Numbers
# Task: Extract Text from Selected Cells and Place in a Keyboard Maestro Variable.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro, @Numbers, @Extract, @Text, @Selected, @Cells, @Place, @Variable
-------------------------------------------------------------------------

tell application "Numbers"
   
   if document 1 exists then
      
      tell document 1
         
         tell active sheet
            
            try
               set activeTable to (the first table whose class of selection range is range)
            on error
               error "Something is wrong with the selection in the front document."
            end try
            
            tell activeTable
               set cellValues to value of cells of selection range
            end tell
            
            set AppleScript's text item delimiters to linefeed
            set cellValues to cellValues as text
            
            setKMVariable("numbersValues", cellValues) of me
            
         end tell
         
      end tell
      
   else
      error "Where is document 1"
   end if
   
end tell

-------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------
# Note that varName must be a string.
on setKMVariable(varName, varValue)
   tell application "Keyboard Maestro Engine"
      setvariable varName to varValue
   end tell
end setKMVariable
-------------------------------------------------------------------------

NOTE: I'm not sure this will work in all cases due to data-types, but it's working for me with text and date data-types right now on macOS 10.12.3 with Numbers 4.0.5.

If there are any failures they should be amenable to workarounds.

Now then – let's go really crazy and extract the public.utf8-plain-text data-type from the clipboard.

-------------------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2016/06/03 22:05
# dMod: 2017/02/04 14:47
# Appl: Keyboard Maestro
# Task: Extract public.utf8-plain-text data-type from the clipboard if available.
# Aojc: True
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Extract, @Keyboard_Maestro, @Extract, @public.utf8-plain-text, @Data-type, @Clipboard
-------------------------------------------------------------------------
use framework "Foundation"
use scripting additions
-------------------------------------------------------------------------

set kmPlistStr to missing value
set systemPasteBoard to current application's NSPasteboard's generalPasteboard()

if (systemPasteBoard's canReadItemWithDataConformingToTypes:{"public.utf8-plain-text"}) as boolean then
   # Copied public.utf8-plain-text
   set kmPlistStr to systemPasteBoard's stringForType:"public.utf8-plain-text"
end if

if kmPlistStr ≠ missing value then
   return kmPlistStr as text
else
   beep
end if

-------------------------------------------------------------------------

In general – run the AppleScripts from the Script Editor.app, before trying them from an Execute an AppleScript action, and look at the results in the result-viewer.

Although this won't work with the first script, because its result is placed into a Keyboard Maestro variable for you with cell values one per line.

The second script returns tab and LF delimited records.

It looks to me like you can make your process pretty bombproof, but only time and trial will tell.

If you run into any stumbling blocks let me know.

-Chris

1 Like