Get the result of a replace in AppleScript

83 views
Skip to first unread message

Lionel

unread,
Sep 20, 2022, 8:55:34 AM9/20/22
to BBEdit Talk
Hi,

I'm using AppleScript to do some text transformations in BBEdit. For exemple :

replace "\\s+&\\s+_\\s+" using " \\& " options {search mode:grep, starting at top:true}

When BBEdit is used with AppleScript, it does not post notifications, that's why I'd like to get the numbers of occurrences found.

result = {replace ...} doesn't work. Is there a way to do that ?

Thanks

Lionel

unread,
Sep 20, 2022, 11:47:48 AM9/20/22
to BBEdit Talk
I answer myself.

A call to the AppleScript library of BBEdit sometimes returns the number of hits found, this is the case with replace.

process lines containing text 1 of text document 1 matching string "^'" output options {deleting matched lines:true} with matching with grep

But with "process lines containing text" for example, this is not the case and it is better to avoid testing the returned value. It must be somewhere but not in the global property « result ». If someone has a solution, I'm interested.

Thanks.

Christopher Stone

unread,
Sep 21, 2022, 11:19:07 AM9/21/22
to BBEdit-Talk
On Sep 20, 2022, at 10:46, Lionel <att...@free.fr> wrote:

A call to the AppleScript library of BBEdit sometimes returns the number of hits found, this is the case with replace.

process lines containing text 1 of text document 1 matching string "^'" output options {deleting matched lines:true} with matching with grep

But with "process lines containing text" for example, this is not the case and it is better to avoid testing the returned value. It must be somewhere but not in the global property « result ». If someone has a solution, I'm interested.


Hey Lionel,

BBEdit's process lines containing command does not return the number of processed lines - period.

You have to look at the actual output of the command.  When you do you find it returns a record containing two items:

modified text
copied lines

Modified text is what the text in your document is transformed into by the command, and copied lines is the lines that are deleted.

What's more – copied lines is always punctuated with a linefeed – even when the last line deleted doesn't have one.

So – you have to do something like this:

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/09/21 10:00
# dMod: 2022/09/21 10:00 
# Appl: BBEdit
# Task: Return the Number of Processed Lines.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Processed, @Lines, @Processed_Lines
--------------------------------------------------------

tell application "BBEdit"
tell front text window's text
set processLinesResultRecord to process lines containing matching string "^[ts]" output options {deleting matched lines:true} with matching with grep
end tell


set numberOfProcessedLines to copied lines of processLinesResultRecord


if numberOfProcessedLines ≠ "" then
# Remove the extraneous linefeed at the end of 'copied lines'.
set numberOfProcessedLines to replace "\\s\\z" using "" searchingString numberOfProcessedLines options {search mode:grep}
# Count the number of paragraphs to determine the number of processed lines.
set numberOfProcessedLines to length of (get paragraphs of numberOfProcessedLines)
end if


end tell

--------------------------------------------------------

You could simply subtract 1 from the number of paragraphs instead of munging the string as I have.

I'm using BBEdit 14.1.2 on macOS 10.14.6 for this test.  I doubt the newest version has changed much in this regard but can't be certain.

--
Best Regards,
Chris

Lionel

unread,
Sep 26, 2022, 9:26:12 AM9/26/22
to BBEdit Talk
Hi Chris,

Thank you for the answer.

I'm sorry for the late reply. I was not notified of your response. I will change the preferences for tracking responses in this group.

I will implement what you describe and come back here for feedback.

Lionel

unread,
Sep 26, 2022, 9:26:19 AM9/26/22
to BBEdit Talk
Hey Christopher,

The code you gave works very well. However, when integrated with my own instructions, AppleScript crashes! I don't understand why. So I wrote a substitution routine.

set count_comments to replaceTokensWithGrep("^'[\\S\\t ]*\\r", nullStr)

replaceTokensWithGrep(textToSearch, replaceWith) just do a replace all.

AppleScript is not really a friend of regex ;-) You have to test the text to transform with BBEdit and then translate it into AppleScript string to incorporate it into the script.

And sometimes it doesn't work, for example with \x20{6} supposed to replace six spaces. Is there a way to enter in AppleScript the same plain text used in BBEdit, with a tell statement for exemple ?

Best Regards,
Lionel

Le mercredi 21 septembre 2022 à 17:19:07 UTC+2, listmei...@gmail.com a écrit :

Christopher Stone

unread,
Oct 14, 2022, 6:29:56 PM10/14/22
to BBEdit-Talk


On Sep 26, 2022, at 08:22, Lionel <att...@free.fr> wrote:

AppleScript is not really a friend of regex ;-) You have to test the text to transform with BBEdit and then translate it into AppleScript string to incorporate it into the script. 

And sometimes it doesn't work, for example with \x20{6} supposed to replace six spaces. Is there a way to enter in AppleScript the same plain text used in BBEdit, with a tell statement for exemple ?


Hey Lionel,

I suppose you could look at it that way.  I know I cussed and carried on about having to manage quoting strings in AppleScript strings for a while. I even had a routine that did all the work on the clipboard with a regex osax way back in the day.

Then I bought Script Debugger which has a built-in command for pasting-quoted, and I never looked back.

Keep in mind that the back-slash is a reserved character in AppleScript and as such must be escaped to be used as a literal.



tell application "BBEdit"
    tell front text window's text
        replace "\\x20{6}" using "••••••" options {search mode:grep, case sensitive:false, starting at top:true}
    end tell
end tell



--
Best Regards,
Chris

Reply all
Reply to author
Forward
0 new messages