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

2020-08-23 11:15:16 +08:00
 xurunfei

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

4785 次点击
所在节点    macOS
26 条回复
xurunfei
2020-09-15 14:13:48 +08:00
@ivyliner 好的谢谢,我主要是用在其他软件界面配合 touchbar 和 BetterTouchTool.app 的 applescript 使用,这样我能直接点击 touchbar 按钮直接复制软件对应的文件
yimq
2021-03-10 17:47:07 +08:00
在 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
2021-05-23 07:52:01 +08:00
@yimq 是的
@Mysqto 给出这个 stack exchange 上的答案有问题,需要先取绝对路径
Mysqto
2021-05-24 10:29:18 +08:00
@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
```
DifferentDrum
2022-02-14 14:59:10 +08:00
klovej
106 天前
@ivyliner 好用,感谢工作

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/700647

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX