1

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

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

使用 AppleScript 和 BBEdit 我希望能够缩短这一天Sunday并在之前移动它,Monday但是当我参考 BBEdit 字典时,我看到了以下能力cut

在此处输入图像描述

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

BBEdit 出现错误:“Sunday”不理解“cut”消息。

编码:

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脚本在一个连续循环中工作并放在Sunday之前Monday但我不能打破循环,因为我的 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在一天中移动它?

4

2 回答 2

1

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

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

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

所以使用found object属性而不是found text

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
于 2017-09-30T15:23:24.617 回答
0

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

我完全删除了该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在本例中是listfind命令返回的 a,将其保留为 a,list因为您实际上需要使用其中的两个属性
    • found text在这种情况下,也在上面found objectset thePull to cut ... 中。
  • 我通常使用linefeed代替,return因为在某些情况下,虽然不是这个,但后者最终会作为0D代替0A。换句话说,它在 macOS 文档中以 a\r而不是典型的形式结束。\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
于 2017-09-30T15:22:08.873 回答