用于在引号中粘贴纯文本的自动化服务的 Apple 脚本

Apple script for an automator service for pasting plain text in quotation marks

我是 MacOS 的新手,但我找不到任何解决方案,所以这就是我需要的: 我在写作时使用了很多 pdf-s,我需要使用很多引文。 我需要具有以下功能的 macos sierra 自动化服务: 当我从 PDF 复制文本时,我需要将其粘贴到文字处理器(主要是抄写员,但最好也粘贴到其他任何地方)。 诀窍:我需要将其粘贴为纯文本以匹配接收文档的格式。我还需要摆脱所有换行符。我需要在粘贴文本的开头和结尾加上引号。

感谢任何建议!

这是关于 how to make a systemwide service 使用 Automator 的文档。您需要阅读并按照其说明进行操作,该说明通过一个 step-by-step 示例说明如何获取一些选定的文本并将其大小写更改为大写,或者什么不是。

因此,该方法与您实现结果所需采用的方法不会相去甚远。

您提到使用 Scrivener,但隐含地假设我们也都使用它。我个人不这样做,所以我不知道它提供了什么 Automator 操作(如果有的话),也不知道它是否是 AppleScriptable。因此,我能为您提供的帮助已经有限,因此我将在本例中专注于使用另一个应用程序。

When I copy a text from PDF I need to paste it to a word processor

因此,您的 Automator 服务将采用选定的文本,这些文本可以来自任何应用程序 或限于您选择的 特定应用程序 ,例如 Preview 或其他一些 PDF 文档查看器。

大部分 Automator 工作流程将被编写成脚本,我主要使用 AppleScript 来完成。所以我会插入一个 运行 AppleScript 动作作为第一个动作,它将接收选定的文本。

这将通过 run 处理程序的变量 input 发送并存储:

    on run {input, parameters}

I need to paste it as plain text

所以脚本的第一行可能是:

        set input to input as text

这可能为时过早,而且几乎肯定是多余的,因为稍后会对变量的内容进行操作。但是,明确声明它只是为了当人们自己读回脚本时也很有用。

I need the line breaks to be replaced with a space

即没有段落,只有一个散文,就像你的问题一样。

        set the text item delimiters to {space, linefeed, return}
        set input to the text items of input as text

I need quotation marks at the beginning and at the end of the pasted text.

        set input to [quote, input, quote] as text

目前的工作流程如下:

从这里开始,有几个关于您可以做什么的选项:

① 将重新格式化的文本放到剪贴板上,正如您看到的那样,然后允许用户简单地移动到他们想要的任何应用程序并自己粘贴文本。如果这是您的选择,那么您就完成了,上面的屏幕截图是您完成的服务,一旦保存,您可以从您选择的任何应用程序(或任何应用程序,如果您离开它)的服务菜单中访问它。

但是,如果你想继续...

② 获取打开可编写脚本的应用程序并将文档内容设置为 input 变量内容的工作流。我将使用 Apple 的 TextEdit.

进行演示

③ 获取打开任何常规 (non-scriptable) 应用程序的工作流程,并使用一种相当不优雅的技术以有限的方式对应用程序的某些元素施加控制。这比 ② 稍微不稳健,但如果它在您将始终使用的单个应用程序上经过良好测试,没有理由不可靠。


插入可编写脚本的应用程序,例如TextEdit.

结果比我希望的要复杂一些。虽然可编写脚本确实使它变得容易得多,但 TextEdit 甚至 Apple 的 Pages 等缺少特定的 AppleScript 属性,这意味着常规脚本让我 half-way 到达了我的目标,所以不得不使用我提到的一些不优雅的方式来完成剩下的事情。因此,我不会让您厌烦它的工作原理。但这是负责将突出显示的输入文本输入 TextEdit:

的脚本部分
    script TextEdit
        use application "System Events"
        use T : application "TextEdit"
        
        property parent : this
        
        
        to receiveTheText:(plainText as string)
            activate T
            
            repeat until name of processes ¬
                contains "TextEdit"
                delay 1
            end repeat
            
            if not (exists document 1 of T) then
                tell T to set D to make new document
            else
                set D to T's first document
            end if
            
            set [a, b] to the value of ¬
                attribute "AXSelectedTextRange" of ¬
                text area 1 of ¬
                scroll area 1 of ¬
                window 1 of ¬
                process "TextEdit"
            
            set character a of D's text to plainText & linefeed
        end receiveTheText:
    end script

然而,它的效果出奇地好,保留了 TextEdit 文档的本地格式,并将传入的文本与本地样式相​​匹配

代书人

因此,我继续下载了 Scrivener 的试用版。遗憾的是,它不是 AppleScript-able,因此这在某些方面限制了我们。即,虽然 TextEdit 不必打开,因为 Automator 服务可以打开应用程序并在需要时创建新文档,Scrivener 确实需要一个准备好接收文本的打开项目。我想这不会造成太大的不便,但是从脚本 point-of-view 来看,这是一个更难编写的脚本,如果 Scrivener[=108=,它有可能停止正常工作] 决定更改他们的程序。

这是 Scrivener 的脚本:

    script Scrivener
        use scripting additions
        
        property parent : this
        
        
        to receiveTheText:(plainText as string)
            tell application "System Events" to ¬
                if not (exists process "Scrivener") then ¬
                    return display alert ¬
                        "Application not running." & ¬
                        linefeed & linefeed & ¬
                        "When using this service with Scrivener, an open " & ¬
                        "project is required into which the text can be " & ¬
                        "inserted.  AppleScript is unable to create a project " & ¬
                        "for you." as critical
            
            tell application "Scrivener" to activate
            
            tell application "System Events"
                
                tell process "Scrivener" to set _P to ¬
                    a reference to (first window whose ¬
                        name of attributes contains "AXDocument" and ¬
                        value of attribute "AXDocument" contains ".scriv")
                
                if not (exists _P) then ¬
                    return display alert ¬
                        "No open documents." & ¬
                        linefeed & linefeed & ¬
                        "Please create a project and try again." as critical
                
                repeat until (count _P) = 0
                    set _T to a reference to (UI elements of _P ¬
                        whose role is "AXTextArea")
                    if (count _T) ≠ 0 then exit repeat
                    set _P to a reference to UI elements of _P
                end repeat
                
                set [_T] to _T --> The text area
                
                tell _T to set value of attribute "AXSelectedText" to plainText
                
            end tell
        end receiveTheText:
    end script

终于...

最后,您需要整个脚本。我在上面粘贴的各个组件 scriptlet 对我自己来说作用不大。您需要使用我之前向您展示的 运行 AppleScript 操作将整个脚本复制并粘贴到 Automator 工作流程中。

您可能只需要一个可选变量ge,它位于脚本的最底部:您会看到两行 almost-identical,其中一行被前面的 # 注释掉了。这些指示服务是将文本传递给 TextEdit 还是传递给 Scrivener。目前,它将转到 Scrivener。如果您想尝试使用 TextEdit.

,只需将 # 符号切换到另一行
    property parent : AppleScript
    property this : me
    --------------------------------------------------------------------------------
    script TextEdit
        use application "System Events"
        use T : application "TextEdit"
        
        property parent : this
        
        
        to receiveTheText:(plainText as string)
            activate T
            
            repeat until name of processes ¬
                contains "TextEdit"
                delay 1
            end repeat
            
            if not (exists document 1 of T) then
                tell T to set D to make new document
            else
                set D to T's first document
            end if
            
            set [a, b] to the value of ¬
                attribute "AXSelectedTextRange" of ¬
                text area 1 of ¬
                scroll area 1 of ¬
                window 1 of ¬
                process "TextEdit"
            
            set character a of D's text to plainText & linefeed
        end receiveTheText:
    end script

    script Scrivener
        use scripting additions
        
        property parent : this
        
        
        to receiveTheText:(plainText as string)
            tell application "System Events" to ¬
                if not (exists process "Scrivener") then ¬
                    return display alert ¬
                        "Application not running." & ¬
                        linefeed & linefeed & ¬
                        "When using this service with Scrivener, an open " & ¬
                        "project is required into which the text can be " & ¬
                        "inserted.  AppleScript is unable to create a project " & ¬
                        "for you." as critical
            
            tell application "Scrivener" to activate
            
            tell application "System Events"
                
                tell process "Scrivener" to set _P to ¬
                    a reference to (first window whose ¬
                        name of attributes contains "AXDocument" and ¬
                        value of attribute "AXDocument" contains ".scriv")
                
                if not (exists _P) then ¬
                    return display alert ¬
                        "No open documents." & ¬
                        linefeed & linefeed & ¬
                        "Please create a project and try again." as critical
                
                repeat until (count _P) = 0
                    set _T to a reference to (UI elements of _P ¬
                        whose role is "AXTextArea")
                    if (count _T) ≠ 0 then exit repeat
                    set _P to a reference to UI elements of _P
                end repeat
                
                set [_T] to _T --> The text area
                
                tell _T to set value of attribute "AXSelectedText" to plainText
                
            end tell
        end receiveTheText:
    end script
    --------------------------------------------------------------------------------
    on run {input, parameters}
        set input to input as text
        
        set the text item delimiters to {space, linefeed, return}
        set input to the text items of input as text
        set input to [quote, input, quote] as text
        
        # tell TextEdit to receiveTheText:input
        tell Scrivener to receiveTheText:input
    end run
    -----------------------------------------------------------------------------END