Action

Run Previous Action

Posted by David Degner, Last update 3 months ago - Unlisted

Re-runs the last logged action on this draft, excluding the current action.

Most usefull with a keyboard shortcut to quickly repeat the last thing.

Steps

  • script

    // Rerun the most recent *different* action logged on this draft.
    // Notes:
    // - Requires actions you want to rerun are set to log.
    // - Uses app.queueAction, so it runs after this action completes.
    
    function mostRecentOtherActionForDraft(d) {
      if (!d || !d.actionLogs || d.actionLogs.length === 0) return undefined;
    
      // Defensive: sort logs newest-first by executedAt when available
      var logs = d.actionLogs.slice();
      logs.sort(function(a, b) {
        var at = (a && a.executedAt && a.executedAt.getTime) ? a.executedAt.getTime() : 0;
        var bt = (b && b.executedAt && b.executedAt.getTime) ? b.executedAt.getTime() : 0;
        return bt - at;
      });
    
      // Current action UUID if available (Drafts provides `action` in action steps)
      var currentUuid = (typeof action !== "undefined" && action && action.uuid) ? action.uuid : null;
    
      for (var i = 0; i < logs.length; i++) {
        var log = logs[i];
        if (!log) continue;
    
        var a = log.action;
        if (!a) continue; // action might not exist anymore, or wasn't recorded
    
        // Skip re-queuing the current action to avoid infinite loops
        if (currentUuid && a.uuid === currentUuid) continue;
    
        return a;
      }
    
      return undefined;
    }
    
    var prev = mostRecentOtherActionForDraft(draft);
    
    if (!prev) {
      alert("No previous logged action found for this draft (or it no longer exists).");
      context.cancel("No previous logged action found.");
    } else {
      var ok = app.queueAction(prev, draft);
      if (!ok) {
        alert("Failed to queue previous action: " + (prev.name ? prev.name : "(unnamed action)"));
        context.fail();
      }
    }
    
    // No top-level return in Drafts script steps.
    

Options

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