如何使用AppleScript在文件中的特定位置写入文本?

时间:2019-09-26 21:25:55

标签: applescript

我想创建一个AppleScript,它:

  • 从Latex文件中剪切一些文本(我正在使用TexShop)

  • 提示用户输入新文件名

  • 将用文本/subfile{certain_path}

  • 剪切的文本替换
  • 在some_path创建一个新文件
  • 使用特定代码填充新文件
  • 然后将剪切文本粘贴到文件中的某个位置。

我相信我可以通过调用set theData to (the clipboard as text)从剪贴板中获取内容。 我知道如何用display dialogtext returned of等向用户询问文件名。 我现在知道如何通过从document 1获取路径,然后在其上调用container of来在同一目录中创建一个新文件。 而且我知道如何写入该文件。

我遇到的问题是:

  • 如果我想让AppleScript运行cut而不是用户,我是否必须使用GUI脚本命令tell application "System Events" to keystroke "x" using {command down},因为TexShop词典中没有“ cut text”命令?

  • 如何告诉AppleScript在剪切文本的确切位置写subfile{etc.}?如何告诉AppleScript将文本准确地粘贴到我想要的新文件的一部分中?

基本概念:

  1. 假定文本已被剪切(?):

    tell application "Finder"/"TexShop" to set thisWord to get clipboard as text
    
  2. 或者,使用AppleScript以某种方式“调用” CMD-X:

    tell application "System Events" to keystroke "x" using {command down}
    
  3. 获取文件名:

    set response to display dialog "Filename: " default answer ""
    set fileName to get text of response & ".tex"`
    
  4. 获取容器路径

    tell application "TexShop" to set thePath to get path of document 1
    tell application "Finder" to set parentPath to get container of thePath
    
  5. "\subfile{" & parentPath & fileName & "}"确切地是从哪里剪切出来的?怎么样???

  6. 制作新文件:

    tell application "Finder" to set newFile to ¬
        make new file at parentPath with properties {name:fileName}
    
  7. 写入文件:

    set preamble to "
    \documentclass{article}
    
    \begin{document}
    \begin{English}"
    
    & thisWord &
    
    "\end{English}
    
    \begin{Swedish}
    
    \end{Swedish}
    
    \end{document}"
    
    set openFile to open newFile with write permission
    write preamble to openFile
    
  8. 按上述方式组合字符串对我来说很好,但是在我已经写完以上内容之后知道如何将Word写入特定位置也很有趣。

我知道这里有多个问题,但是如果您能帮助我,我将无法表达我的感激之情。这个问题困扰了我太久了,我只是想知道如何使这种自​​动化发挥作用。谢谢。

0 个答案:

没有答案
相关问题