Monday, July 26, 2021

#1 2021-06-12 05:58:06 am

Registered: 2019-10-23
Posts: 854

NSTabView, NSTabViewItem example

Simple example of a NSTabView to display 2 tabs of WKWebView.

The approach of the code is to make it like building block to be easy to adapt and to use in other script. The labels and views need to be the same amount. In my example I have 2 tabs that show
2 websites. ex. and

The script could be running directly in Script Editor.


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
       my performSelectorOnMainThread:"performDialog:" withObject:arguments waitUntilDone:true
   end if
end run

on performDialog:arguments
   set labels to {"Apple", "Google"}
   set theWebView1 to createWebView("", 800, 600)
   set theWebView2 to createWebView("", 800, 600)
   set theView to {theWebView1, theWebView2}
   set theTabView to createTabViewWithRect(labels, 0, 10, 10, 830, 700, theView)
   set subviewItems to {theTabView}
   set theWindow to createWindowWithRect(subviewItems, 0, 0, 850, 750)
   theWindow's setTitle:"Examples with NSTabView..."
   theWindow's |center|()
   theWindow's makeKeyAndOrderFront:me
end performDialog:

on createTabViewWithRect(labels, theType, x, y, width, height, views)
   set labelsCount to count of labels
   set tabViewSize to current application's NSMakeRect(x, y, width, height)
   set theTabView to current application's NSTabView's alloc()'s initWithFrame:tabViewSize
   theTabView's setTabViewType:theType
   repeat with i from 1 to labelsCount
       set theTabViewItem to (current application's NSTabViewItem's alloc()'s initWithIdentifier:i)
       (theTabViewItem's setLabel:(item i of labels))
       (theTabViewItem's setView:(item i of views))
       (theTabView's addTabViewItem:theTabViewItem)
   end repeat
   return theTabView
end createTabViewWithRect

on createWindowWithRect(subviewItems, x, y, width, height)
   set windowSize to current application's NSMakeRect(x, y, width, height)
   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:true
   repeat with anSubview in subviewItems
       (theWindow's contentView()'s addSubview:anSubview)
   end repeat
   return theWindow
end createWindowWithRect

on createWebView(theURLString, width, height)
   set theConfiguration to current application's WKWebViewConfiguration's alloc()'s init()
   set theFetch to my fetchJSSourceString(theURLString)
   set theUserScript to current application's WKUserScript's alloc()'s initWithSource:theFetch injectionTime:(current application's WKUserScriptInjectionTimeAtDocumentEnd) forMainFrameOnly:true
   set theUserContentController to current application's WKUserContentController's alloc()'s init()
   theUserContentController's addUserScript:theUserScript
   theConfiguration's setUserContentController:theUserContentController
   set webViewSize to current application's NSMakeRect(0, 0, width, height)
   set webView to current application's WKWebView's alloc()'s initWithFrame:webViewSize configuration:theConfiguration
   webView's setNavigationDelegate:me
   webView's setUIDelegate:me
   webView's setTranslatesAutoresizingMaskIntoConstraints:true
   set theURL to current application's |NSURL|'s URLWithString:theURLString
   set theRequest to current application's NSURLRequest's requestWithURL:theURL
   webView's loadRequest:theRequest
   return webView
end createWebView

on fetchJSSourceString(theURLString)
   set theURL to current application's |NSURL|'s URLWithString:theURLString
   set theSource to current application's NSString's stringWithContentsOfURL:theURL encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
   return theSource
end fetchJSSourceString

Last edited by Fredrik71 (2021-06-14 05:39:20 pm)

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



#2 2021-06-14 08:42:38 am

From:: Greece
Registered: 2019-03-03
Posts: 1954

Re: NSTabView, NSTabViewItem example

Nice example, thanks.

set type in the createTabViewWithRect method in the pipes, and it will work in the Script Debugger too.

Last edited by KniazidisR (2021-06-14 08:44:01 am)

Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)