swift + OS X sandboxing: treat 'NSVBOpenPanel' as a 'NSOpenPanel' :: because I need to get the sender in the delegate method
Asked Answered
P

2

3

Im using swift and I show a NSOpenPanel. In the delegate I need to look at the sender's prompt to distinguish which action to take:

e.g.

func show() {
    ... 
    panel.delegate = self
    panel.prompt = "xy"

    panel.run ....
}

func show2() {
    ... 
    panel.delegate = self
    panel.prompt = "abc"

    panel.run ....
}

//delegate
func panel(sender: AnyObject, shouldEnableURL url: NSURL) -> Bool {
    let panelPrompt = (sender as! NSOpenPanel).prompt       ... 
}
  • without sandbox = WORKS fine

    • the sender of the delegate is a NSOpenPanel indeed
  • with sandbox = Cast fails, crash

    • the sender of the delegate is NOT a NSOpenPanel but a NSVBOpenPanel. Apple's private class that remotely speaks to the outside world and allows the user to choose files NORMALLY not in your sandbox. (for details I refer to apple's sandboxing guide)

So the question is how do I do use this in swift without crashing?
Is there a nice way or is it just a bug/ugly idk behavior
Do I have to revert to use performSelector?

===

Addition: extensions to NSOpenPanel don't work either!

Polyclitus answered 9/5, 2016 at 15:27 Comment(2)
What is NSVBOpenPanel? It is mentioned in the title, but occurs nowhere in the question or answer.Halliehallman
sorry. youre right.. NSVVB stuff is apple's private sandbox open panel variant which should behave different at all AFAICS. Ill edit the question right awayPolyclitus
H
4

Instead of casting the sender to NSOpenPanel (which fails because the sender is an instance of the private NSVBOpenPanel class), or some performSelector magic, you can use the fact that arbitrary methods and properties can be accessed on AnyObject without casting, and the call behaves like an implicitly unwrapped optional:

func panel(sender: AnyObject, shouldEnableURL url: NSURL) -> Bool {
    let panelPrompt = sender.prompt ?? ""
    // ...
    return true
}

This gives the prompt for any sender object which has a prompt property, and the empty string as a fallback. In my test it worked well in a sandboxed environment.

See The strange behaviour of Swift's AnyObject for more details, examples, and references to the documentation.

Halliehallman answered 9/5, 2016 at 16:24 Comment(3)
Strange, but this does work with AnyObject. The sender seems to be immutable, however. How do you mutate it? Say needing to call sender.directoryURL = newURLModestia
bug when setting AnyObject parameters: forums.swift.org/t/…Modestia
@Modestia have you found out how to change the directory of NSVBOpenPanel? I'm looking for a Swift version of #5683166Wording
P
0

This is how it would work with performSelector. It is quite ugly though:

let panelPromptUnmanaged = (sender as! NSObject).performSelector(NSSelectorFromString("prompt"))
let panelPrompt = panelPromptUnmanaged != nil ? panelPromptUnmanaged.takeRetainedValue() as! String : ""
Polyclitus answered 9/5, 2016 at 15:33 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.