如何在 AppleScript 和 BBEdit 中剪切文本?

How can I cut text in AppleScript and BBEdit?

在给定以下文本的文本文件中:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

使用 AppleScript 和 BBEdit 我希望能够缩短日期 Sunday 并将其移动到 Monday 之前,但是当我引用 BBEdit 字典时,我看到了 cut:

当我尝试剪切文本并将其添加到该行之前时出现错误:

BBEdit got an error: "Sunday" doesn’t understand the “cut” message.

代码:

tell application "BBEdit"
    set theFile to "foobar.txt"
    select insertion point before first character of text document theFile
    repeat
        set theWeekend to find "(?<=Saturday\n)Sunday" searching in text 1 of text document theFile options {search mode:grep} with selecting match
        if theWeekend is not found then exit repeat
        set theWeekend to found text of theWeekend
        select insertion point before first character of text document theFile
        set beforeMon to find "(?!<=Sunday\n)Monday" searching in text 1 of text document theFile options {search mode:grep} with selecting match
        if beforeMon is found then
            set beforeMon to found text of beforeMon
            set theLine to startLine of selection
            set addDay to select insertion point before first character of line theLine of text document theFile
            set thePull to cut theWeekend ## where error occurs
            set before line theLine of text of text document theFile to (theWeekend & return)
        end if
    end repeat
end tell

如果我注释掉 set thePull to cut theWeekend 脚本会连续循环并在 Monday 之前放置 Sunday 但我无法中断循环,因为我的 grep 变量 theWeekend 仍然是 false.

其他失败的尝试:

 cut selection of (theWeekend)
 cut theWeekend
 cut theWeekend of selection
 cut selection of contents of (theWeekend)
 cut contents of theWeekend

在 BBEdit 和 AppleScript 中我如何 cut 移动它?

这是您的 代码 的修改版本,在测试中对我有用:

我完全删除了set theWeekend to found text of theWeekend ,同时修改:

set before line theLine of text of text document theFile to (theWeekend & return)

收件人:

set before line theLine of text of text document theFile to (found text of theWeekend & linefeed)
  • theWeekend 在这种情况下是从 find 命令返回的 list,将其保留为 list 因为您实际上需要使用两个 properties 从它。
    • found text 在这种情况下,found objectset thePull to cut ... 上面。
  • 我通常使用 linefeed 而不是 return,因为在某些情况下,虽然不是这个,但后者确实以 0D 而不是 0A 结束。换句话说,它最终是 \r 而不是 macOS 文档中的典型 \n

我也把set thePull to cut theWeekend改成了:

set thePull to cut (found object of theWeekend)

重写 AppleScript 代码:

tell application "BBEdit"
    set theFile to "foobar.txt"
    select insertion point before first character of text document theFile
    repeat
        set theWeekend to find "(?<=Saturday\n)Sunday" searching in text 1 of text document theFile options {search mode:grep} with selecting match
        log theWeekend
        if theWeekend is not found then exit repeat
        select insertion point before first character of text document theFile
        set beforeMon to find "(?!<=Sunday\n)Monday" searching in text 1 of text document theFile options {search mode:grep} with selecting match
        if beforeMon is found then
            set beforeMon to found text of beforeMon
            set theLine to startLine of selection
            set addDay to select insertion point before first character of line theLine of text document theFile
            set thePull to cut (found object of theWeekend)
            set before line theLine of text of text document theFile to (found text of theWeekend & return)
        end if
    end repeat
end tell

错误是 theWeekend 变量包含一个字符串,而不是对字符串的引用。

剪切命令需要引用文档中的一个(字符、单词或行),如下所示:

cut characters 51 thru 56 of text document 1
cut line 7 of text document 1
cut word 7 of text document 1
cut selection -- or this

所以使用找到的对象 属性而不是找到的文本

tell application "BBEdit"
    activate
    tell text document "foobar.txt"
        select insertion point before first character
        repeat
            set theWeekend to find "(?<=Saturday\n)Sunday" searching in text 1 options {search mode:grep} with selecting match
            if theWeekend is not found then exit repeat
            select insertion point before first character
            set beforeMon to find "(?!<=Sunday\n)Monday" searching in text 1 options {search mode:grep} with selecting match
            if beforeMon is found then
                cut (found object of theWeekend) -- the cut command returns nothing, it's useless to put the result in a variable
                set before (found object of beforeMon) to (found text of theWeekend) & return
            end if
        end repeat
    end tell
end tell