Simple example of NSTextView

Here is a simple example of a NSTextView.

You could copy Rich Text Format and paste inside the NSWindow.
In this example I do not use attributeString but I guess it would be possible.

In this example I only use a simple text string, you could run this script in Script Editor.

It would be cool to have a application in SplitView with PDF on left and Markdown on right.
The approach could be to select text on PDF and paste in the TextView to build a Markdown :slight_smile:

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

property arguments : missing value

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

on performTextView:arguments
    set {theWidth, theHeight} to {700, 600}
    
    set theTextView to createTextView(0, 0, theWidth, theHeight)
    theTextView's insertText:"Some text in the NSTextView..."
    
    set theWindow to createWindowWithRect(0, 0, theWidth, theHeight)
    theWindow's contentView()'s addSubview:theTextView
    
    set windowController to createWindowController(theWindow)
    windowController's |window|'s |center|()
    windowController's |window|'s makeKeyAndOrderFront:me
end performTextView:

on createTextView(xMin, yMin, xLen, yLen)
    set textViewSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
    set theTextView to current application's NSTextView's alloc()'s initWithFrame:textViewSize
    return theTextView
end createTextView

on createWindowWithRect(xMin, yMin, xLen, yLen)
    set windowSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
    set winStyle to (current application's NSWindowStyleMaskTitled as integer) + (current application's NSWindowStyleMaskClosable as integer)
    set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:yes
    return theWindow
end createWindowWithRect

on createWindowController(theWindow)
    set theController to current application's NSWindowController's alloc()'s initWithWindow:theWindow
end createWindowController

Same as post #1 but also include NSScrollView

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

property arguments : missing value

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

(**
* https://developer.apple.com/library/archive/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Introduction/Introduction.html
* Text views are user interface objects instantiated from the NSTextView class. Text views typically display
* multiple lines of text laid out in paragraphs with all the characteristics of sophisticated typesetting. 
* A text view is the main user interface to the Cocoa text-editing system. It handles user events to provide text
* entry and modification, and to display any font, including those of non-English languages, with arbitrary colors, 
* styles, and other attributes.
*)
on performTextView:arguments
	set {theWidth, theHeight} to {700, 600}
	
	set theTextView to createTextView(0, 0, theWidth, theHeight)
	theTextView's insertText:"Some text in the NSTextView..."
	theTextView's setAllowsUndo:true
	-- theTextView's setEditable:false
	-- theTextView's setSelectable:true
	
	set theScrollView to createScrollView(0, 0, theWidth, theHeight)
	theScrollView's setDocumentView:theTextView
	
	set theWindow to createWindowWithRect(0, 0, theWidth, theHeight)
	theWindow's contentView()'s addSubview:theScrollView
	
	set windowController to createWindowController(theWindow)
	windowController's |window|'s |center|()
	windowController's |window|'s makeKeyAndOrderFront:me
end performTextView:

on createTextView(xMin, yMin, xLen, yLen)
	set textViewSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
	set theTextView to current application's NSTextView's alloc()'s initWithFrame:textViewSize
	return theTextView
end createTextView

on createScrollView(xMin, yMin, xLen, yLen)
	set scrollSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
	set theScrollView to current application's NSScrollView's alloc()'s initWithFrame:scrollSize
	return theScrollView
end createScrollView

on createWindowWithRect(xMin, yMin, xLen, yLen)
	set windowSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
	set winStyle to (current application's NSWindowStyleMaskTitled as integer) + (current application's NSWindowStyleMaskClosable as integer)
	set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:yes
	return theWindow
end createWindowWithRect

on createWindowController(theWindow)
	set theController to current application's NSWindowController's alloc()'s initWithWindow:theWindow
end createWindowController