On 05/24/2021, at 19:11, Duncan Thorne <[email protected] 
<mailto:[email protected]>> 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 "[email protected]" rather than 
posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
--- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/bbedit/137F6908-E277-4388-BAB0-5E0B9A3A4E55%40gmail.com.

Reply via email to