Friday, October 22, 2021

#1 2021-07-03 11:22:01 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 901

NSPrintInfo and the methods

Early test to use NSPrintInfo class and the methods...
The example use styled text to build a textview to open print dialog...

Applescript:

use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions

property arguments : missing value

on run
   if my NSThread's isMainThread() as boolean then
       my performDialog:arguments
   else
       my performSelectorOnMainThread:"performDialog:" withObject:arguments waitUntilDone:true
   end if
end run

on performDialog:arguments
   -- width:595.0, height:842.0*
   -- 1.27 cm = 36 points
   set {leftMargin, rightMargin, topMargin, bottomMargin} to {36, 36, 36, 36}
   set {x, y, width, height} to {0, 0, 595, 842}
   
   set textString to "Welcome all scripters, my name is macscripter and I'm your host." & return & return & "Today is a beautiful day to not waste, lets learn something together..." & return & "AppleScript is not like other scripting language so please do not compare me, I have a soul and I'm special."
   set {theRed, theGreen, theblue} to {0.25, 0.4, 0.25} -- set text color and shadow
   
   -- Set the attributes for paragraph style. (alignment,lineSpacing)
   set attributedStringParagraphStyleObjects to createParagraphStyleAttributes("left", 0)
   
   -- Set the attributes for shadow. (shadowOffset,shadowBlurRadius,shadowColor)
   set attributedStringShadowObjects to createShadowAttributes({4.0, -4.5}, 3.6, {theRed, theGreen, theblue, 0.35})
   
   
   set {StrokeWidth, Obliqueness} to {2.5, -0.5}
   -- Set the values/objects for a attributedString.
   set attributedStringObjects to {current application's NSColor's colorWithRed:theRed green:theGreen blue:theblue alpha:1.0, ¬
       current application's NSFont's fontWithName:"Zapfino" |size|:8, ¬
       attributedStringShadowObjects, attributedStringParagraphStyleObjects, ¬
       StrokeWidth, Obliqueness}
   
   set attributedStringKeys to {current application's NSForegroundColorAttributeName, ¬
       current application's NSFontAttributeName, ¬
       current application's NSShadowAttributeName, ¬
       current application's NSParagraphStyleAttributeName, ¬
       current application's NSStrokeWidthAttributeName, ¬
       current application's NSObliquenessAttributeName}
   
   -- Make the AttributedString for NSTextView
   set theAttributedString to stringWithAttributes(textString, attributedStringObjects, attributedStringKeys)
   
   -- TextView
   set theTextView to createTextView(x, y, width, height)
   (theTextView's textStorage)'s appendAttributedString:theAttributedString
   -- Set the background color of text view.
   theTextView's setBackgroundColor:(current application's NSColor's colorWithRed:0.6 green:0.8 blue:0.4 alpha:1.0)
   log (theTextView's frame())
   set {theOrigin, theSize} to theTextView's frame()
   set {width, height} to theSize
   
   -- Setup the PrintInfo
   set thePath to POSIX path of (path to desktop) & "printInfo.pdf"
   set thePrintInfo to createPrintInfo(leftMargin, rightMargin, topMargin, bottomMargin, theTextView, thePath)
   thePrintInfo's runOperation()
end performDialog:

on createTextView(x, y, width, height)
   set textViewSize to current application's NSMakeRect(x, y, width, height)
   set theTextView to current application's NSTextView's alloc()'s initWithFrame:textViewSize
   return theTextView
end createTextView

(*******************************
Handlers for NSAttributedString
*******************************)


(**
* [Class]: NSAttributedString
*    A string with associated attributes (such as visual style, hyperlinks,
*    or accessibility data) for portions of its text.
**
* https://developer.apple.com/documentation/foundation/nsattributedstring?language=objc
**
* [stringWithAttributes(textString string, objects list, keys list)]
*)

on stringWithAttributes(textString, objects, keys)
   set textString to current application's NSString's stringWithString:textString
   set attributes to current application's NSDictionary's dictionaryWithObjects:objects forKeys:keys
   set attributedString to current application's NSAttributedString's alloc()'s initWithString:textString attributes:attributes
   -- log attributedString's |description|() as text
   -- current application's NSLog("%@", attributedString)
   return attributedString
end stringWithAttributes

(**
* [Class] NSParagraphStyle
*    The paragraph or ruler attributes for an attributed string.
**
* [createParagraphStyleAttributes(alignment string)]
*)

on createParagraphStyleAttributes(alignment, lineSpacing)
   set attributedStringParagraphStyle to (current application's NSParagraphStyle's defaultParagraphStyle())'s mutableCopy()
   if alignment is "center" then
       attributedStringParagraphStyle's setAlignment:(current application's NSTextAlignmentCenter)
   else if alignment is "right" then
       attributedStringParagraphStyle's setAlignment:(current application's NSTextAlignmentRight)
   end if
   (**
   * [Instance Property]: lineSpacing
   *    The distance in points between the bottom of one line fragment and the top of the next.
   *)

   attributedStringParagraphStyle's setLineSpacing:lineSpacing
   -- log attributedStringParagraphStyle's |description|() as text
   return attributedStringParagraphStyle
end createParagraphStyleAttributes

(**
* [Class]: NSShadow
*    An encapsulation of the attributes that create a drop shadow during drawing operations.
**
* [createShadowAttributes({width real,height real}, shadowBlurRadius real, {red real, green real, blue real, alpha real})]
*)

on createShadowAttributes(shadowOffset, shadowBlurRadius, shadowColor)
   set {theRed, theGreen, theblue, theAlpha} to shadowColor
   set attributedStringShadow to current application's NSShadow's alloc()'s init()
   (**
   * This property contains the horizontal and vertical offset values, specified
   * using the width and height fields of the CGSize data type. These offsets are
   * measured using the default user coordinate space and are not affected by custom
   * transformations. This means that positive values always extend down and to the
   * right from the user's perspective.
   *)

   attributedStringShadow's setShadowOffset:shadowOffset
   (**
   * This property contains the blur radius, as measured in the default user coordinate space.
   * A value of 0 indicates no blur, while larger values produce correspondingly larger blurring.
   * This value must not be negative. The default value is 0.
   *)

   attributedStringShadow's setShadowBlurRadius:shadowBlurRadius
   (**
   * The default shadow color is black with an alpha of 1/3. If you set this property
   * to nil, the shadow is not drawn. The color you specify must be convertible
   * to an RGBA color and may contain alpha information.
   *)

   attributedStringShadow's setShadowColor:(current application's NSColor's colorWithRed:theRed green:theGreen blue:theblue alpha:theAlpha)
   -- log attributedStringShadow's |description|() as text
   return attributedStringShadow
end createShadowAttributes

on createPrintInfo(leftMargin, rightMargin, topMargin, bottomMargin, theView, thePath)
   set theURL to current application's |NSURL|'s fileURLWithPath:thePath
   set theDict to (current application's NSDictionary's dictionaryWithObject:theURL forKey:(current application's NSPrintJobSavingURL))
   log theDict's superclass()'s |description|() as text
   
   set thePrintInfo to current application's NSPrintInfo's alloc()'s initWithDictionary:theDict
   -- Constants that specify values for the print job disposition.
   thePrintInfo's setJobDisposition:(current application's NSPrintSaveJob)
   -- Constants that specify the different ways in which an image is divided into pages.
   thePrintInfo's setHorizontalPagination:(current application's NSPrintingPaginationModeClip)
   thePrintInfo's setVerticalPagination:(current application's NSPrintingPaginationModeAutomatic)
   
   set pageSize to thePrintInfo's paperSize()
   thePrintInfo's setLeftMargin:leftMargin
   thePrintInfo's setRightMargin:rightMargin
   thePrintInfo's setTopMargin:topMargin
   thePrintInfo's setBottomMargin:bottomMargin
   log thePrintInfo's |description|() as text
   
   set theView1 to theView
   theView1's setFrame:{{0, 0}, {(pageSize's width) - leftMargin - rightMargin, 1200}}
   -- theView1's sizeToFit()
   
   
   set thePrintOperation to current application's NSPrintOperation's printOperationWithView:theView1 printInfo:thePrintInfo
   thePrintOperation's setShowsPrintPanel:true
   thePrintOperation's setShowsProgressPanel:false
   return thePrintOperation
end createPrintInfo

Last edited by Fredrik71 (2021-07-03 11:23:53 pm)


if you are the expert, who will you call if its not your imagination.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)