0

I am trying to copy an audio file to the NSPasteboard so that it can be pasted somewhere else on the computer or into another program like Ableton or Pro Tools. Here is how I am getting the url of the file. (An example url d after casting to string is: file:///Users/ben/Music/Ableton/User%20Library/Vox.wav )

let url = directoryItems?[tableview.selectedRow].url
let urlString = (url?.absoluteString)! as String

let pb = NSPasteboard.general()
let pasted = pb.writeFileContents(urlString)

It is not being copied to the pasteboard (pasted is set to false) and I can not find any resources that talk about writing audio files to the NSPasteboard. Any help would be greatly appreciated.

Edit:

I also tried using the url instead of the string and had the same outcome

    let pb = NSPasteboard.general()
    let pasted = pb.writeObjects([url as! NSPasteboardWriting])
Benjamin Porter
  • 447
  • 2
  • 7
  • 18

1 Answers1

0

To copy a file in such a way that you can paste it in the Finder, you'd want a file URL, not a string.

As for copying the music into a music editor, presumably you'd need to load the music file yourself into some waveform format that can be pasted into that editor.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • My most recent edit addresses your suggestion regarding the URL – Benjamin Porter Mar 20 '17 at 19:59
  • Well, I tried it (the thing I said in the first paragraph) and it worked fine. I was able to switch to the Finder and hit Edit > Paste, and the file whose file URL I had put on the pasteboard was copied to the current folder. Isn't that what you said you wanted? – matt Mar 20 '17 at 20:32
  • By the way I notice that you forgot to say `clearContents()`. – matt Mar 20 '17 at 20:34
  • I just forgot to clear the contents, it seems to be working now. Thanks! – Benjamin Porter Mar 20 '17 at 21:08