为什么复制多个文件使用AppleScript不能粘贴到第三方应用程序?

发布于 2025-02-08 15:31:25 字数 1694 浏览 1 评论 0原文

我不能粘贴到应用程序,但是在Finder中它可以正常工作。

我还尝试使用Swift + Nspasteboard,似乎是同样的问题。

我需要做什么才能使其与Finder完全一样?

无论是使用AppleScript还是Swift,我都会接受。

#!/usr/bin/osascript

use framework "Appkit"
use scripting additions
use Finder : application "Finder"

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 input
    if input's class = script then set input to ¬
        Finder's selection as alias list

    init()
    clearClipboard()
    addToClipboard(input)
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
        set f to do shell script "realpath '" & f & "'"
        if f's class = alias then set f to f's POSIX path
        set fp to (NSString's stringWithString:f)'s ¬
            stringByStandardizingPath()
        if (FileManager's fileExistsAtPath:fp) then ¬
            (fURLs's addObject:(NSURL's fileURLWithPath:fp))
    end repeat

    if pb = missing value then init()
    pb's writeObjects:fURLs
    #pb's setPropertyList:(fURLs, NSPasteboard's PasteboardType)
end addToClipboard

I cannot paste to application, but in finder it works fine.

I also try use swift + NSPasteboard, seems to be the same problem.

What do I need to do to make it work totally same like finder?

Whether to use AppleScript or swift, I will take that.

#!/usr/bin/osascript

use framework "Appkit"
use scripting additions
use Finder : application "Finder"

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 input
    if input's class = script then set input to ¬
        Finder's selection as alias list

    init()
    clearClipboard()
    addToClipboard(input)
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
        set f to do shell script "realpath '" & f & "'"
        if f's class = alias then set f to f's POSIX path
        set fp to (NSString's stringWithString:f)'s ¬
            stringByStandardizingPath()
        if (FileManager's fileExistsAtPath:fp) then ¬
            (fURLs's addObject:(NSURL's fileURLWithPath:fp))
    end repeat

    if pb = missing value then init()
    pb's writeObjects:fURLs
    #pb's setPropertyList:(fURLs, NSPasteboard's PasteboardType)
end addToClipboard

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文