AppleScript as visibility filter?

Hey,
I’m trying to make a LaTeX extension appear only when it’s actually relevant.

I currently set the extension to appear for apps like BBEdit, Texpad, CotEditor, like this:

// #popclip
// name: LaTeX
// identifier: com.megira.latex-tools
// description: Smart LaTeX wrapping tools for Overleaf and LaTeX editors.
// icon: square filled text:TeX
// show as: icon
// language: javascript
// module: true
// entitlements: [dynamic]

const LATEX_APPS = new Set([
  "com.barebones.bbedit",
  "com.vallettaventures.Texpad",
  "com.coteditor.CotEditor",
  "com.microsoft.Powerpoint",
]);

 >> rest of code.....

But sometimes I may be editing a non-TeX document in bbedit, so app filtering alone is too broad.

BBEdit exposes the active document language via AppleScript:

tell application id "com.barebones.bbedit" to get source language of active document of front text window

If that returns TeX, I’d like the extension to appear. Otherwise, I’d like it hidden.
The AppleScript should be activate only when I’m doing a selection in Bbedit of course.

So my question, Is there any way to incorporate AppleScript result as a visibility condition?

I’m trying to avoid a workaround where the action appears and only checks after being clicked.

Thanks!

1 Like

HI @megira, welcome to the forum and thanks for the question.
It’s not currently possible to do this. Visibility filtering can be done by code, but only JavaScript code, and that JavaScript can’t call into AppleScript.
I do actually have calling AppleScript from JavaScript on my roadmap though.
However, I’m not sure if it would be allowed at the population phase, i.e., the time when the popclip is generating its actions. That’s because anything that could slow down showing the bar could introduce a meaningful delay. There’s also a potential security issue because it runs on every test selection. I’ll need to do some testing and some design & thinking.

Hey @nick, thanks for the reply

So I was thinking on another way that doesn’t involve AppleScript.
Does PopClip’s JavaScript have the ability to identify the type of file I’m selecting in? Perhaps based on the file extension. Same as it is currently capable of knowing the app I’m in.

Then it would be even more powerful than the initial idea of running the BBedit AppleScript.

Just an idea to resolve this in opposite way. BBEdit has option to call scripts for selected actions like document open, document close etc.
If PopClip will have AppleScript interface to:

  • enumerate list of extension with information about state (enabled/disabled)
  • possibility to enable/disable extension via AppleScript
    then action could be looks like
  • BBEdit - document open /windows activated - check type of extension
    • if extension is TeX - enable extension
    • if not - disable extension

Unfortunately currently PopClip has very poor API for AppleScript, only TGKA (The Good Know Author - @nick ) can resolve this.

This mechanizm can be used i other editors with only one condition - possibility to call AppleScript when windows /file type is changing.

By the way - I use one this BBEdit’s action to change syntax of document with extension .popcliptxt between yaml (default), javascript and Unix Shell.
I will put this in another topic in this forum.

Thanks you. It sounds intersting , I’ll give it a try

@nick - do you think that will be possible to extend AppleScript API to enable/disable extensions dynamically?

1 Like