首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在AppleScript和BBEdit中剪切文本?

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

Stack Overflow用户
提问于 2017-09-29 21:04:00
回答 2查看 669关注 0票数 1

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

代码语言:javascript
复制
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

使用AppleScript和BBEdit,我希望能够缩短Sunday的时间,并在Monday之前移动它,但是当我引用BBEdit字典时,我看到了cut的能力

当我试图剪切文本并在行之前添加它时,我会得到一个错误:

BBEdit出了一个错误:“星期日”不理解“剪切”信息。

代码:

代码语言:javascript
复制
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

其他失败的尝试:

代码语言:javascript
复制
 cut selection of (theWeekend)
 cut theWeekend
 cut theWeekend of selection
 cut selection of contents of (theWeekend)
 cut contents of theWeekend

在“BBEdit”和“AppleScript”中,我怎样才能把这一天变成现实呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-09-30 15:23:24

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

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

代码语言:javascript
复制
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 属性而不是found

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2017-09-30 15:22:08

下面是您代码的修改版本,在测试中对我起了作用:

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

代码语言:javascript
复制
set before line theLine of text of text document theFile to (theWeekend & return)

至:

代码语言:javascript
复制
set before line theLine of text of text document theFile to (found text of theWeekend & linefeed)
  • 在本例中,theWeekend是从find命令返回的list,将其保留为list,因为您实际上需要使用其中的两个属性。
    • 这个实例中的found text,以及上面的set thePull to cut ...行中的found object

  • 我通常使用linefeed而不是return,因为在某些情况下(虽然不是这种情况),后者最终会变成0D而不是0A。换句话说,它最终是一个\r,而不是典型的macOS文档中的\n

我还将set thePull to cut theWeekend更改为:

代码语言:javascript
复制
set thePull to cut (found object of theWeekend)

code 返工AppleScript

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46496891

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档