【问题标题】:Applescript: Split sentence at cursor - remove punctuation from selection, capitalize letter and insert periodApplescript:在光标处拆分句子 - 从选择中删除标点符号,大写字母并插入句点
【发布时间】:2014-05-11 03:12:22
【问题描述】:

我是 AppleScript 的新手,但我认为它可以自动处理我有时处理的一些小烦恼。

假设我写道:“我喜欢披萨,在上面放培根!” 但我决定我想把它分成两句话:“我喜欢披萨。把培根放进去!”

我希望能够选择字符串“, p” 使用键盘快捷键,让脚本删除所有标点符号、添加句点并将第一个字母大写。

我想出了如何设置系统偏好设置 > 键盘来运行自动化服务,运行 AppleScript,但我无法通过谷歌搜索足够的信息来从头开始创建脚本。

任何帮助或指导都会很棒!

【问题讨论】:

  • 如果您的选择仅包含“, p”,那么以通用方式获得它所属的完整句子将是非常重要的。下一个最好的事情是选择整个句子并让服务对其采取行动。这可以接受吗?

标签: macos applescript automator


【解决方案1】:

替代方案:由于 Automator 也支持其他解释器,因此无需严格使用 AppleScript。

这是一个替代解决方案,它使用Run Shell Script 操作将bash 与awk 结合,这样可以减少代码:

  • 创建一个 Automator 服务:
    • 在any application 中接收selected text
    • 已选中复选框Output replaces selected text
    • 包含Run Shell Script 操作,代码如下
  • 然后调用服务并选择整个句子(“我喜欢披萨,放培根!”)。
awk -F ', ' '{ 
    printf $1   # print the first "field"; lines without ", " only have 1 field
    for (i=2; i<=NF; ++i) { # all other fields, i.e., all ", "-separated clauses.
       # Join the remaining clauses with ". " with their 1st char. capitalized.
      printf ". " toupper(substr($i,1,1)) substr($i,2)
    }
    printf "\n" # terminate the output with a newline
  }'

警告:难以置信,awk(从 OS X 10.9.2 开始)不能正确处理外来字符 - 将它们传递给未经修改的作品,但 toupper() 和 tolower() 不会将它们识别为字母。因此,此解决方案 不能正确地用于外来字符的输入。必须大写;例如:"I like pizza, ṕut bacon on it!" 不会将 ṕ 转换为 Ṕ。

【讨论】:

    【解决方案2】:

    如果你:

    • 创建一个 Automator 服务:
      • 在any application 中接收selected text
      • 已选中复选框Output replaces selected text
      • 包含Run AppleScript 操作,代码如下
    • 然后调用服务并选择整个句子(“我喜欢披萨,放培根!”)

    它应该做你想做的。

    on run {input, parameters}
    
        # Initialize return variable.
        set res to ""
    
        # Split the selection into clauses by ", ".
        set {orgTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {", "}} #'
        set clauses to text items of item 1 of input
        set AppleScript's text item delimiters to orgTIDs #'
    
        # Join the clauses with ". ", capitalizing the first letter of all clauses but the first.
        repeat with clause in clauses
            if res = "" then
                set res to clause
            else
                set res to res & ". " & my capitalizeFirstChar(clause)
            end if
        end repeat
    
        # Return the result
        return res
    end run
    
    # Returns the specified string with its first character capitalized.
    on capitalizeFirstChar(s)
        set {orgTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {""}} #'
        set res to my toUpper((text item 1 of s) as text) & text items 2 thru -1 of s as text
        set AppleScript's text item delimiters to orgTIDs #'
        return res
    end capitalizeFirstChar
    
    # Converts the specified string (as a whole) to uppercase.
    on toUpper(s)
        tell AppleScript to return do shell script "export LANG='" & user locale of (system info) & ".UTF-8'; tr [:lower:] [:upper:] <<< " & quoted form of s
    end toUpper
    

    【讨论】:

    • @user2716144 很高兴;是的,将 AppleScript 用于此类任务并不好玩;但是,您不仅限于在 Automator 中使用 AppleScript:我已经发布了一个替代的、基于 shell 的解决方案(使用 bash 和 awk),它要短得多(注意警告,不过)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 2023-02-10
    相关资源
    最近更新 更多