3

I was testing out the drag-drop feature in MacOS with a GUI app (uses SwiftUI framework).

I have modified Info.plist file to accept any file on drag-drop.

<key>CFBundleDocumentTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeExtensions</key>
            <array>
                <string>*</string>
            </array>
        </dict>
    </array>

DragDropV3App.swift

class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ notification: Notification) {
        print("hello")
        
        // C function which writes the passed argument (in this case, the string, "Hello") to a log file.
        PrintToFile("Hello");
    }
    
    func application(_ sender: NSApplication, openFile filename: String) -> Bool {
            
        PrintToFile ("File open func called!");
    
        return true
    }
}
    

@main
struct DragDropV3App: App {
    
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

When I drag and drop a file into the .app icon, the application launches and "Hello" string is written to log file, but the string - "File open func called!", is not present in the file. The application:openFile is not called when the file is drag-dropped.

To understand basic swift, I have followed the official documentation. Some of the links I referred during this exercise are,

  1. Basic Template of SwiftUI
  2. Add AppDelegate to SwiftUI app
  3. application:openFile

I have also checked other stackoverflow answers (like this) to check if this is right way. The application:openFile seems to be the right function.

Do let me know where I went wrong. Thank you.

NightFuryLxD
  • 847
  • 5
  • 15
  • As it stands, the `application(_:openFile:)` method should be called. Try a simpler app (ie without SwiftUI) and see if it acts the same. – msbit Jun 10 '21 at 06:54
  • I started with a simple console app. But the drag-drop works only on app bundles (.app files) - a GUI application. I had originally used AppKit AppDelegate lifecycle framework, but there were crashes. I was suggested to use new SwiftUI framework to create a GUI application. – NightFuryLxD Jun 11 '21 at 01:42
  • There are more keys that associate the _types_ of documents - see the [Information Property List Key Reference](https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/20001431-101685). – red_menace Jun 14 '21 at 13:35
  • @red_menance, With the current setting, app icon accepts files. I think that part is fine. – NightFuryLxD Jun 16 '21 at 07:49
  • @msbit, I did the same exercise in objective c.....without the swiftUI framework. It worked. application(_:openFile:) is called. But I'm still not sure where the problem is here. – NightFuryLxD Jun 16 '21 at 07:51

1 Answers1

0

env: macOS 14

@main
struct DragDropV3App: App {
    
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onOpenURL { url in  <--- here!
                    print(url)
                }
        }
    }
}

enter image description here

hstdt
  • 5,652
  • 2
  • 34
  • 34