How to move a symlink to the trash?

与世无争的帅哥 提交于 2019-12-01 18:23:45

Use FSPathMakeRefWithOptions() to generate an FSRef to the link. Then use FSMoveObjectToTrashSync() to delete it.

The other way would be to tell the NSWorkspace to “recycle” it, by sending it either a performFileOperation:source:destination:files:tag: message with the NSWorkspaceRecycleOperation operation or a recycleURLs:completionHandler: message.

I don't know how well either one of these would work on symlinks, but it's worth trying if you'd rather not deal with FSRefs.

my retro-futuristic approach

https://github.com/reklis/recycle

//
//  main.swift
//  recycle
//
//  usage: recycle <files or directories to throw out>
//

import Foundation
import AppKit

var args = NSProcessInfo.processInfo().arguments
args.removeAtIndex(0)   // first item in list is the program itself

var w = NSWorkspace.sharedWorkspace()
var fm = NSFileManager.defaultManager()

for arg in args {
    let path = arg.stringByStandardizingPath;

    let file = path.lastPathComponent
    let source = path.stringByDeletingLastPathComponent

    w.performFileOperation(NSWorkspaceRecycleOperation,
        source:source,
        destination: "",
        files: [file],
        tag: nil)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!