Read a text file with Automator.app line by line

戏子无情 提交于 2019-12-01 10:43:28

You can create one looping workflow (called as LinesToClipboard.workflow) what will

  • get a line from an text file (not rtf, or doc)
  • copy the line to clipboard
  • run your current workflow
  • loop again for the next line

The workflow:

  • Create new automator workflow
  • create a variable
    • at the bottom find the icon "Show or hide the workflow variables list" and show the workflow wariables (empty)
    • right click and "New variable..."
    • name the variable as "LineNumber"
  • add actions:
    • Get Value of Variable (LineNumber)
    • Run Shell Script
    • shell: /bin/bash
    • important: change the Pass input to as arguments
    • add the following content (copy exactly, with all quotes and such):
    • in the content of script, change the /etc/passwd to the full path of your filename, like /Users/myname/Documents/myfile.txt
    • at the end of this action the clipboard will contain one line from the file
linenum=${1:-0}
filename="/etc/passwd" # full path of your text-filename
let linenum++
sed -n "${linenum}p" < "$filename" | pbcopy
echo $linenum
  • Set Value of Variable (LineNumber)
  • Run Workflow - add your current workflow (or the "ShowClipboard.workflow" - see bellow)
    • the Wait for workflow to finish should be checked
    • important The Output menu should be: "Return action input"
  • Loop
    • add your count...
  • Run Shell Script (Ignore this action's input), content one line: echo 0 (This will reset the variable LineNumber to zero, when the loop ends)
  • Set Value of Variable (LineNumber)

For testing, you can create another workflow, called ShowClipboard.workflow, with an content:

  • Get Contents of Cliboard
  • Set Value of Variable (clipval)
  • Ask for confirmation (and drag the (clipval) to the Message field)

Run the first workflow.

Screenshots (for sure) :)

The second workflow (for testing)

You don't need AppleScript to get the URLs but can directly extract them with Automator by using a shell task. After using the task that's getting contents of a folder (this is a Finder task in Automator), add a shell task as the next task. Make sure you select that the input is send as arguments instead of sending it to stdin. When you have done that you only need something like one of the following shell scripts.

cat $@ | egrep -io '\S?(http|https|ftp|afp|smb|mailto|webcal):\S+''

It first read all the files using cat. The $@ is a shell variable that contains the arguments collected by the previous task: A list of paths to all files of batch folder. We pipe them to egrep will which will only output the URL filtered by their schemes. If you want to support any scheme (official and unofficial schemes):

cat $@ | egrep -io '\S?[A-Z][A-Z0-9+-.]+:\S+'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!