Skip to content

Instantly share code, notes, and snippets.

@KevinGutowski
Last active September 26, 2020 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save KevinGutowski/80f81a094420d0189ba54b89e1842890 to your computer and use it in GitHub Desktop.
Save KevinGutowski/80f81a094420d0189ba54b89e1842890 to your computer and use it in GitHub Desktop.
Replicating Sketch's Preview window
let previewWindow = NSApplication.sharedApplication().windows().find(window => window.title() == 'Preview')
let webview = previewWindow.contentView().subviews()[1] // need to launch sketch preview in order to find the proper webview and to start up the sketch server
let threadDictionary = NSThread.mainThread().threadDictionary()
let panelID = "com.testPanel"
if (threadDictionary[panelID]) {
let panel = threadDictionary[panelID]
panel.close()
threadDictionary.removeObjectForKey(panelID)
}
var panelWidth = 312
var panelHeight = 210
let panel = NSPanel.alloc().init()
panel.setFrame_display(NSMakeRect(0, 0, panelWidth, panelHeight), true)
panel.setStyleMask(NSTexturedBackgroundWindowMask | NSTitledWindowMask | NSClosableWindowMask)
panel.title = "Testing"
panel.center()
panel.makeKeyAndOrderFront(null)
panel.setLevel(NSFloatingWindowLevel)
panel.standardWindowButton(NSWindowMiniaturizeButton).setHidden(true)
panel.standardWindowButton(NSWindowZoomButton).setHidden(true)
threadDictionary[panelID] = panel
//Setup
let frame = NSMakeRect(0,0,panelWidth,panelHeight)
let webviewConfig = webview.configuration().copy()
let newWebview = MSMirrorWebView.alloc().initWithFrame_configuration(frame,webviewConfig)
let url = webview.URL()
let request = NSURLRequest.alloc().initWithURL(url)
newWebview.loadRequest(request)
panel.contentView().addSubview(newWebview)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment