• 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: Shane Stanley <email@hidden>
  • Date: Sun, 21 Jun 2015 23:28:08 +1000

On 21 Jun 2015, at 3:13 pm, Shane Stanley <email@hidden> wrote:

You can also do rotation similarly, using affine transforms. That gets a bit more complicated.

But not too much. You create an affine transform for the rotation, and apply that to the graphics context. So the full code then becomes:

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

set thePath to "/Users/shane/Desktop/Testing4.bmp"
its fileFromClipToPath:thePath

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
set oldRep to current application's NSBitmapImageRep's imageRepWithData:theData
-- get size of bitmap
set {width:theWidth, height:theHeight} to oldRep's |size|()
-- calculate new height
set newHeight to theHeight - 50 -- whatever you want to chop off the top
-- make new bitmapImageRep; notice height and width are swapped
set newRep to (current application's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:newHeight pixelsHigh:theWidth bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(current application's NSDeviceRGBColorSpace) bitmapFormat:(current application's NSAlphaFirstBitmapFormat) bytesPerRow:0 bitsPerPixel:32)
-- create a rotation transform, rotating about the centre of the destination bitmapImageRep
set theTransform to current application's NSAffineTransform's transform()
theTransform's translateXBy:newHeight / 2 yBy:theWidth / 2 -- so it rotates about centre
theTransform's rotateByDegrees:90
theTransform's translateXBy:-theWidth / 2 yBy:-newHeight / 2 -- move it back
-- store the existing graphics context
current application's NSGraphicsContext's saveGraphicsState()
-- set graphics context to new context based on the new bitmapImageRep
(current application's NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:newRep))
-- set the active color to white
current application's NSColor's whiteColor()'s |set|()
-- fill the bitmapImageRep with white
current application's NSRectFill({origin:{x:0, y:0}, |size|:{width:newHeight, height:theWidth}})
-- apply the transform to the graphics context
theTransform's concat()
-- draw from the original bitmapImageRep to the new one
oldRep's drawInRect:{origin:{x:0, y:0}, |size|:{width:theWidth, height:newHeight}} fromRect:{origin:{x:0, y:0}, |size|:{width:theWidth, height:newHeight}} operation:(current application's NSCompositeSourceOver) fraction:1.0 respectFlipped:true hints:(missing value)
-- restore graphics state
current application's NSGraphicsContext's restoreGraphicsState()
-- save bitmapImageRep as image
set theData to (newRep's representationUsingType:(current application's NSBMPFileType) |properties|:{NSImageProgressive:false})
set theResult to (theData's writeToFile:thePath atomically:true)
-- current application's NSWorkspace's sharedWorkspace()'s openFile:thePath
return (theResult = 1)
end fileFromClipToPath:



-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
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

References: 
 >Re: 'Make new attachment' dilemma (From: Yvan KOENIG <email@hidden>)
 >Re: 'Make new attachment' dilemma (From: Shane Stanley <email@hidden>)
 >Re: 'Make new attachment' dilemma (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: 'Make new attachment' dilemma
  • Next by Date: Bluetooth headset button click event
  • Previous by thread: Re: 'Make new attachment' dilemma
  • Next by thread: Bluetooth headset button click event
  • Index(es):
    • Date
    • Thread