Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #plugin action not working in modern versions of Sketch #286

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

rodionovd
Copy link

@rodionovd rodionovd commented Feb 16, 2024

Getting this action working again is key to unlock workflows like this one: https://forum.sketch.com/t/populate-color-from-json-file/2465


P.S. The official documentation lacks practical examples so here's how the #plugin action is supposed to be used:

  1. Let's say you have the following plugin installed that you want to invoke on one of your template layers based on some condition:
    Screenshot 2024-02-16 at 13 54 14

  2. Let's also assume that you have the following template layer structure:
    Screenshot 2024-02-16 at 13 56 10

  3. Now, to invoke the plugin on the Rectangle layer, rename the latter as follows:

    #plugin[{condition}, My Plugin > My Action]
    
    Screenshot 2024-02-16 at 13 59 01

    ℹ️ There're no quotation marks around the plugin command path.
    ℹ️ You may only invoke single-level commands, so things like My Plugin > Group > My Action won't work).

Notes

  • Your {condition} might be as simple as just true (e.g. #plugin[true, My Plugin > My Action]) if you want to always invoke the plugin no matter what's in your data. It may also reference other variables in your data: see examples.

  • You may pass arguments to a plugin by including them in a layer name:

    #plugin[{condition}, My Plugin > My Action, arg1, arg2 {zip_code}, arg3]
    

    ℹ️ The {zip_code} placeholder in the example above will be replaced with an actual value from your data just like any other placeholder).

    In order to access those arguments in your plugin, use the following:

    // in script.js
    
    const util = require('util')
    var onRun = function (context) {
       let arguments = util.toArray(context.selection[0].userInfo()["datapopulator"]).map(String)
       // arguments == ["arg1", "arg2 {94024}", "arg3"]
    }

    As you can see, argument are passed to the plugin as an array of strings.

    ⚠️ Don't forget to use util.toArray() and the map bit to convert it to a proper JavaScript array of Strings.

@rodionovd
Copy link
Author

Linking #220

@@ -69,7 +69,11 @@ export function perform(condition, layer, params) {
setCommandParamsToMetadata(layer, params)

// run the command
NSApp.delegate().runPluginCommand(command)
if (NSApp.delegate().runPluginCommand_fromMenu_context_) {
NSApp.delegate().runPluginCommand_fromMenu_context_(command, null, {})
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory we might use the context argument here to pass the same command params as we do via userInfo, but it will suffer from the same serialization limitations, so I wouldn't bother.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant