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