You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MacOS剪贴板是否有路径?Python如何读取剪贴板内容?

解决方案:访问MacOS剪贴板的Python方法

一、可读取剪贴板内容的Python模块

你提到的pyperclip和clipboard其实都支持读取剪贴板内容,只是需要用到对应的方法:

  • pyperclip
    通过paste()方法直接读取当前剪贴板的文本内容:

    import pyperclip
    current_clip_content = pyperclip.paste()
    print(current_clip_content)
    
  • clipboard
    同样可以用paste()获取内容:

    import clipboard
    print(clipboard.paste())
    
  • pyobjc(MacOS原生功能适配)
    如果需要处理文本以外的内容(比如图片、富文本),可以用这个模块调用MacOS的Cocoa框架API:

    from AppKit import NSPasteboard, NSStringPboardType
    
    pasteboard = NSPasteboard.generalPasteboard()
    text_content = pasteboard.stringForType_(NSStringPboardType)
    print(text_content)
    

二、MacOS剪贴板的文件路径说明

MacOS的剪贴板并非以普通本地文件形式存储,它由系统pboard服务管理,数据存放在内存中,没有直接可读取的文件路径,必须通过系统API或封装好的Python模块进行访问。

三、剪贴板历史的免费获取方案

MacOS系统自带的剪贴板历史属于隐私数据,没有公开API直接读取。免费实现历史记录功能可以自己做监控:

用Python定时读取剪贴板内容,对比内容变化来记录历史:

import pyperclip
import time

last_content = ""
clip_history = []

while True:
    current_content = pyperclip.paste()
    if current_content != last_content and current_content not in clip_history:
        clip_history.append(current_content)
        print(f"新增历史记录: {current_content}")
        last_content = current_content
    time.sleep(1)

这个脚本会持续监控剪贴板,将新内容存入clip_history列表,你可以根据需求把列表写入本地文件实现持久化保存。

内容的提问来源于stack exchange,提问作者Daksh Dubey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 22:20:47