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

Text 如何在AppleScript和BBEdit中剪切文本?,text,applescript,repeat,cut,bbedit,Text,Applescript,Repeat,Cut,Bbedit,在给定以下文本的文本文件中: Monday Tuesday Wednesday Thursday Friday Saturday Sunday 使用AppleScript和BBEdit,我希望能够在Sunday之前剪切一天,并将其移动到Sunday之前,但当我参考BBEdit字典时,我看到了剪切的功能: 当我尝试剪切文本并将其添加到行之前时,我得到一个错误: BBEdit出错:“Sunday”不理解“cut”消息 代码: tell application "BBEdit" set t

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

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
使用AppleScript和BBEdit,我希望能够在
Sunday
之前剪切一天,并将其移动到
Sunday
之前,但当我参考BBEdit字典时,我看到了
剪切的功能:

当我尝试剪切文本并将其添加到行之前时,我得到一个错误:

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
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

在BBEdit和AppleScript中,我如何
剪切日期并将其移动?

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

我完全删除了
设置weekend以查找weekend
行的文本,同时修改了:

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

  • weekend
    在本例中是从
    find
    命令返回的
    list
    ,将其保留为
    list
    ,因为实际上需要使用其中的两个属性。
    • found text
      在本例中,以及
      中的
      found object
      将pull设置为cut…
  • 我通常使用
    linefeed
    而不是
    return
    ,因为在某些情况下,虽然不是这一种,但后者最终会成为
    0D
    而不是
    0A
    。换句话说,它最终成为一个
    \r
    ,而不是macOS文档中典型的
    \n
我还将
设置Pull以缩短周末时间
,改为:

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
        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
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
告诉应用程序“BBEdit”
将文件设置为“foobar.txt”
选择文件中文本文档第一个字符前的插入点
重复

设置weekend以查找“(?错误在于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

因此,请使用找到的对象属性,而不是找到的文本

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
告诉应用程序“BBEdit”
激活
告诉文本文档“foobar.txt”
选择第一个字符之前的插入点
重复

设置周末查找“(?这次我比你快了一分钟。)但是,我认为你比我解释得更好,而且像往常一样,你的编码效率更高。