简体   繁体   中英

NSSavePanel name not user editable

I'm a total beginner to OSX GUI programming, so please be gentle with me.

I'm trying some experiments with adding light GUI elements from appkit to a CLI, so I'm working on a very small program to take the contents of a PDF and save it to a text file.

Here's the code I have

import AppKit
import Foundation
import Quartz

func helperReadPDF(_ filename: String) -> String {
    let pdata = try! NSData(contentsOfFile: filename) as Data
    let pdf = PDFDocument(data: pdata)
    return pdf!.string!
}

func selectFile() -> URL? {
    let dialog = NSOpenPanel()
    dialog.allowedFileTypes = ["pdf"]
    guard dialog.runModal() == .OK else { return nil }
    return dialog.url
}


func getSaveLocation() -> URL? {
    let sa = NSSavePanel()
    sa.nameFieldStringValue = "Untitled.txt"
    sa.canCreateDirectories = true
    sa.allowedFileTypes = ["txt"]
    guard sa.runModal() == .OK else { return nil }
    return sa.url
}

let file = selectFile()?.path ?? ""

print("where to save?")

let dest = getSaveLocation()!

try! helperReadPDF(file).write(to: dest, atomically: true, encoding: .utf8)

(I know, there are lots of unidiomatic things in here, like all the forced unwrapping and pointlessly converting URLs to paths. I have obscure reasons...)

So this code mostly works: when I run it from a terminal window with swift guitest.swift it'll pop up a file picker window, let me select a pdf file, and then pop up a save dialogue and let me choose the directory, and then save the extracted text from the pdf into that directory.

But it won't let me change the filename. I can highlight the "Untitled.txt" provided by default, I can even get a cursor into the field... but it doesn't respond to keyboard input.

In this previous SO , someone suggested adding a nameFieldStringValue to make it editable, but, as you can see from the above code, I did that, and it doesn't work.

I see from this very old SO that at least in Objective-C-land, you have to initiate some kind of application object in order to accept keyboard input. Is that true today in Swift-land as well? (Even though for some weird reason you can accept mouse input without doing any of that?!) If so, how do I do that here?

Edit: I get from the comments to that last prior SO I linked that this is probably a terrible idea, and that if I want to learn Mac GUI programming I should do it the heavy way with XCode and storyboards and all the rest. But could you indulge my doing it the stupid way in an effort to try to learn one thing at a time? (Ie, learn the GUI APIs on offer without also trying to learn XCode and Apple's preferred style of architecture at the same time.)

Thanks!

(Swift 4.2 on latest version of OSX. Not using XCode at all.)

Setting the application's ActivationPolicy will make it work.

// Import statements... (import Quartz)

NSApplication.shared.setActivationPolicy(.accessory)

// Functions and so on... (func helper..)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM