A question about JavaScript package extension

Is it still possible to define the main method in the file corresponding to the Javascript-file option? I have a main method defined in my plugin, but it doesn’t seem to be executed.

Config.json

{ 
// ...
  "options": [
    {
      "identifier": "option-a",
      "label": "Option A",
      "type": "boolean",
      "defaultValue": false
    }
  ],
  "actions": [
    {
      "identifier": "action-a",
      "icon": "iconify:ph:rocket-launch-thin",
      "title": "Action A",
      "javascript-file": "actionA.js",
      "after": "show-result"
    }
  ]
}

actionA.js

const main = async () => {
  const optA = popclip.options["option-a"];
  const text = popclip.input.text;
  return `input:${text} option:${optA}`;
};

1 Like

For the simplified javascript and javascript file actions, the the main() wrapper is added by PopClip and the actual source file should just be code:

const optA = popclip.options["option-a"];
const text = popclip.input.text;
return `input:${text} option:${optA}`;

Or, if you want to program with real functions in your file (which is nice), use a module-based extension.

Oh, I see, thanks :grin:

And I have more 2 questions:

  1. The function util.buildQuery() doesn’t seem to support boolean-type values, and I’m trying to use it to convert options to url query, but I’m finding that all boolean-type data is being discarded.
  2. I have a string type option and I want to show the action when the option is not null, how do I set the requirements, i tried it
    requirements:
      - option-my-opt
    
    but it doesn’t work.

Great question. I’ve checked the source and it seems util.buildQuery only accepts string parameters. Anything else will be discarded. (I need to document that, or maybe just remove it from the docs.)

So you could either wrap your params in String() to stringify them, or (better) use URLSearchParams instead, which is bundled with PopClip along with URL

(The util.buildQuery and related API is something I put together whilst first working on JS extensions, and it was a means to an end. Later I imported those standard URL and URLSearchParams APIs and today I would recommend using those. Again, I need to improrove the docs on this.)

I wrote a little test to demonstrate:

const obj = { foo: "bar", state: true, count: 5 };

const query = util.buildQuery(obj);
print(query);

const query2 = new URLSearchParams(obj);
print(query2.toString());

Running that with the PopClip test harness I get:

PopClip JavaScript Test Harness (url-query-test.js)
foo=bar
foo=bar&state=true&count=5
Done: success: <no output text>

So you should be golden.

For this, you can use:

requirements:
  - '!option-my-opt='

This may be read as “the option called my-opt is not an empty string”.

The quotes are necessary because of the ! at the start of the string, which has special meaning in YAML.

1 Like

Great!Thanks a lot :smile:

1 Like