• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: 'Make new attachment' dilemma
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 'Make new attachment' dilemma


  • Subject: Re: 'Make new attachment' dilemma
  • From: Yvan KOENIG <email@hidden>
  • Date: Sat, 20 Jun 2015 12:01:26 +0200


Le 28/05/2015 à 02:07, Shane Stanley <email@hidden> a écrit :

On 27 May 2015, at 11:54 pm, Yvan KOENIG <email@hidden> wrote:

set pb to current application's NSPasteboard's generalPasteboard() -- get pasteboard
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"

It would probably be more generally useful if it wasn't confined to just TIFF on the clipboard. This version will check for several formats, in order, and return the first it finds:

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 type is missing value then error "No suitable image data found on the clipboard"
set theData to pb's dataForType:theType

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.

So I tried to use :

#=====

# 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


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: 'Make new attachment' dilemma
      • From: Shane Stanley <email@hidden>
    • Re: 'Make new attachment' dilemma
      • From: Yvan KOENIG <email@hidden>
  • Prev by Date: How can I add 'Conversation' button to Mail toolbar?
  • Next by Date: Re: 'Make new attachment' dilemma
  • Previous by thread: How can I add 'Conversation' button to Mail toolbar?
  • Next by thread: Re: 'Make new attachment' dilemma
  • Index(es):
    • Date
    • Thread