Re: Get the result of a replace in AppleScript

2022-10-14 Thread Christopher Stone


> On Sep 26, 2022, at 08:22, Lionel  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

-- 
This is the BBEdit Talk public discussion group. If you have a feature request 
or need technical support, please email "supp...@barebones.com" rather than 
posting here. Follow @bbedit on Twitter: 
--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to bbedit+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/bbedit/59D2F19E-F530-41E3-ADC5-01A8AD637655%40gmail.com.


Re: Get the result of a replace in AppleScript

2022-09-26 Thread Lionel
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 :

> On Sep 20, 2022, at 10:46, Lionel  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
>
>

-- 
This is the BBEdit Talk public discussion group. If you have a feature request 
or need technical support, please email "supp...@barebones.com" rather than 
posting here. Follow @bbedit on Twitter: 
--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to bbedit+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/bbedit/3760f14e-34b7-4c21-832e-f97d4a19e671n%40googlegroups.com.


Re: Get the result of a replace in AppleScript

2022-09-26 Thread Lionel
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.

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

> On Sep 20, 2022, at 10:46, Lionel  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
>
>

-- 
This is the BBEdit Talk public discussion group. If you have a feature request 
or need technical support, please email "supp...@barebones.com" rather than 
posting here. Follow @bbedit on Twitter: 
--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to bbedit+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/bbedit/4ef5a6e2-6636-488a-99ee-c92aad345134n%40googlegroups.com.


Re: Get the result of a replace in AppleScript

2022-09-21 Thread Christopher Stone
> On Sep 20, 2022, at 10:46, Lionel  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

-- 
This is the BBEdit Talk public discussion group. If you have a feature request 
or need technical support, please email "supp...@barebones.com" rather than 
posting here. Follow @bbedit on Twitter: 
--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to bbedit+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/bbedit/7DC96F7E-5C88-443D-BBCF-4A9D36EE8DE6%40gmail.com.


Re: Get the result of a replace in AppleScript

2022-09-20 Thread Lionel
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.

Le mardi 20 septembre 2022 à 14:55:34 UTC+2, Lionel a écrit :

> 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

-- 
This is the BBEdit Talk public discussion group. If you have a feature request 
or need technical support, please email "supp...@barebones.com" rather than 
posting here. Follow @bbedit on Twitter: 
--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to bbedit+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/bbedit/46350874-b3b0-4741-a902-fa746863060bn%40googlegroups.com.