Re: Finding & replacing text with Applescript

2021-05-24 Thread Christopher Stone
On 05/24/2021, at 21:44, Rich Siegel mailto:sie...@barebones.com>> wrote:
> his is a good script. Some feedback: as a matter of best practices and future 
> proofing, avoid targeting windows (or elements of them).


Hey Rich,

Thanks for the feedback.

Actually I often use this sort of construct:


tell application "BBEdit"
activate
set myDoc to make new text document with properties {text:"some text"}
return myDoc
end tell

--> text document 1 of application "BBEdit"


What I don't like about it is the reference it creates – it will break if you 
change the window index in the script and then refer to the reference again.


Getting a reference to any document besides document 1 produces a more sturdy 
reference:

tell application "BBEdit"
set docRef to document 2
end tell

--> project document "untitled project 45" of application "BBEdit"


If I need the reference to be more robust I have to do something like this:

tell application "BBEdit"
activate

set docID to ID of (make new document with properties {text:"some text"})

tell document id docID
set after its text to linefeed & "some more text" & linefeed
select insertion point after its text
end tell

end tell

Most of the time working with the front document is perfectly adequate – but 
not always.


--
Take Care,
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/89BBA9CA-23BE-4A42-BEE0-063A77475A28%40gmail.com.


Re: Finding & replacing text with Applescript

2021-05-24 Thread Rich Siegel

On 24 May 2021, at 22:37, Christopher Stone wrote:

On 05/24/2021, at 19:11, Duncan Thorne > wrote:
I'm pretty clueless with Applescript so please bear with me. I want 
to copy text from a Safari table, paste it into a new BBEdit window 
and do some text manipulation, mostly finding and replacing.



Hey Duncan,

This task is pretty simple if you know how, and a real head-scratcher 
if you don't.


It's great to be able to record AppleScript, but it often produces 
semi-useless results – unless you savvy AppleScript enough to be 
able to rewrite the recording and pick out the good bits.


I almost never use it, BUT it sometimes comes in really handy when I 
just can't figure out the syntax for something.


Appended is a script that will:

- Copy the selected text in Safari.
- Create a new BBEdit document with said text.
- Reset the size and position of the new document.
- Do one regex-based find/replace on the document.
- You can add more replace statements as needed.


This is a good script. Some feedback: as a matter of best practices and 
future proofing, avoid targeting windows (or elements of them).


"make new text document" will return a reference to the document just 
created.


The "text" property of a document provides access to the text without 
going through the windowing system. It's also faster and avoids 
complications that can arise when the 1:1 relationship between documents 
and windows is not in effect (as has been the case since BBEdit started 
supporting multiple documents per window, back whenever that was).


Thus:

   set myDoc to make new text document with properties { text: 
tableContent }


followed by

   tell text of myDoc
   -- do the replace in here
   end tell

is recommended.

R.

--
Rich Siegel Bare Bones Software, Inc.
  

Someday I'll look back on all this and laugh... until they sedate me.

--
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/9F85F2C7-5105-426B-A609-ECA60A235988%40barebones.com.


Re: Finding & replacing text with Applescript

2021-05-24 Thread Christopher Stone
On 05/24/2021, at 19:11, Duncan Thorne mailto:duncan...@gmail.com>> wrote:
> I'm pretty clueless with Applescript so please bear with me. I want to copy 
> text from a Safari table, paste it into a new BBEdit window and do some text 
> manipulation, mostly finding and replacing.


Hey Duncan,

This task is pretty simple if you know how, and a real head-scratcher if you 
don't.

It's great to be able to record AppleScript, but it often produces semi-useless 
results – unless you savvy AppleScript enough to be able to rewrite the 
recording and pick out the good bits.

I almost never use it, BUT it sometimes comes in really handy when I just can't 
figure out the syntax for something.

Appended is a script that will:

- Copy the selected text in Safari.
- Create a new BBEdit document with said text.
- Reset the size and position of the new document.
- Do one regex-based find/replace on the document.
- You can add more replace statements as needed.

Enjoy.

--
Best Regards,
Chris


# Auth: Christopher Stone
# dCre: 2021/05/24 21:24
# dMod: 2021/05/24 21:29
# Appl: BBEdit
# Task: Copy Safari Table to New BBEdit Document and Find-Replace.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Table, @RegEx, @Massage, @Data


set jsCmdStr to "

(() => {
return window.getSelection().toString()
})();

"
set tableContent to doJavaScriptInSafari(jsCmdStr)

tell application "BBEdit"

activate

make new text document with properties {text:tableContent}

# Resize and reposition the new document.
# {x1, y1, x2, y2} Upper-Left-Corner, Lower-Right-Corner.
set bounds of front window to {0, 45, 1314, 1196}

tell front text window's text
replace "Carbon|Helium" using "••" options {search 
mode:grep, case sensitive:false, starting at top:true}
end tell

end tell


--» HANDLERS

on doJavaScriptInSafari(jsCmdStr)
try
tell application "Safari" to do JavaScript jsCmdStr in front 
document
on error e
error "Error in handler doJavaScriptInSafari() of library NLb!" 
& return & return & e
end try
end doJavaScriptInSafari


-- 
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/137F6908-E277-4388-BAB0-5E0B9A3A4E55%40gmail.com.


Finding & replacing text with Applescript

2021-05-24 Thread Duncan Thorne
I'm pretty clueless with Applescript so please bear with me. I want to copy 
text from a Safari table, paste it into a new BBEdit window and do some 
text manipulation, mostly finding and replacing.
I managed this with the old Tex-Edit Plus (not TextEdit) which is 
unfortunately 32-bit. With T-E+ I would paste the text from the clipboard 
with the commands make "new window", "paste", "select window 1" and then 
use a "replace window 1 looking for..." command.
When I try recording something in BBE I get the line
replace "[original text]" using "[replacement text]" searching in text 1 of 
text document "*untitled text ##*" ...
— i.e. a named doc as opposed to simply the front doc. Not a surprise, but 
what do I replace "untitled text ##" with?

-- 
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/e763c904-8187-458f-a2f5-a19ccec5db3dn%40googlegroups.com.


Re: Grep find/replace numbers

2021-05-24 Thread Christopher Stone
On 05/22/2021, at 19:22, severdia mailto:sever...@gmail.com>> wrote:
> Yes, you're right. I got it working, but for future reference here's what I 
> was trying to do...I have many of these elements:
> 
> Macbeth5.15.1.64
> 
> Both  and  contain act/scene/line number info and either one other 
> the other is incomplete (the one with two numbers) and I want to keep the one 
> that's complete (with three numbers). My objective was to delete either  
> or  if it had only two numbers in it.


Hey Ron,

Explaining your actual task is quite helpful – but I always want to see 
examples of the start condition and the desired outcome in black and white.  

Like so:

Data Sample 01:

Macbeth5.15.1.64

Desired Outcome 01:

Macbeth5.1.64

You wouldn't believe how often people leave out or otherwise mangle 
instructions.  The "A picture is worth a thousand words." rule is highly 
relevant with this sort of task.

Here's what I would do:

Find:

\d+\.\d+

Replace with nothing.

With  I'm finding a digit that may ore may not exist and I'm using 
that capture in the closing tag .  This covers any loc tag from 0-9.

This is a trifle simpler and more direct (if there are only  and  
tags).

\d+\.\d+

While we're on the topic of data massage with regex, let me recommend this site 
to you:

https://regex101.com/r/WtIAmE/3  (This link 
contains your example problem.)

When I'm working on complex regular expressions I always start with BBEdit, but 
if I have substantial problems I have some regex visualizer apps (RegExRx 
 in particular) and 
RegEx101.com  to fall back on.

I can't use BBEdit 13's new Pattern Playground on my main Mac, because Sierra 
isn't supported.

One reason to look forward to getting new hardware...

--
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/7FB473E5-2467-4D8D-BE44-2351216E4B49%40gmail.com.