Watson1978
1/7/2017 - 4:53 AM

readlinglist.swift

// Add URL into safari's reading list
//
// Usage:
//   swift readlinglist.swift http://apple.com
//   
import Cocoa

class AppDelegate: NSObject, NSApplicationDelegate, NSSharingServiceDelegate {
    func add(URL url : String) {
        let service = NSSharingService(named: NSSharingServiceNameAddToSafariReadingList)!
        service.delegate = self

        let item = [NSURL(string: url)!]

        service.perform(withItems: item)
    }

    public func sharingService(_ sharingService: NSSharingService, didShareItems items: [Any]) {
        print("Succeeded")
        exit(0)
    }

    public func sharingService(_ sharingService: NSSharingService, didFailToShareItems items: [Any], error: Error) {
        print("Failed")
        exit(1)
    }
}

if CommandLine.argc < 2 {
    exit(0)
}

var app = NSApplication.shared()
var delegate = AppDelegate()
delegate.add(URL: CommandLine.arguments[1])
app.delegate = delegate
app.run()