swiftaudionspasteboard

Swift 3 Mac OS Copy Audio File to NSPasteboard


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])

Solution

  • 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.