使用 Automator.app 逐行读取文本文件 [英] Read a text file with Automator.app line by line

查看:53
本文介绍了使用 Automator.app 逐行读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编码新手,所以请耐心等待.

I'm a novice at coding so please be patient with me.

我已经使用 Automator (OSX) 创建了一个工作流,它工作正常.我唯一的问题是我希望它在多个输入(即批处理)上运行.我已经插入了循环操作,但我遇到的问题是每次都更改初始输入.

I've created a Workflow with Automator (OSX) which works fine. The only issue I have is that I want it to run on a number of inputs (that is as a batch). I've inserted the Loop action but the problem I'm having is about changing the initial input each time.

我想使用applescript 每次自动插入初始输入.

I would like to use an applescript to automate the insertion of the initial input each time.

我有一个带有 URL 的 TXT 文件.使用苹果脚本,我想将 URL(或一行文本)复制到剪贴板.在下一次迭代中,我想复制下一个 URL(或一行文本).

I have a TXT file with URLs. With an apple script, I'd like to copy a URL (or a line of text) to clipboard. In the next iteration I'd like to copy the next URL (or line of text).

有人可以帮忙吗?

谢谢!!

推荐答案

您可以创建一个循环工作流(称为 LinesToClipboard.workflow)

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

  • 从文本文件(不是 rtf 或 doc)中获取一行
  • 将该行复制到剪贴板
  • 运行您当前的工作流程
  • 再次循环到下一行

工作流程:

  • 创建新的自动化工作流程
  • 创建一个变量
    • 在底部找到显示或隐藏工作流变量列表"图标并显示工作流变量(空)
    • 右键单击并新建变量..."
    • 将变量命名为LineNumber"
    • 获取变量的值 (LineNumber)
    • 运行 Shell 脚本
    • 外壳:/bin/bash
    • 重要:将Pass input改为as arguments
    • 添加以下内容(准确复制,包括所有引号等):
    • 在脚本的内容中,将/etc/passwd改为你的文件名的完整路径,比如/Users/myname/Documents/myfile.txt莉>
    • 在此操作结束时,剪贴板将包含文件中的一行
    • 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
    

    • 设置变量的值 (LineNumber)
    • Run Workflow - 添加您当前的工作流程(或ShowClipboard.workflow" - 见下文)
      • 应该检查等待工作流完成
      • 重要输出菜单应该是:返回动作输入"
        • 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"
            • 添加您的计数...

            为了测试,您可以创建另一个名为 ShowClipboard.workflow 的工作流,其中包含一个内容:

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

            • 获取剪贴板的内容
            • 设置变量的值 (clipval)
            • 请求确认(并将(clipval)拖到Message字段)
            • Get Contents of Cliboard
            • Set Value of Variable (clipval)
            • Ask for confirmation (and drag the (clipval) to the Message field)

            运行第一个工作流.

            截图(肯定的):)

            第二个工作流程(用于测试)

            The second workflow (for testing)

            这篇关于使用 Automator.app 逐行读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆