如何通过 AppleScript 从剪贴板复制所有信息

How to copy all the info from clipboard via AppleScript

事情是这样的:
我在 Automator 中使用 AppleScript 来获取剪贴板值,当然它可以工作,但是当我想获取多个分隔值时,它总是 returns 我只有一个值在顶部。
这是步骤:

  1. 在 Automator 中,导入多个 "Get Value of Variable" 动作,然后 在这些动作中,我会设置多个值,所有这些值 是电子邮件格式
  2. 导入一个名为“Ask For Confirmation”的动作,没有这个动作, 我无法将多个值传递给下一个操作“从列表中选择”(我 不知道为什么,但它有效)
  3. 导入名为“从列表中选择”的操作,让用户选择 我在此 Automator 应用程序中预设的电子邮件值
  4. 导入另一个名为“设置变量值”的动作来获取 用户选择的值
  5. 导入名为“复制到剪贴板”的操作以将这些值复制到 剪贴板
  6. 导入名为“运行 AppleScript”的动作,这是我的代码:

on run {input, parameters}
    --get the clipboard info
    set Storage to get the clipboard
    display dialog Storage
    return input
end run

我试图复制一些 text_1, text_2 ... 手动(command+c, command+v)然后 运行 我的 AppleScript,然后它变成了结果是我真正想要的:

这是我的脚本编辑器代码:

我不得不说,由于某些限制我只能使用Automator和AppleScript,请问有什么解决方案或建议吗? 这是 "Get Value of Variable" 图片 Get Value of Variable

可能的解释:

我认为这是 Automator Copy To Clipbard 操作或 AppleScript 中的错误。 Automator 动作通常用 Objective-C 编写,它有一些 AppleScript 没有的数据类型。看起来 Automator 操作将 array 复制到剪贴板,这是您可以使用 Objective-C 执行的操作,但不能使用 AppleScript .

我的感觉是 AppleScript 是这里的错误实体,因为操作正在执行它的意图,并且在 Automator 上下文中,它不会构成将剪贴板的数据保存为数组类型时出现问题。 AppleScript 在其剪贴板数据处理的实现中可能没有满足这一点,并且在将数组或列表强制转换为纯文本方面做得很差,正如您所说的那样,它只包含数组的第一个元素。

解决方案:

1。使用 do shell script 命令

而不是:

set Storage to get the clipboard

尝试:

set Storage to do shell script "pbpaste"

2。使用 AppleScriptObjC

由于 Automator 操作可能是用 ObjC 编写的,因此可以合理地假设使用 AppleScriptObjC 将使我们能够访问必要的数据类型。

用这个替换你的整个 AppleScript:

    use framework "Foundation"
    use scripting additions

    set Storage to (current application's NSPasteboard's generalPasteboard's ¬
        stringForType:(current application's NSPasteboardTypeString)) ¬
        as text

    display alert Storage

3。通过input变量

访问数据

Run AppleScript 中的操作 Automator 获取前一个操作的结果并将其存储在附加到on run {input, parameters} handler,即input(可以忽略parameters)。

目前,您的工作流实际上将剪贴板的内容(Copy To Clipboard 操作的输出)直接发送到您的 AppleScript 的 input 变量.

因此,您可以将整个 AppleScript 替换为:

    on run {input, parameters}

        set the text item delimiters to linefeed
        set Storage to the input as text

        display dialog Storage

    end run

这些解决方案中的任何一种都应该有效,因此只需选择您喜欢的方法即可。 数字 3 可能在您当前的设置中最有意义,也是最简单的。