I'm continuing to work with this script.
At this time, I need an ending file of type bmp but the pasteboard doesn't contain this kind of datas.
At first, I created a png file then called ImageEvents to convert it in bmp but, as the original data has no background (transparent one) the resulting file was a rectangular block filled with black pixels.
#=====
# Handler built upon the one written by Shane STANLEY
on fileFromClipToPath:thePath
set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
set theType to pb's availableTypeFromArray:{current application's NSPasteboardTypeTIFF}
if the theType is missing value then error "No suitable image data found on the clipboard"
-- 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 required, save tiff data as is
else if thePath ends with ".png" then # Convert the Tiff datas into Png ones
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 # Convert the Tiff datas into Jpeg ones
set compFactor to 1
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})
else if (thePath ends with ".bmp") then # Convert the Tiff datas into Bmp ones
set newRep to current application's NSBitmapImageRep's imageRepWithData:theData
set theData to (newRep's representationUsingType:(current application's NSBMPFileType) |properties|:{NSImageProgressive:false})
else # No extension given, save as Tiff
set thePath to my ChangeExtension(thePath, "tiff") as text
end if
end if
set theResult to (theData's writeToFile:thePath atomically:true)
return (theResult = 1)
end fileFromClipToPath:
#=====
called passing a pathname ending with ".bmp"
Alas, the resulting file was a black block of pixels.
So, I built a workaround which converts the original datas in jpeg so that it has a white background then convert it in bmp.
It works and the code is given below.
#=====
# Handler built upon the one written by Shane STANLEY
on fileFromClipToPath:thePath
set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
set theType to pb's availableTypeFromArray:{current application's NSPasteboardTypeTIFF}
if the theType is missing value then error "No suitable image data found on the clipboard"
-- 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 required, save tiff data as is
else if thePath ends with ".png" then # Convert the Tiff datas into Png ones
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 # Convert the Tiff datas into Jpeg ones
set compFactor to 1
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})
else if (thePath ends with ".bmp") then # Convert the Tiff datas into Bmp ones
# convert it as jpeg one so that it has a background. Without that we get a black block
set compFactor to 1 # ADDED
set newRep to current application's NSBitmapImageRep's imageRepWithData:theData # ADDED
set theData to (newRep's representationUsingType:(current application's NSJPEGFileType) |properties|:{NSImageCompressionFactor:compFactor, NSImageProgressive:false}) # ADDED
# Now, converts the data into bmp
set newRep to current application's NSBitmapImageRep's imageRepWithData:theData
set theData to (newRep's representationUsingType:(current application's NSBMPFileType) |properties|:{NSImageProgressive:false})
else # No extension given, save as Tiff
set thePath to my ChangeExtension(thePath, "tiff") as text
end if
end if
set theResult to (theData's writeToFile:thePath atomically:true)
return (theResult = 1)
end fileFromClipToPath:
#=====
Is there a neater way to achieve the wanted goal ?
Yvan KOENIG (VALLAURIS, France) samedi 20 juin 2015 12:00:59