macOS Drag and Drop onto App Icon


Bill Pitcher
 

Hi Folks,

I will dispense with a rant about the eroding freedoms and the pains of sandboxing.


My problem is finding a correct (App Store approval) method to enable drag and drop to the App Icon.

I'm fan of this Mac feature and consider it an act of "user intent" but I can't find a override to get the dropped URL.

Code examples are always good too.

Thanks
Bill


Bill Pitcher
 

for archive sake…

As is often the case with my problems, it lay between the keyboard and the seat.

Answer: check the filename before making it into a URL

.expandingTildeInPath
fileURLWithPath:

are your friends. It sure produced some interesting messages in console which put me off the scent.

Also os.log is a great tool to deal with the unruly console log, I remember when you could get that console torrent completely silent.

Thanks
Bill

On 21 Apr 2019, at 8:31 am, Bill Pitcher <bill@ilike.co.nz> wrote:

Hi Folks,

I will dispense with a rant about the eroding freedoms and the pains of sandboxing.


My problem is finding a correct (App Store approval) method to enable drag and drop to the App Icon.

I'm fan of this Mac feature and consider it an act of "user intent" but I can't find a override to get the dropped URL.

Code examples are always good too.

Thanks
Bill


 



On Apr 20, 2019, at 3:31 PM, Bill Pitcher <bill@...> wrote:

My problem is finding a correct (App Store approval) method to enable drag and drop to the App Icon.
I'm fan of this Mac feature and consider it an act of "user intent" but I can't find a override to get the dropped URL.

Same as it ever was — just implement the -application:openFile: method in your app delegate.
You also need to edit your Info.plist to include the document types your app knows how to open.

To my knowledge, none of this is affected by sandboxing. The sandbox allows your app to access any files that the user has explicitly asked it to open, via drag-and-drop or the Open panel.

—Jens


Marco S Hyman
 


My problem is finding a correct (App Store approval) method to enable drag and drop to the App Icon.
I thought this was answered earlier. Implement this in your app delegate

// MARK: Open File (UTI support)

/// process files give to us via "Open With..."
///
/// - parameter sender: unused
/// - parameter fileName: path of file to process
/// - returns: true if the file was opened
///
/// If multiple files are specified the function appears to be called once per file.

func application(_ sender: NSApplication,
openFile filename: String) -> Bool {
let url = URL(fileURLWithPath: filename)
/// do something with URL Return true if OK

return false
}

I think that is all that is necessary.

And with that, a caveat. My “do something” code happens to eventually contain this:

updateGroup.enter()
DispatchQueue.global(qos: .userInitiated).async {
// some code
DispatchQueue.main.async {
// more code
updateGroup.leave()
}
}

When I drag a file onto the app icon my app dies with

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't posix_spawn: error 1'
abort() called
terminating with uncaught exception of type NSException

I don’t think it did that before I added the DispatchQueue. I just discovered this. Something else to debug.

Marc