Change Document in Open Window in MACos App

How to hide Open and Save document features in a Document Based application?

It is very simple - just remove (delete) the Open, Open Recent..., Save, Save as... menu items from the menu XIB. If you don't want a title bar, simply untick the "Title" checkbox for the window in the XIB, though that makes the window difficult to move.

Sample Image

If you have a title bar, to override "Untitled", you could

override var displayName: String! {
get {
return "Anything you like, even \"\""
}
set {

}
}

however that would still allow access to the save as menu through the chevron. To suppress that, you need an NSWindowDelegate Docs

window(_:shouldPopUpDocumentPathMenu:)
Asks the delegate whether the window displays the title pop-up menu in response to a Command-click or Control-click on its title.

Can Xcode Document-based app be set to always open in a new tab

Set window.tabbingMode to preferred in makeWindowControllers().

override func makeWindowControllers() {
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()

// Create the window and set the content view.
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.isReleasedWhenClosed = false
window.center()
window.contentView = NSHostingView(rootView: contentView)

window.tabbingMode = .preferred // open new document in tab

let windowController = NSWindowController(window: window)
self.addWindowController(windowController)
}

macOS SwiftUI: Instantiate new non-document window in document app

We need a WindowGroup for a window, so the first/simple step is just to add it, like

(Demo created with Xcode 12.5 / macOS 11.3, based on Document App template)

struct PlayDocumentXApp: App {
var body: some Scene {
DocumentGroup(newDocument: PlayDocumentXDocument()) { file in
ContentView(document: file.$document)
}
WindowGroup("Tools") {
Text("This is tool window")
.frame(width: 200, height: 400)
}
}
}

and we have it available via File > New menu

demo

If we want to create/show it programmatically from other place, say via commands, we can use URL-based approach, like

demo2

struct PlayDocumentXApp: App {
var body: some Scene {
DocumentGroup(newDocument: PlayDocumentXDocument()) { file in
ContentView(document: file.$document)
}
.commands {
CommandMenu("Test") {
Button("Show Tools") {
NSWorkspace.shared.open(URL(string: "myTools://my")!)
}
}
}
WindowGroup("Tools") {
Text("This is tool window")
.frame(width: 200, height: 400)
.handlesExternalEvents(preferring: Set(arrayLiteral: "myTools://my"), allowing: Set(arrayLiteral: "myTools://my"))
}
}
}

demo3

How do you make the Application window open when the dock icon is clicked?

Implement - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag in your app delegate. Check the documentation for the details of the return value.

Document based apps and non-document based apps behave slightly differently. If there are no open windows when the dock icon of a document based app is clicked then it will create a new document. If there are no open windows when the dock icon of a non-document based app is clicked then it will do nothing.

Document based application - how to open recent documents merged in one window

In Swift, try this: In your app delegate implement applicationDidFinishLaunching by calling 'window.mergeAllWindows' where 'window' is the window of the first window controller of the first document. Note that mergeAllWindows is available in Mac OS 10.12.

'DispatchQueue.main.asyncAfter' was used to ensure windows have been restored by the time mergeAllWindows is called, you may prefer a better way to ensure all windows have been restored.

func applicationDidFinishLaunching(_ aNotification: Notification) {

let dc = NSDocumentController.shared()

// …

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) { () -> Void in

if dc.documents.count > 0 {
let doc = dc.documents[0]

let wcs = doc.windowControllers

guard let window = wcs[0].window else { return }

if #available(OSX 10.12, *) {
window.mergeAllWindows(self)
} else {
// Fallback on earlier versions
}
}
}
}

Open window of an application and move it to a different Space in OSX, is it possible?

Short answer: you can't unless you make more than one copy of the application and change its bundle identifier (not recommended for lots of reasons). Spaces works on an application basis, not a per-instance basis (which is uncommon on OS X for GUI apps anyway) nor on a per-window basis of an app.



Related Topics



Leave a reply



Submit