4 Replies
      Latest reply on Oct 22, 2019 8:04 AM by bss
      bss Level 1 Level 1 (0 points)

        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?

        • Re: Trouble getting selected files via NSService
          Claude31 Level 8 Level 8 (7,005 points)

          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