Numbers: Make new shape from clipboard

EDIT
Ok, so the pasted chart is an image not a shape. The question stands. Is there a better way of importing an image without pasting it?

ORIGINAL
Is there a way. besides command v, to copy a chart from excel to make a new shape in numbers?

The following creates a new shape much different from that copied in excel.


tell application "Numbers"
	tell active sheet of document 1
set thisShape to make new shape with data the clipboard
	end tell
end tell

If you do that you will get an image, not a living chart.
It would be better to open the spreadsheet in Numbers so you would get a new Numbers document.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) jeudi 28 janvier 2016 19:30:36

Hi Yvan,

I would have done that, except the chart is not supported in numbers. I am stuck with an image format.

Do you know of a way of creating an image without pasting it?

I am thinking of saving the clipboard to temp items and then inserting the url.

I don’t use Excel so I’m not sure of what is available in the clipboard.

You may try these scripts mainly written by Shane STANLEY.

# clip2tiff

use scripting additions
use framework "Foundation"
use framework "AppKit"

--set beg to current date
set pathToChart to (path to desktop as text) & "theCells.tiff"
my fileFromClipToPath:(POSIX path of pathToChart)

--tell application "SystemUIServer" to display dialog "terminé en " & (current date) - beg
--> "terminé en 28 secondes"

on fileFromClipToPath:thePath
	set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
	# log (pb's types as list)
	-->	{"com.apple.iWork.TSPNativeMetadata", "com.apple.iWork.TSPNativeData", "com.apple.iWork.TSPDescription", "public.rtf", "NeXT Rich Text Format v1.0 pasteboard type", "public.utf16-external-plain-text", "CorePasteboardFlavorType 0x75743136", "public.utf8-plain-text", "NSStringPboardType", "dyn.ah62d4rv4gk81n65yru", "CorePasteboardFlavorType 0x7573746C", "com.apple.traditional-mac-plain-text", "CorePasteboardFlavorType 0x54455854", "dyn.ah62d4rv4gk81g7d3ru", "CorePasteboardFlavorType 0x7374796C", "com.apple.rtfd", "public.html", "Apple HTML pasteboard type", "public.png", "Apple PNG pasteboard type", "com.adobe.pdf", "Apple PDF pasteboard type", "public.tiff", "NeXT TIFF v4.0 pasteboard type"}
	set theData to pb's dataForType:(current application's NSPasteboardTypeTIFF) -- get tiff data off pasteboard
	if theData = missing value then error "No tiff data found on clipboard"
	set theResult to (theData's writeToFile:thePath atomically:true)
	return (theResult = 1)
end fileFromClipToPath:
# clip2pdf

use scripting additions
use framework "Foundation"
use framework "AppKit"

# set beg to current date

set pathToChart to (path to desktop as text) & "theCells.pdf"
my fileFromClipToPath:(POSIX path of pathToChart)

# tell application "SystemUIServer" to display dialog "terminé en " & (current date) - beg

on fileFromClipToPath:thePath
	set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
	# log (pb's types as list)
	-->	{"com.apple.iWork.TSPNativeMetadata", "com.apple.iWork.TSPNativeData", "com.apple.iWork.TSPDescription", "public.rtf", "NeXT Rich Text Format v1.0 pasteboard type", "public.utf16-external-plain-text", "CorePasteboardFlavorType 0x75743136", "public.utf8-plain-text", "NSStringPboardType", "dyn.ah62d4rv4gk81n65yru", "CorePasteboardFlavorType 0x7573746C", "com.apple.traditional-mac-plain-text", "CorePasteboardFlavorType 0x54455854", "dyn.ah62d4rv4gk81g7d3ru", "CorePasteboardFlavorType 0x7374796C", "com.apple.rtfd", "public.html", "Apple HTML pasteboard type", "public.png", "Apple PNG pasteboard type", "com.adobe.pdf", "Apple PDF pasteboard type", "public.tiff", "NeXT TIFF v4.0 pasteboard type"}
	set theData to pb's dataForType:(current application's NSPasteboardTypePDF) -- get pdf data of pasteboard
	if theData = missing value then error "No pdf data found on clipboard"
	set theResult to (theData's writeToFile:thePath atomically:true)
	return (theResult = 1)
end fileFromClipToPath:

The pasteboard’s types reported in the scripts were got from a Numbers document.
Enable the log instruction to get the list of what is available after copying from Excel.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) jeudi 28 janvier 2016 19:50:10

I believe once the data is in the clipboard, I can simply

on saveToTemp(fileName)
	set savePath to (path to temporary items folder as text) & fileName & ".pdf"
	set myData to «class PDF » of (the clipboard as record)
	try
		set OA to open for access file savePath with write permission
		write myData to OA
		close access OA
	on error
		try
			close access file savePath
		end try
	end try
	return savePath
end saveToTemp

I repeat : I don’t use Excel so I don’t know what kind of data it put in the clipboard.
If it don’t put PDF data, your script will fail.

When I copy a chart from Numbers, your code fails.
If I copy a block of cells it works.

Here is a script, whose most of the code was written by Shane STANLEY, able to save as jpeg, tiff or png.

use scripting additions
use framework "Foundation"
use framework "AppKit"

set pathToChart to (path to desktop as text) & "theChart.JPEG"

log 1
my jpegFromClipToPath:(POSIX path of pathToChart) compressFactor:1.0
log result
log 2
my pngFromClipToPath:(POSIX path of pathToChart)
log result
log 3
my tiffFromClipToPath:(POSIX path of pathToChart)
log result
log 4
my fileFromClipToPath:(POSIX path of pathToChart)
log result


#===== NSBMPFileType, NSGIFFileType, NSJPEGFileType, NSPNGFileType, and NSTIFFFileType are other data formats

# CAUTION : thePath must be a POSIX path !

on jpegFromClipToPath:thePath compressFactor:compFactor -- 0.0 = max compression, 1.0 = none
	-- set thePath to POSIX path of thePath
	set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
	-- log (pb's types as list) # fails, I don't know why
	set theData to pb's dataForType:"public.tiff" -- get tiff data off pasteboard
	if theData = missing value then error "No tiff data found on clipboard"
	set newRep to current application's NSBitmapImageRep's imageRepWithData:theData
	set theData to (newRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:compFactor, NSImageProgressive:false})
	if (thePath does not end with ".jpg") and (thePath does not end with ".jpeg") then
		set thePath to my ChangeExtension(thePath, "jpg") as text
	end if
	set theResult to (theData's writeToFile:thePath atomically:true)
	return {(theResult = 1), thePath}
end jpegFromClipToPath:compressFactor:

#=====

on tiffFromClipToPath:thePath
	-- set thePath to POSIX path of thePath
	set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
	--- log (pb's types as list) # fails, I don't know why
	set theData to pb's dataForType:"public.tiff" -- get tiff data off pasteboard
	if theData = missing value then error "No tiff data found on clipboard"
	if thePath does not end with ".tiff" then set thePath to my ChangeExtension(thePath, "tiff") as text
	set theResult to (theData's writeToFile:thePath atomically:true)
	return {(theResult = 1), thePath}
end tiffFromClipToPath:

#=====

on pngFromClipToPath:thePath
	-- set thePath to POSIX path of thePath
	log "traite png"
	set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
	-- log (pb's types as list) # fails, I don't know why
	set theData to pb's dataForType:"public.png" -- get png data off pasteboard
	if theData = missing value then error "No png data found on clipboard"
	if thePath does not end with ".png" then set thePath to my ChangeExtension(thePath, "png") as text
	set theResult to (theData's writeToFile:thePath atomically:true)
	return {(theResult = 1), thePath}
end pngFromClipToPath:

#=====

on pngFromClipToPathAlt:thePath
	-- set thePath to POSIX path of thePath
	log "traite png alt"
	set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
	-- log (pb's types as list) # fails, I don't know why
	set theData to pb's dataForType:"public.tiff" -- get tiff data off pasteboard
	if theData = missing value then error "No tiff data found on clipboard"
	set newRep to current application's NSBitmapImageRep's imageRepWithData:theData
	set theData to (newRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSTIFFCompressionNone:1})
	if thePath does not end with ".png" then set thePath to my ChangeExtension(thePath, "png") as text
	set theResult to (theData's writeToFile:thePath atomically:true)
	return {(theResult = 1), thePath}
end pngFromClipToPathAlt:

#=====

on fileFromClipToPath:thePath
	set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
	set theType to pb's availableTypeFromArray:{current application's NSPasteboardTypeTIFF, current application's NSPasteboardTypePNG, current application's NSPasteboardTypePDF, "public.jpeg", "com.compuserve.gif", "com.microsoft.bmp"} -- returns first type found
	if the theType is missing value then error "No suitable image data found on the clipboard"
	log theType
	log (theType as text)
	set theData to pb's dataForType:theType
	if (theType = current application's NSPasteboardTypeTIFF) then
		if thePath ends with ".tiff" then
			-- no change
		else if thePath ends with ".png" then
			set newRep to current application's NSBitmapImageRep's imageRepWithData:theData
			set theData to (newRep's representationUsingType:(current application's NSPNGFileType) |properties|:{NSTIFFCompressionNone:1})
		else if (thePath ends with ".jpg") or (thePath ends with ".jpeg") then
			set theData to my convertToJpeg(theData)
		else
			set thePath to my ChangeExtension(thePath, "tiff") as text
		end if
	else if (theType = current application's NSPasteboardTypePNG) then
		if thePath ends with ".png" then
			-- no change	
		else if thePath ends with ".tiff" then
			set newRep to current application's NSBitmapImageRep's imageRepWithData:theData
			set theData to (newRep's representationUsingType:(current application's NSTIFFFileType) |properties|:{NSTIFFCompressionNone:1})
		else if (thePath ends with ".jpg") or (thePath ends with ".jpeg") then
			set theData to my convertToJpeg(theData)
		else
			set thePath to my ChangeExtension(thePath, "png") as text
		end if
	end if
	set theResult to (theData's writeToFile:thePath atomically:true)
	return {(theResult = 1), thePath}
end fileFromClipToPath:

#=====

on convertToJpeg(theData)
	set compFactor to 1
	set newRep to current application's NSBitmapImageRep's imageRepWithData:theData
	return (newRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:compFactor, NSImageProgressive:false})
end convertToJpeg

#=====

on ChangeExtension(thePath, newExt)
	set pathNSString to current application's NSString's stringWithString:thePath
	return pathNSString's stringByDeletingPathExtension()'s stringByAppendingPathExtension:newExt
end ChangeExtension

Tomorrow I will search in my archives in which I hope that there is a script able to convert the datas into PDF if it’s required.

At this time I wonder why the log instruction fails.
It worked in may 2015. Maybe something was broken by El Capitan.

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) jeudi 28 janvier 2016 22:47:41