Macos 如何在OSX上使用Automator复制文本文件上的每一行文本?

Macos 如何在OSX上使用Automator复制文本文件上的每一行文本?,macos,text,applescript,clipboard,automator,Macos,Text,Applescript,Clipboard,Automator,交易是这样的: 我使用ClipMenu来创建剪贴板的20个状态,因为我需要分别复制某个txt文件的每一行。所以我打开了txt文件,我检查了每一行,点击command+shift+→ 然后命令+c然后↑ 依此类推,直到到达顶部,我将所有行复制并存储在ClipMenu的历史记录中 我的问题是,有没有一种方法可以让服务或脚本自动复制每一行?我想我可以制作一个脚本,重复这些击键,直到它到达txt文件的顶部,但我不知道如何做到这一点 非常感谢。我不知道如何使用Automator进行此操作,但使用mono[

交易是这样的:

我使用ClipMenu来创建剪贴板的20个状态,因为我需要分别复制某个txt文件的每一行。所以我打开了txt文件,我检查了每一行,点击command+shift+→ 然后命令+c然后↑ 依此类推,直到到达顶部,我将所有行复制并存储在ClipMenu的历史记录中

我的问题是,有没有一种方法可以让服务或脚本自动复制每一行?我想我可以制作一个脚本,重复这些击键,直到它到达txt文件的顶部,但我不知道如何做到这一点


非常感谢。

我不知道如何使用Automator进行此操作,但使用mono[MonoMac]非常简单:

using (System.IO.FileStream file = new System.IO.FileStream("path", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)) {
    using (System.IO.StreamReader reader = new System.IO.StreamReader(file)) {
        while (!reader.EndOfStream) {
            String line = reader.ReadLine ();
            System.Windows.Forms.Clipboard.SetText(line);
        }
    }
}
尝试:

ClipMenu似乎忽略了“临时”剪贴板,因此在复制操作之间还需要延迟:

read POSIX file "/Users/username/Documents/test.txt" as «class utf8»
repeat with p in reverse of paragraphs of result
    if contents of p is not "" then set the clipboard to contents of p
    delay 1
end repeat
或使用UI脚本:

delay 1
tell application "TextEdit"
    activate
    set n to number of paragraphs of document 1
end tell
tell application "System Events"
    key code {125, 123} using command down
    repeat n times
        key code 124 using {shift down, command down}
        keystroke "c" using command down
        key code 126
        delay 1
    end repeat
end tell

关键代码列在Events.h.

中,然后第二个代码工作,但每个文本文件上的行数不同。我可以将“重复”设置为“重复”,直到到达第一行吗?谢谢。我编辑了答案。第一个脚本也适用于我,但您是否正确指定了路径?现在它可以工作,但它不会复制第一行。我怎样才能解决这个问题?。谢谢。第二个脚本用于不以空行结尾的文件。不过,您可以添加
将n设置为n+1
。文件不会以空行结尾,只是第一行(包含文本)不会被复制。不过我要试试n+1,谢谢。
delay 1
tell application "TextEdit"
    activate
    set n to number of paragraphs of document 1
end tell
tell application "System Events"
    key code {125, 123} using command down
    repeat n times
        key code 124 using {shift down, command down}
        keystroke "c" using command down
        key code 126
        delay 1
    end repeat
end tell