1

In a text file given the following text:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

with AppleScript and BBEdit I'd like to be able to cut the day Sunday and move it before Monday but when I reference the BBEdit dictionary I see the ability to cut:

enter image description here

when I try to cut the text and add it before the line I get an error:

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

the code:

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

If I comment out set thePull to cut theWeekend the script works in a continuous loop and places Sunday before Monday but I can't break the loop because my grep variable theWeekend is still false.

Other failed attempts:

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

In BBEdit and AppleScript how can I cut the day and move it?

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127

2 Answers2

1

The error is that the theWeekend variable contains a string, not a reference to a string.

The cut command need a reference to a (character, word or line) in the document, like this:

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

So use the found object property instead of the 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
jackjr300
  • 7,111
  • 2
  • 15
  • 25
  • I beat you by a minute, this time. :) However, I think you explained the issue better then I did and as usual your _coding_ is more efficient. – user3439894 Sep 30 '17 at 15:28
0

Here is a modified version of your code that worked for me in testing:

I removed the set theWeekend to found text of theWeekend line completely, while modifying:

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

To:

set before line theLine of text of text document theFile to (found text of theWeekend & linefeed)
  • theWeekend in this case is a list returned from the find command, leave it as a list because you actually need to use two of the properties from it.
    • found text in this instance and also found object in the set thePull to cut ... line above this.
  • I typically use linefeed instead of return because in some instances, although not this one, the latter does end up as 0D instead of 0A. In other words, it ends up as a \r and not what is typical, \n in macOS documents.

I also changed set thePull to cut theWeekend, to:

set thePull to cut (found object of theWeekend)

Reworked AppleScript code:

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
user3439894
  • 7,266
  • 3
  • 17
  • 28