(half solve)Why copy file reference to clipboard programmatically only working for system folder in MacOS?

72 Views Asked by At

Solution

add this code pasteboard.data(forType: .fileURL) after pasteboard.writeObjects(emptyArray), it will work.

But I don't know why?


System Verison: MacOS Ventura 13.0

copy.swift

 #!/usr/bin/swift

 import Cocoa

 private func copyToClipBoard(path :String) {
     let pasteboard = NSPasteboard.general

     var emptyArray = [NSPasteboardWriting]()
     emptyArray.append(NSURL(fileURLWithPath: path))

     pasteboard.clearContents()

     pasteboard.writeObjects(emptyArray)

 }

 copyToClipBoard(path: CommandLine.arguments[1])

If I run swift copy.swift /Users/USER/Downloads/FILE, paste to chat window for third-party applications is working.

If I run swift copy.swift /Users/USER/tmp/FILE, cannot paste to chat window for third-party applications, application occur no permission error, I already give full access disk to this application.

What's the difference between Downloads and tmp?

How can I make copy file reference programmatically working for any folder?

0

There are 0 best solutions below