Home > Enterprise >  Trying to read MacOS clipboard contents
Trying to read MacOS clipboard contents

Time:07-03

On my adventure to learn Rust I decided to try and print to the cli contents of the clipboard. I've done this before in Swift so thought I would have much issues in Rust.

However I'm having a hard time printing the contents of the returned NSArray. I've spent a few hours playing around with different functions but haven't made much progress.

The Swift code I have that works:

import Foundation
import AppKit

let pasteboard = NSPasteboard.general



func reload() -> [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)
        }
    }
    return clipboardItems;
}


// Access the item in the clipboard
while true {
    let firstClipboardItem = reload()
    print(firstClipboardItem);
    sleep(1);
}

Here is the Rust code:

use cocoa::appkit::{NSApp, NSPasteboard, NSPasteboardReading, NSPasteboardTypeString};
use cocoa::foundation::NSArray;

fn main() {
    unsafe {
        let app = NSApp();
        let pid = NSPasteboard::generalPasteboard(app);
        let changec = pid.changeCount();

        let pid_item = pid.pasteboardItems();

        if pid_item.count() != 0 {
            let items = &*pid_item.objectAtIndex(0);
            println!("{:?}", items);
        }

        println!("{:?}", *pid.stringForType(NSPasteboardTypeString));
    }
}

The code above produces: *<NSPasteboardItem: 0x6000021a3de0>*

EDIT:

I've made a little progress but stuck on one last bit. I've managed to get the first UTF8 char out of the clipboard.

The issue I have is if I copy the text: World the system will loop the correct amount of times for the word length but will only print the first letter, in this case W. Output below:

TEXT 'W'
TEXT 'W'
TEXT 'W'
TEXT 'W'
TEXT 'W'

The bit I'm trying to get my head around is how to move to the next i8. I can't seem to find a way to point to the next i8.

The NSString function UTF8String() returns *const i8. I'm scratching my head with how one would walk the text.

use cocoa::appkit::{NSApp, NSPasteboard, NSPasteboardTypeString};
use cocoa::foundation::{NSArray, NSString};

fn main() {
    unsafe {
        let app = NSApp();
        let pid = NSPasteboard::generalPasteboard(app);
        let changec = pid.changeCount();

        let nsarray_ptr = pid.pasteboardItems();

        if nsarray_ptr.count() != 0 {
            for i in 0..NSArray::count(nsarray_ptr) {
                let raw_item_ptr = NSArray::objectAtIndex(nsarray_ptr, i);
                let itm = raw_item_ptr.stringForType(NSPasteboardTypeString);

                for u in 0..itm.len() {
                    let stri = itm.UTF8String();
                    println!("TEXT {:?}", *stri as u8 as char);
                }
            }
        }
    }
}

To everyone who's looked/commented on this so far thank you.

CodePudding user response:

After reading some tests provided by cocoa I figured out what I needed to do.

The code below prints the contents of the clipboard. Thanks to those who pointed me in the right direction.

use cocoa::appkit::{NSApp, NSPasteboard, NSPasteboardTypeString};
use cocoa::foundation::{NSArray, NSString};
use std::{str, slice};

fn main() {
    unsafe {
        let app = NSApp();
        let pid = NSPasteboard::generalPasteboard(app);
      
        let nsarray_ptr = pid.pasteboardItems();

        if nsarray_ptr.count() != 0 {
            for i in 0..NSArray::count(nsarray_ptr) {
                let raw_item_ptr = NSArray::objectAtIndex(nsarray_ptr, i);
                let itm = raw_item_ptr.stringForType(NSPasteboardTypeString);

                let stri = itm.UTF8String() as *const u8;
                let clipboard = str::from_utf8(slice::from_raw_parts(stri, itm.len()))
                .unwrap();
                
                println!("{}", clipboard);

            }
        }
    }
}
  • Related