Read from the clipboard on Macos

I am a beginner in Swift, and I am trying to figure out how to read what has been copied to the clipboard on macOS (Swift 3)? I search a lot, but I can’t seem to find anything useful.

Some things I tried on the Internet:

var pasteboardItems: [NSPasteboardItem]? {get }
print("\(pasteboardItems)")

and

let pb = NSPasteboard.general() 
pb.string(forType: NSPasteboardTypeString)

print("\(pb)")

and

let pasteboard = UIPasteboard.general
if let string = pasteboard.string {
// text was found and placed in the "string" constant
}

Last< /p>

func paste(sender: AnyObject?) {

let pasteboard = NSPasteboard.generalPasteboard()

if let nofElements = pasteboard.pasteboardItems?.count {

if nofElements> 0 {


// Assume they are strings

var strArr: Array = []
for element in pasteboard.pasteboardItems! {
if let str = element.stringForType("public.utf8-plain-text") {
strArr.appen d(str)
}
}


// Exit if no string was read

if strArr.count == 0 { return }


// Perform the paste operation

dataSource.cmdPaste(strArr)
}
}
}< /pre>
for Swift 3 and Swift 4

// Set string to clipboard
let pasteboard = NSPasteboard.general
pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
pasteboard.setString("Good Morning ", forType: NSPasteboard.PasteboardType.string)

var clipboardItems: [String] = []
for element in pasteboard.pasteboardItems! {
if let str = element.string (forType: NSPasteboard.PasteboardType(rawValue: "public.utf8-plain-text")) {
clipboardItems.append(str)
}
}

/ / Access the item in the clipboard
let firstClipboardIt em = clipboardItems[0] // Good Morning

I am a beginner in Swift and I am trying to figure out how to read copied to clipboard on macOS (Swift 3) The content of the board? I search a lot, but I can’t seem to find anything useful.

Some things I tried on the Internet:

var pasteboardItems: [NSPasteboardItem]? {get }
print("\(pasteboardItems)")

and

let pb = NSPasteboard.general() 
pb.string(forType: NSPasteboardTypeString)

print("\(pb)")

and

let pasteboard = UIPasteboard.general
if let string = pasteboard.string {
// text was found and placed in the "string" constant
}

Last< /p>

func paste(sender: AnyObject?) {

let pasteboard = NSPasteboard.generalPasteboard()

if let nofElements = pasteboard.pasteboardItems?.count {

if nofElements> 0 {


// Assume they are strings

var strArr: Array = []
for element in pasteboard.pasteboardItems! {
if let str = element.stringForType("public.utf8-plain-text") {
strArr.append(str )
}
}


// Exit if no string was read

if strArr.count == 0 {return }


// Perform the paste operation

dataSource.cmdPaste(strArr)
}
}
}

< p>

Suitable for Swift 3 and Swift 4

// Set string to clipboard
let pasteboard = NSPasteboard.general< br />pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
pasteboard.setString("Good Morning", forType: NSPasteboard.PasteboardType.string)

var clipboardItems : [String] = []
for element in pasteboard.pasteboardItems! {
if let str = element.string(forType: NSPasteboard.PasteboardType(rawValue: "public.utf8-plain-text")) {
clipboardItems.append(str)
}
}

// Access the item in the clipboard
let firstClipboardItem = clipboardItems[0] // Good Morning

Leave a Comment

Your email address will not be published.