V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
xurunfei
V2EX  ›  macOS

求 macos 复制文件到剪贴板命令

  •  
  •   xurunfei · Aug 23, 2020 · 6542 views
    This topic created in 2076 days ago, the information mentioned may be changed or developed.

    注意是复制文件到剪贴板,不是文件内容,目前找到好多命令都是复制内容的,比如 pbcopy 命令范围 shell osascript python 都行,或者有更好的替代方法

    Supplement 1  ·  Aug 23, 2020

    我描述一下mac 下复制一个文件,我用 alfred 剪贴板访问的时候是这样的 图片 而 pbcopy 得到的是a.txt 的内容并不是文件 我需要图中这种形式,就是命令复制后,到一个文件夹直接能粘贴这个文件,而不是仅仅复制了文件的内容

    Supplement 2  ·  Aug 23, 2020
    或者说的最简单的我需要一个命令实现 command+c 的功能
    27 replies    2024-12-25 19:33:46 +08:00
    lxk11153
        1
    lxk11153  
       Aug 23, 2020
    蹲一个~
    Kobayashi
        2
    Kobayashi  
       Aug 23, 2020 via Android
    Finder 不知道有没有插件。我用 Forklift,自带此功能,也可以 Forklift 通过自定义命令实现。终端用 ranger 实现。
    ihwbunny
        3
    ihwbunny  
       Aug 23, 2020
    pbcopy | pbpaste
    ihwbunny
        4
    ihwbunny  
       Aug 23, 2020
    什么叫复制文件,而不复制内容?是文件全路径?
    xgQikk
        5
    xgQikk  
       Aug 23, 2020 via iPhone
    你在说什么东西……
    minamike
        6
    minamike  
       Aug 23, 2020
    tell application "Finder" to set theItems to selection
    repeat with itemRef in theItems
    set the clipboard to (POSIX file (POSIX path of (itemRef as string)))
    end repeat
    Tink
        7
    Tink  
    PRO
       Aug 23, 2020 via Android
    文件路径?
    goldenlove
        8
    goldenlove  
       Aug 23, 2020
    直接 cp 或 scp 不香么?一定要走剪贴板?主要做啥用?
    tyhunter
        9
    tyhunter  
       Aug 23, 2020
    临时收集文件吗?考虑下 Yoink 这类
    lxk11153
        10
    lxk11153  
       Aug 23, 2020
    @goldenlove #8 可能是类似在 Finder 里先 cmd+c,然后再另一个程序里 cmd+v 吧。
    xgQikk
        11
    xgQikk  
       Aug 23, 2020 via iPhone   ❤️ 1
    我还是没听懂他在说什么 解释了这么多 还是没讲清
    littlewing
        12
    littlewing  
       Aug 23, 2020
    打开 iTerm2,执行 cp
    lxk11153
        13
    lxk11153  
       Aug 23, 2020
    @xgQikk #11 我猜的:
    比如 第一步 在 Finder 里对文件 cmd+c,第二步 在 另一个程序里 cmd+v
    改为 第一步 用命令行完成,第二步 同上
    Mysqto
        14
    Mysqto  
       Aug 23, 2020   ❤️ 1
    xurunfei
        15
    xurunfei  
    OP
       Aug 23, 2020   ❤️ 1
    @Mysqto 谢谢,就是要的这个 ,也谢谢大家的回复 ,需要的脚本如下
    ```
    #!/usr/bin/osascript
    on run args
    set the clipboard to POSIX file (first item of args)
    end
    ```
    ladychili
        16
    ladychili  
       Aug 25, 2020
    hhhhhh
    和 lz 的需求刚好逆向,想知道如何通过一个脚本 /命令获取剪贴板的图片(想做个 Alfred workflow 快速对剪贴板里的图片 google 以图搜图
    xurunfei
        17
    xurunfei  
    OP
       Aug 25, 2020
    @ladychili 我这边整理了一点你看看有用没
    ```
    # 复制文件
    osascript -e 'set the clipboard to POSIX file ("/Users/xurunfei/Documents/tmp/a.png")'
    #从剪贴板获取数据然后当做文件路径复制回剪贴板, 这里只是文本,没有试过图片
    osascript -e 'set theData to (the clipboard as text)'
    #另外 python 可以用下面这个从剪贴板读取图片
    img = ImageGrab.grabclipboard()
    ```
    dovme
        18
    dovme  
       Aug 25, 2020
    你可能需要的是这个软件 paste
    https://i.loli.net/2020/08/25/mejKfRwQrY3WL6c.png
    xurunfei
        19
    xurunfei  
    OP
       Aug 26, 2020
    @dovme 不是,这个功能我的软件自带了,我用的 alfred 的剪贴板,东西已经找到了,可以看 15 楼我的回复
    ivyliner
        20
    ivyliner  
       Sep 15, 2020   ❤️ 1
    @xurunfei 我写了一个命令行工具, 解决 #15 楼的 Applescript 的缺点
    1. 不支持批量
    2. 不支持文件相对路径

    项目地址: https://github.com/yujinqiu/pbadd
    xurunfei
        21
    xurunfei  
    OP
       Sep 15, 2020
    @ivyliner 好的谢谢,我主要是用在其他软件界面配合 touchbar 和 BetterTouchTool.app 的 applescript 使用,这样我能直接点击 touchbar 按钮直接复制软件对应的文件
    yimq
        22
    yimq  
       Mar 10, 2021
    在 big sur 15 楼的脚本好像有权限问题,可以试试这个
    qq() {
    file_real_path=$(realpath $1)
    osascript -e 'tell app "Finder" to set the clipboard to ( POSIX file '\"$file_real_path\"' )'
    open -a WeChat
    }
    Neoth
        23
    Neoth  
       May 23, 2021
    @yimq 是的
    @Mysqto 给出这个 stack exchange 上的答案有问题,需要先取绝对路径
    Mysqto
        24
    Mysqto  
       May 24, 2021
    @Neoth https://gist.github.com/mysqto/e3826dbe2772acf1af8e74b0558157c0

    ```
    #!/usr/bin/osascript
    use framework "Appkit"

    property this : a reference to current application
    property NSFileManager : a reference to NSFileManager of this
    property NSImage : a reference to NSImage of this
    property NSMutableArray : a reference to NSMutableArray of this
    property NSPasteboard : a reference to NSPasteboard of this
    property NSString : a reference to NSString of this
    property NSURL : a reference to NSURL of this

    property pb : missing value

    on run argv
    init()
    clearClipboard()
    addToClipboard(argv)
    end run


    to init()
    set pb to NSPasteboard's generalPasteboard()
    end init

    to clearClipboard()
    if pb = missing value then init()
    pb's clearContents()
    end clearClipboard

    to addToClipboard(fs)
    local fs

    set fURLs to NSMutableArray's array()
    set FileManager to NSFileManager's defaultManager()

    repeat with f in fs
    if f's class = alias then set f to f's POSIX path
    set pwd to (FileManager's currentDirectoryPath)
    set fn to (NSString's stringWithString:f)
    set fp to (pwd's stringByAppendingPathComponent:fn)'s stringByStandardizingPath()
    if (FileManager's fileExistsAtPath:fp) then
    (fURLs's addObject:(NSURL's fileURLWithPath:fp))
    end if
    end repeat

    if pb = missing value then init()
    pb's writeObjects:fURLs
    end addToClipboard
    ```
    klovej
        26
    klovej  
       Jan 30, 2024   ❤️ 1
    @ivyliner 好用,感谢工作
    mayooot
        27
    mayooot  
       Dec 25, 2024
    可以试试这个
    用 golang 调用 Object-C 实现的
    https://github.com/mayooot/ClipChirp
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   892 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 482ms · UTC 19:24 · PVG 03:24 · LAX 12:24 · JFK 15:24
    ♥ Do have faith in what you're doing.