I don’t want to keep always jpeg only but I feel that it may be interesting to paste jpeg in some cases where I know that it would be smaller than png.
With the proposed file I ran
[code]# borrowed from : How Do I Copy Image File to Clipboard and Retain Format?
use AppleScript version “2.4” – Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
(*
set posixPath to "/Users/shane/desktop/test-1200.png"
set posixPath to POSIX path of “Macintosh HD:Users:Important:pour ebay:_X28737ƒYK:##28755.png”
*)
set posixPath to POSIX path of ((path to desktop as text) & “test-1200.png”)
set {theData, theError} to current application’s NSData’s dataWithContentsOfFile:posixPath options:0 |error|:(reference)
set theClip to current application’s NSPasteboard’s generalPasteboard()
theClip’s clearContents()
theClip’s setData:theData forType:"public.png"
set theType to theClip’s (pasteboardItems()'s objectAtIndex:0)'s types() as list
log theType (public.png)
set theTypes to theClip’s types() as list
log theTypes (public.png, Apple PNG pasteboard type, public.tiff, NeXT TIFF v4.0 pasteboard type)
clipboard info
The default compfactor applied to jpeg is ≃ 0.75
–> {{«class PNGf», 1 070 884}, {«class 8BPS», 11 066 614}, {GIF picture, 626 510}, {«class jp2 », 4 139 634}, {JPEG picture, 6 359 827}, {TIFF picture, 6.3105946E+8}, {«class BMP », 6.31061382E+8}, {«class TPIC», 7 359 854}}
set theData to theClip’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 compfactor to 1.0
set theData to (newRep’s representationUsingType:(current application’s NSJPEGFileType) |properties|:{NSImageCompressionFactor:compfactor, NSImageProgressive:false})
theClip’s setData:theData forType:"public.jpeg"
set theType to theClip’s (pasteboardItems()'s objectAtIndex:0)'s types() as list
log theType (public.png, public.jpeg)
set theTypes to theClip’s types() as list
log theTypes (public.png, Apple PNG pasteboard type, public.jpeg, CorePasteboardFlavorType 0x4A504547, public.tiff, NeXT TIFF v4.0 pasteboard type)
clipboard info
with compfactor = 1.0
–> {{«class PNGf», 1 070 884}, {JPEG picture, 11 564 428}, {«class 8BPS», 11 066 614}, {GIF picture, 626 510}, {«class jp2 », 4 139 634}, {TIFF picture, 6.3105946E+8}, {«class BMP », 6.31061382E+8}, {«class TPIC», 7 359 854}}
[/code]
This thought me that :
(1) the jpeg stored by default is not the best quality one, it’s compressed with a factor ≃ 0.75
(2) how to store a jpeg with no compression
(3) the png object is smaller than the jpeg one so it’s useless to try to play with the jpeg one.