Syntax for file filter in AppleScript?

148 views
Skip to first unread message

Sonic Purity

unread,
Oct 9, 2021, 1:07:07 AM10/9/21
to BBEdit Talk
I’m finding the documentation on using the filter option for replace in an AppleScript less than helpful: i don’t understand the syntax.

Without that option, this replace command works perfectly in the overall script:

replace (("TOCCHAP" & currchap as text) & ">") using navlinkholder searching in story_directory options {showing results:falsesaving yes


The moment i add the clause filter "Exclude index.html" so it becomes this:

replace (("TOCCHAP" & currchap as text) & ">") using navlinkholder searching in story_directory options {showing results:false} filter "Exclude index.html" saving yes

and try to run the script, it throws the following error:

error "BBEdit got an error: Could not make some data into the desired type (MacOS Error code: -1700)" number -1700 to record


There is an existing file filter i made via the multiple file Find interface named:

Exclude index.html

with settings:

Any File name root is not index


Seems obvious that my clause for filter is all wrong, but the documentation isn’t making it clear and searches on this group haven’t yielded anything.


What is the proper syntax when one wants to use the BBEdit filter noun in AppleScript?


Thanks in advance as always—the help has been stellar so far!

(BBEdit 11.6.8, macOS 10.12.6, AppleScript 2.5, Script Editor 2.9, if any of these matter)

jj

unread,
Oct 9, 2021, 5:32:36 AM10/9/21
to BBEdit Talk
Hi Sonic,

As far as I know there is no way to call filters by name or to create filters with AppleScript.
Nonetheless, you can record them and use them in your scripts.

 1. In BBEdit, open the "Multi-File Search" window.
 
 2. Configure the search options you want –– File and Folders filters included.
 
 3. Use a word that WILL NOT match for the Find: and Replace: fields. (This is just a precaution to avoid accidentally replacing anything.)
 
    find: ___THIS__WILL__N_E_V_E_R__BE__FOUND___
    replace: ___THIS__WILL__N_E_V_E_R__BE__FOUND___
    
 4. Launch Script Editor, open a new script and click the "Record a Script" button.
 
 5. Go back to BBEdit and click on "Replace All" in the "Multi-File Search" window.
 
 6. In Script Editor pause the recording.
 
 7. Script Editor should have recorded the command. 
 
    tell application "BBEdit"
        activate
        replace "___THIS__WILL__N_E_V_E_R__BE__FOUND___" using "___THIS__WILL__N_E_V_E_R__BE__FOUND___" saving no searching in {file "macosx:Users:jj:Documents:Notebook:"} options {search mode:grep, filter:{ID:"0DD90624-6CEA-4757-BCD1-A28CCA6DB640", filter_mode:or_mode, filter_terms:{{operand:".applescript", field:«constant ****FnSf», operator:op_is_equal}}}, showing results:true} with skip shielded folders, search invisible folders and search git_ignored files without text files only
    end tell

 8. The Filter is in the options. You can use it in your AppleScript script.
 
    filter:{ID:"0DD90624-6CEA-4757-BCD1-A28CCA6DB640", filter_mode:or_mode, filter_terms:{{operand:".applescript", field:«constant ****FnSf», operator:op_is_equal}}}
 
An easier option is to create a Text Factory that includes your Filters and call it from AppleScript like so.

    tell application "BBEdit"
        set vFactoryFile to POSIX file "/path/to/my/replace.textfactory"
        apply text factory (vFactoryFile as alias) to (first text of first document of first window) saving no
    end tell

Note that, as your BBEdit copy is lagging behind a few versions, some of this might not work as described.

HTH

Jean Jourdain

Sonic Purity

unread,
Oct 9, 2021, 8:12:06 PM10/9/21
to BBEdit Talk
Thank you again, Jean. I’d not tried recording manual actions, as having done so earlier in this project produced no results whatsoever in the recorded script. It does indeed work as you describe in my older BBEdit version.

Further experimentation:
* Removing the ID clause (all the way up to the f in filter mode) works equally well, likely because all the needed information is inline in the script.
* Leaving the ID clause alone and removing the rest of the terms fails: script compiles and runs, but finds as part of the replace fail.
* Removing everything and using filter:{name:[name of my filter here, in quotes]} fails exactly as above.

Either the original (with ID) or leaving all the filter information other than the ID work and are wholly sufficient for my use case. I continue to wonder whether there’s a way to call existing filters, as much for curiosity as any other reason.

Reply all
Reply to author
Forward
0 new messages