Searching an html tag with Applescript and BBEdit

42 views
Skip to first unread message

Murat

unread,
Jun 28, 2021, 8:47:56 AM6/28/21
to BBEdit Talk
Hello,
I am new to BBEdit. I have searched this forum but have not been able to locate a discussion relevant for my problem. I am maybe doing something stupid but, I cannot make BBEdit find an HTML using it through Applescript, in a document that definitely includes this tag.
My simple script is:
tell application "BBEdit"
    open (alias "path to my file") -- this file opens, no problem, using the real path that I do not give here
    set result to (find "<title>" searching in text of text window 1 with selecting match)
    set mytext to text of text window 1 -- this text is good and contain then <title> tag
end tell

But the tag is not found and the result variable is not created with the result of the search.

I can search <title> directly in BBEdit, and it of course finds it.
I have tried to escape then < and > characters but of course this does not make sense for Applescript either. These characters do not need escaping I think.

I am a bit lost here because I do not see why BBEdit cannot find the tag in a file that contains it. Any help would be very much appreciated.
Very cordially,

Murat

jj

unread,
Jun 28, 2021, 10:32:07 AM6/28/21
to BBEdit Talk

Hi Murat,

Might it be that the insertion point in your file is past the <title> tag and the find wrap around option is not set?

Try setting the "wrap around" option to true in the find command:

find "<title>" searching in text 1 of text window 1 options {search mode:grep, starting at top:true, wrap around:true, returning results:true} with selecting match


The find command has various options as reported in the BBEdit dictionary:

Search Optionsn : Options for “find” and “replace” commands properties

search mode (literal/‌grep) : the type of search (literal search if omitted)

starting at top (boolean) : start from the top of the document? (false if omitted)

wrap around (boolean) : should the search wrap from the end of the document? (false if omitted)

backwards (boolean) : should the search proceed backwards? (false if omitted)

case sensitive (boolean) : should the search be case-sensitive? (false if omitted)

match words (boolean) : should the search only find whole words? (false if omitted)

extend selection (boolean) : should the selection range include the original selection start through the end of the match? (false if omitted)

returning results (boolean) : if performing a batch or multi-file search, return a list of matches? (false if omitted)

showing results (boolean) : if performing a batch or multi-file search, open the list of results? (true if omitted)

HTH

Jean Jourdain

Murat

unread,
Jun 28, 2021, 12:02:11 PM6/28/21
to BBEdit Talk
Thank you very much Jean,
I will explore these options to check if they solve my problem. Indeed, I have assumed that if I open the document, the cursor would be at the beginning of the file, which is not necessarily true.
I have seen these options in the dictionary but was not sure how to use them.
In the meantime, I have solved my replacement blues using TextSoap, but I would like to check if BBEdit is more reactive in Applescripts.
Thank you very much!

Murat

unread,
Jun 28, 2021, 12:42:20 PM6/28/21
to BBEdit Talk
Yes! These options helps to find the text now but the replace part poses a problem now (I do not use the grep here because it is a simple substitution:

-- THis script is directly adapted from the BBEdit manual, page 335
tell application "BBEdit"
    
    set result to (find "<title>" searching in ¬
        (alias "HFS path to an html file") ¬
            options {starting at top:true, returning results:true} with selecting match)
    
    if (found of result) then
        set text of (found object of result) to ¬
            "bla bla bla <title>"
Script debugger gives the following AS execution error:
It is impossible to obtain the  found object of {found:true, found matches:{{result_file:file "HFS path to my file", result_kind:note_kind, start_offset:487, end_offset:494, result_line:9, message:"<title>Bla bla bla</title>"}}}

I have tried to replace found object of results by found matches of results but this does not work either. :-(

Le lundi 28 juin 2021 à 21:32:07 UTC+7, jj a écrit :

jj

unread,
Jun 28, 2021, 2:20:26 PM6/28/21
to BBEdit Talk
Murat,

To have a result with the found object you have to remove the "returning results" options or set it to false in the find command.

tell application "BBEdit"

set result to find "<title>" searching in text of first window options {starting at top:true}

end tell

Result:

{found:true, found object:characters 74 thru 80 of text document 1 of application "BBEdit", found text:"<title>"}

Murat

unread,
Jun 29, 2021, 1:37:56 AM6/29/21
to BBEdit Talk
Hello again Jean,
When I do this:

    set result to (find "<title>" searching in ¬
        (alias "full HFS path to my file") ¬
            options {starting at top:true} with selecting match)
    
    if (found of result) then .....

Script debugger tells me, for the last line
Variable result is undefined.

If I execute with option returning results, the variable is defined but its structure does not correspond with what the script given in the manual for the replacement part expects as structure...

Christopher Stone

unread,
Jun 29, 2021, 2:51:40 AM6/29/21
to BBEdit-Talk
On 06/29/2021, at 00:37, Murat <myi...@gmail.com> wrote:
    set result to (find "<title>" searching in ¬
        (alias "full HFS path to my file") ¬
            options {starting at top:true} with selecting match)
    
    if (found of result) then .....

Script debugger tells me, for the last line
Variable result is undefined.

If I execute with option returning results, the variable is defined but its structure does not correspond with what the script given in the manual for the replacement part expects as structure...


Hey Murat,

Do not make a habit of using the result variable in AppleScript.  It's too mutable and makes debugging more difficult.

When you search a file on disk BBEdit's file search browser is activated, and the found result of the AppleScript is omitted.

To get a found result you can do something like this.



set fileAlias to alias "MyHD:Users:UserName:Downloads:Test.ccs.txt"

tell application "BBEdit"
    set docRef to open fileAlias
    set foundRec to (find "Now.+" searching in docRef options {starting at top:true, search mode:grep}) # with selecting match
end tell



To use this script you have to change the path to the file.

Let's make that just a bit more turnkey.



set fileAlias to alias ((path to downloads folder as text) & "Test.ccs.txt")

tell application "BBEdit"
    set docRef to open fileAlias
    set foundRec to (find "Now.+" searching in docRef options {starting at top:true, search mode:grep}) # with selecting match
end tell



Now if we have the file with the same name in the same relative location there is no need to change the path.

You could also write that using a Unix style relative path.  BBEdit 



set filePath to "~/Downloads/Test.ccs.txt"
tell application "System Events" to set fullFilePath to POSIX path of disk item filePath

tell application "BBEdit"
    set docRef to open fullFilePath
    set foundRec to (find "Now.+" searching in docRef options {starting at top:true, search mode:grep}) # with selecting match
end tell




--
Best Regards,
Chris

Murat

unread,
Jun 29, 2021, 3:30:07 AM6/29/21
to BBEdit Talk
Thank you very much for all your help! It now works indeed and it is pretty quick in comparison with my actual solution with another text robot.

set myfile to alias "full HFS file of the file to be searched in"

tell application "BBEdit"
    set docRef to open myfile
    set foundRec to (find "searchText" searching in docRef options {starting at top:true})  
    if (found of foundRec) then

        set text of (found object of result) to ¬
            "SubstitutionText"
    end if
    close docRef with saving
end tell

I can hence process a list of aliases with such a quick tool. Maybe the section in the manual should be updated. I was trying to follow it before ending in this dead alley... :-)
By the way, this part is surprisingly poor in comparison with the global quality and pedagogy of this manual. Really impressive. It made me to understand at least Regex, I have done any attempts in the past but it never clicked in my mind (I do program in several languages, even if Applescript is very new for me). THe explanations in that chapter of the BBEdit manual were illuminating for me. Thank you very much to its authors! I have not become a RegeX wizard, but it already helps me in many text transformations.

Thank you very much again.

Best.

Murat
Reply all
Reply to author
Forward
0 new messages