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

问题描述 投票:1回答:3

这笔交易是这样的:

我使用http://www.clipmenu.com/ ClipMenu有20个状态的剪贴板,因为我需要复制一些分开的txt文件的每一行。所以我打开txt文件,然后我通过命令+ shift +→然后命令+ c然后↑等等直到我到达顶部并且我将所有行复制并存储在ClipMenu的历史中。

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

非常感谢。

macos text applescript clipboard automator
3个回答
0
投票

我不知道如何使用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);
        }
    }
}

0
投票

尝试:

set fileText to read (choose file) as «class utf8»

set filePara to paragraphs of fileText
repeat with aPara in filePara
    set aPara to contents of aPara
    if aPara ≠ "" then set the clipboard to aPara
    end repeat

0
投票

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中。

© www.soinside.com 2019 - 2024. All rights reserved.