Trouble getting selected files via NSService

I recently switched to Swift 4. My app has a service which works off the list of selected Finder items.


I used to get a list of selected Finder items with


let fileArray : [String] = pboard.propertyList(forType: NSFilenamesPboardType) as! [String]


With Swift 4, I now get this error:


'NSFilenamesPboardType' is unavailable in Swift: use 'PasteboardType.fileURL'


So I replace this with


let fileArray : [String] = pboard.propertyList(forType: .fileURL) as! [String]


But at runtime, I get this:


Could not cast value of type '__NSCFString' (0x7fffa9f86378) to 'NSArray' (0x7fffa9f87db8).


How do I get around this? Is it no longer an array that comes back?

Accepted Reply

I distinctly remember posting this in the Processes section, not sure how it ended up in Cocoa Touch. Thanks for replying anyway! Yes, definitely MacOS, not iOS.


I managed to get it going with this, which I think I found on Stack Overflow (sorry don't have the link):


guard let fileArray = pboard.propertyList(forType: NSPasteboard.PasteboardType(rawValue: "NSFilenamesPboardType")) as? [String]
            else { return }

Replies

Is it an iOS (CocoaTocuh) app or OSX (Cocoa) ? Cocoa I guess.


I tested the following which compiles and run


var pboard = NSPasteboard()

let fileArray : [String] = pboard.propertyList(forType: .fileURL) as! [String]


But I get an error log (probably because I miss some register elsewhere):

OSX[13006:7378450] +[NSPasteboard allocWithZone:]: unrecognized selector sent to class 0x7fff8ca9c848


I completed with:

        let pboard = NSPasteboard.general
        print(pboard.name)
        let fileArray = pboard.propertyList(forType: .fileURL) as! [String]
        print("fileArray", fileArray)

I selected files in Finder, copied (in pasteboard) and ran.

Crash as for you:

NSPasteboardName(_rawValue: Apple CFPasteboard general)

Could not cast value of type '__NSCFString' (0x7fff8d157378) to 'NSArray' (0x7fff8d158db8).

2019-10-22 11:41:15.540177+0200 Test For XCode10_3 OSX[13562:7449634] Could not cast value of type '__NSCFString' (0x7fff8d157378) to 'NSArray' (0x7fff8d158db8).



So I tested

        let pboard = NSPasteboard.general
        print(pboard.name)
        let fileArray = pboard.propertyList(forType: .fileURL) as! String
        print("fileArray", fileArray)

I selected files in Finder, copied, and ran.


I got one name, the first selected:

NSPasteboardName(_rawValue: Apple CFPasteboard general)

fileArray file:///Users/ME/Desktop/Facture%20Appilys%20-%20MyFile.pdf


SO, .fileURL returns a String, not an array.

I could not find how to return an array (which selector ?)


I had a quick look here, but did not implement.

https://stackoverflow.com/questions/28504213/writing-fileurls-name-to-pasteboard

I distinctly remember posting this in the Processes section, not sure how it ended up in Cocoa Touch. Thanks for replying anyway! Yes, definitely MacOS, not iOS.


I managed to get it going with this, which I think I found on Stack Overflow (sorry don't have the link):


guard let fileArray = pboard.propertyList(forType: NSPasteboard.PasteboardType(rawValue: "NSFilenamesPboardType")) as? [String]
            else { return }

Thanks for the followup. Don't forget to close the thread on your correct answer.

Yep will do.