简体   繁体   中英

How to copy every line of text on a text file with Automator on OSX?

The deal is this:

I use http://www.clipmenu.com/ ClipMenu to have 20 states of the clipboard, because i need to copy each line of some txt file separated. So i open the txt file, and i go through every line hitting command + shift + then command + c then and so on until i reach the top and i have all the lines copied and stored in the history of ClipMenu.

My question is, is there a way to make a service or script that copies every single line in an automated way? i think i could make a script that repeat those keystrokes until it reaches the top of the txt file but i have no idea how to make it so.

Thanks a lot.

I do not know how to do this using Automator, but using mono [MonoMac] it is very simple:

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);
        }
    }
}

Try:

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

ClipMenu seems to ignore "transient" clipboards, so you also need a delay between the copy actions:

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

Or using UI scripting:

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

The key codes are listed in Events.h.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM