Action

Switch Syntax…

Posted by @nahumck, Last update 12 days ago

Shows a popup allowing you to see the current syntax and choose a new syntax from a list of your installed syntaxes for the current draft, i.e. choosing between Markdown, Taskpaper, etc.

Code mostly by Greg.

Steps

  • script

    // Get currently installed syntax
    const syntaxes = Syntax.getAll();
    const syntaxNames = syntaxes.map(s => s.name);
    
    // Get the current draft syntax
    const currentSyntax = draft.syntax;
    
    // Create a prompt to select a new syntax from a list or cancel
    let p = Prompt.create();
      p.title = "Switch Syntax";
      p.message = "Currently: " + currentSyntax.name;
      p.addSelect("listSelect", "Syntax", syntaxNames, [currentSyntax.name], false);
      p.addButton("Switch Syntax");
    let didSelect = p.show();
    
    // Set the selected syntax in order to switch
    let selected = p.fieldValues["listSelect"];
    let selectedIndex = syntaxes.findIndex(s => s.name == selected);
    let selectedSyntax = syntaxes[selectedIndex];
    
    // if Switch Syntax is selected, change the syntax to the selected syntax in the prompt
    if (p.buttonPressed == "Switch Syntax" && currentSyntax.name != selectedSyntax.name) {
    	draft.syntax = selectedSyntax;
    	draft.update();
    	app.displaySuccessMessage("Switched to " + selectedSyntax.name);
    }

Options

  • After Success Default
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.