尝试读取MacOS剪贴板内容

发布于 2025-02-11 23:42:38 字数 2617 浏览 2 评论 0原文

在学习Rust的冒险中,我决定尝试将其打印到剪贴板的CLI内容。我以前在Swift中做到了这一点,所以认为我在Rust中会有很多问题。

但是,我很难打印返回的nsarray的内容。我花了几个小时的时间来玩不同的功能,但并没有取得太大进展。

我有工作的Swift代码:

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);
}

以下是锈蚀代码:

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));
    }
}

上面的代码产生:*< nspasteboardItem:0x6000021a3de0>*

edit:

我取得了一点进步,但陷入了最后一点。我设法从剪贴板中获取了第一个UTF8字符。

我遇到的问题是,如果我复制文本:world系统将为单词长度循环循环,但只会打印第一个字母,在这种情况下,w。下面的输出:

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

我试图抬头的位是如何转到下一个i8。我似乎找不到一种指向下一个i8的方法。

NSString函数utf8String()返回*const i8。我正在用一个人走文字来挠头。

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);
                }
            }
        }
    }
}

对于到目前为止对此进行/评论的每个人,谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2025-02-18 23:42:38

阅读了可可(Cocoa)提供的一些测试后,我弄清楚了我需要做什么。

下面的代码打印剪贴板的内容。感谢那些将我指向正确方向的人。

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);

            }
        }
    }
}

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);

            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文