Action

Scripted Shortcut Run

Posted by sylumer, Last update over 1 year ago - Unlisted

Steps

  • script

    // Run a shortcut and return the result
    function shortcutRun(p_strShortcutName, p_strShortcutInput)
    {
    	// Define the script to run
    	let objShell = ShellScript.create(`#!/bin/zsh
    echo "${p_strShortcutInput}" | shortcuts run "${p_strShortcutName}" | tee`);
    
    	// Run the script and return the result or write out any error to the log.
    	if (objShell.execute()) return objShell.standardOutput;
    	else
    	{
    		app.displayErrorMessage(objShell.standardError);
    		console.log("STDERR: " + objShell.standardError);
    		return;
    	}
    }
    
    // Run a shortcut and append the result to the end of the draft
    function shortcutAppend(p_strShortcutName, p_strShortcutInput)
    {
    	// Append output to a new line at the end
    	draft.content = draft.content + "\n" + shortcutRun(p_strShortcutName, p_strShortcutInput);
    	return draft.update();
    }
    
    // Run a shortcut and insert the result at the current cursor position
    function shortcutInsert(p_strShortcutName, p_strShortcutInput)
    {
    	return editor.setSelectedText(shortcutRun(p_strShortcutName, p_strShortcutInput));
    }
    
    // Run a shortcut and replace the draft content with the result
    function shortcutReplaceDraft(p_strShortcutName, p_strShortcutInput)
    {
    	// Replace content
    	draft.content = shortcutRun(p_strShortcutName, p_strShortcutInput);
    	return draft.update();
    }
    
    // Run a shortcut and replace the draft body with the result
    function shortcutReplaceBody(p_strShortcutName, p_strShortcutInput)
    {
    	// Append output to the title and replace content
    	draft.content = draft.title + "\n" + shortcutRun(p_strShortcutName, p_strShortcutInput);
    	return draft.update();
    }
    
    // Run a shortcut with the draft content as input and return the result
    function shortcutRun_Draft(p_strShortcutName)
    {
    	return shortcutAppend(shortcutRun_Draft, draft.processTemplate('[[draft]]'));
    }
    
    // Run a shortcut with the draft content as input and append the result to the end of the draft
    function shortcutAppend_Draft(p_strShortcutName)
    {
    	return shortcutAppend(p_strShortcutName, draft.processTemplate('[[draft]]'));
    }
    
    // Run a shortcut with the draft content as input and insert the result at the current cursor position
    function shortcutInsert_Draft(p_strShortcutName)
    {
    	return shortcutInsert(p_strShortcutName, draft.processTemplate('[[draft]]'));
    }
    
    
    // Run a shortcut with the draft content as input and and replace the draft content with the result
    function shortcutReplaceDraft_Draft(p_strShortcutName)
    {
    	return shortcutReplaceDraft(p_strShortcutName, draft.processTemplate('[[draft]]'));
    }
    
    // Run a shortcut with the draft content as input and and replace the draft body with the result
    function shortcutReplaceBody_Draft(p_strShortcutName)
    {
    	return shortcutReplaceBody(p_strShortcutName, draft.processTemplate('[[draft]]'));
    }
    
    // Run a shortcut with the draft body as input and return the result
    function shortcutRun_Body(p_strShortcutName)
    {
    	return shortcutAppend(shortcutRun_Draft, draft.processTemplate('[[body]]'));
    }
    
    // Run a shortcut with the draft body as input and append the result to the end of the draft
    function shortcutAppend_Body(p_strShortcutName)
    {
    	return shortcutAppend(p_strShortcutName, draft.processTemplate('[[body]]'));
    }
    
    // Run a shortcut with the draft body as input and insert the result at the current cursor position
    function shortcutInsert_Body(p_strShortcutName)
    {
    	return shortcutInsert(p_strShortcutName, draft.processTemplate('[[body]]'));
    }
    
    
    // Run a shortcut with the draft body as input and and replace the draft content with the result
    function shortcutReplaceDraft_Body(p_strShortcutName)
    {
    	return shortcutReplaceDraft(p_strShortcutName, draft.processTemplate('[[body]]'));
    }
    
    // Run a shortcut with the draft body as input and and replace the draft body with the result
    function shortcutReplaceBody_Body(p_strShortcutName)
    {
    	return shortcutReplaceBody(p_strShortcutName, draft.processTemplate('[[body]]'));
    }
    
    // Run a shortcut with the selected text as input and return the result
    function shortcutRun_Selected(p_strShortcutName)
    {
    	return shortcutAppend(shortcutRun_Draft, draft.processTemplate('[[selection_only]]'));
    }
    
    // Run a shortcut with the selected text as input and append the result to the end of the draft
    function shortcutAppend_Selected(p_strShortcutName)
    {
    	return shortcutAppend(p_strShortcutName, draft.processTemplate('[[selection_only]]'));
    }
    
    // Run a shortcut with the selected text as input and insert the result at the current cursor position
    function shortcutInsert_Selected(p_strShortcutName)
    {
    	return shortcutInsert(p_strShortcutName, draft.processTemplate('[[selection_only]]'));
    }
    
    
    // Run a shortcut with the selected text as input and and replace the draft content with the result
    function shortcutReplaceDraft_Selected(p_strShortcutName)
    {
    	return shortcutReplaceDraft(p_strShortcutName, draft.processTemplate('[[selection_only]]'));
    }
    
    // Run a shortcut with the selected text as input and and replace the draft body with the result
    function shortcutReplaceBody_Selected(p_strShortcutName)
    {
    	return shortcutReplaceBody(p_strShortcutName, draft.processTemplate('[[selection_only]]'));
    }
  • script

    shortcutInsert_Selected("Drafts Return Test");
    

Options

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