{
  "kind": "dsl",
  "metadata": {
    "title": "Export Sequence Markers To YouTube Chapter Text",
    "descriptionMarkdown": "Exports active-sequence markers as YouTube chapter text. You can choose whether each chapter title comes from the marker name, the marker comment, or both, and whether the result should be copied to the clipboard or saved as a `.txt` file. Markers whose resolved chapter text is empty are skipped."
  },
  "dsl": {
    "version": 0,
    "entry": "b1",
    "blocks": {
      "b1": {
        "type": "setVar",
        "inputs": {
          "name": "totalMarkerCount",
          "value": 0
        },
        "next": "b2"
      },
      "b2": {
        "type": "setVar",
        "inputs": {
          "name": "skippedEmptyTextCount",
          "value": 0
        },
        "next": "b3"
      },
      "b3": {
        "type": "setVar",
        "inputs": {
          "name": "exportedChapterCount",
          "value": 0
        },
        "next": "b4"
      },
      "b4": {
        "type": "setVar",
        "inputs": {
          "name": "fullText",
          "value": ""
        },
        "next": "b5"
      },
      "b5": {
        "type": "setVar",
        "inputs": {
          "name": "promptFields",
          "value": []
        },
        "next": "b6"
      },
      "b6": {
        "type": "listPush",
        "inputs": {
          "name": "promptFields",
          "value": {
            "type": "uiPromptField",
            "inputs": {
              "kind": "SELECT",
              "label": "Chapter title source",
              "var": "textSourceMode",
              "required": true,
              "options": [
                "Marker name",
                "Marker comment",
                "Marker name + comment"
              ],
              "defaultValue": "Marker name"
            }
          }
        },
        "next": "b7"
      },
      "b7": {
        "type": "listPush",
        "inputs": {
          "name": "promptFields",
          "value": {
            "type": "uiPromptField",
            "inputs": {
              "kind": "SELECT",
              "label": "Output",
              "var": "outputMode",
              "required": true,
              "options": [
                "Copy to clipboard",
                "Save to text file"
              ],
              "defaultValue": "Copy to clipboard"
            }
          }
        },
        "next": "b8"
      },
      "b8": {
        "type": "uiPromptForm",
        "inputs": {
          "title": "YouTube Chapter Export Options",
          "message": "Choose which marker text should be used for the chapter titles and where the result should go.",
          "fields": {
            "var": "promptFields"
          },
          "cancelBehavior": "STOP"
        },
        "next": "b9"
      },
      "b9": {
        "type": "setVar",
        "inputs": {
          "name": "exportPath",
          "value": ""
        },
        "next": "b10"
      },
      "b10": {
        "type": "if",
        "inputs": {
          "branches": [
            {
              "condition": {
                "type": "logicCompare",
                "inputs": {
                  "op": "EQ",
                  "a": {
                    "var": "outputMode"
                  },
                  "b": "Save to text file"
                }
              },
              "body": [
                {
                  "type": "setVar",
                  "inputs": {
                    "name": "exportPath",
                    "value": {
                      "type": "saveFileDialog",
                      "inputs": {
                        "suggestedName": "youtube-chapters.txt",
                        "types": [
                          "txt"
                        ],
                        "cancelBehavior": "STOP",
                        "initialDomain": "USER_DOCUMENTS"
                      }
                    }
                  }
                }
              ]
            }
          ]
        },
        "next": "b11"
      },
      "b11": {
        "type": "prForEachMarker",
        "inputs": {
          "owner": "::SEQ\\ACTIVE",
          "order": "ASC",
          "markerVar": "markerRef",
          "indexVar": "i"
        },
        "body": [
          {
            "type": "setVar",
            "inputs": {
              "name": "totalMarkerCount",
              "value": {
                "type": "mathArithmetic",
                "inputs": {
                  "op": "ADD",
                  "a": {
                    "var": "totalMarkerCount"
                  },
                  "b": 1
                }
              }
            }
          },
          {
            "type": "setVar",
            "inputs": {
              "name": "markerName",
              "value": {
                "type": "prGetMarkerProperty",
                "inputs": {
                  "marker": {
                    "var": "markerRef"
                  },
                  "property": "NAME"
                }
              }
            }
          },
          {
            "type": "setVar",
            "inputs": {
              "name": "markerComment",
              "value": {
                "type": "prGetMarkerProperty",
                "inputs": {
                  "marker": {
                    "var": "markerRef"
                  },
                  "property": "COMMENTS"
                }
              }
            }
          },
          {
            "type": "if",
            "inputs": {
              "branches": [
                {
                  "condition": {
                    "type": "logicCompare",
                    "inputs": {
                      "op": "EQ",
                      "a": {
                        "var": "textSourceMode"
                      },
                      "b": "Marker comment"
                    }
                  },
                  "body": [
                    {
                      "type": "setVar",
                      "inputs": {
                        "name": "markerText",
                        "value": {
                          "var": "markerComment"
                        }
                      }
                    }
                  ]
                },
                {
                  "condition": {
                    "type": "logicCompare",
                    "inputs": {
                      "op": "EQ",
                      "a": {
                        "var": "textSourceMode"
                      },
                      "b": "Marker name + comment"
                    }
                  },
                  "body": [
                    {
                      "type": "if",
                      "inputs": {
                        "branches": [
                          {
                            "condition": {
                              "type": "textIsEmpty",
                              "inputs": {
                                "value": {
                                  "var": "markerName"
                                }
                              }
                            },
                            "body": [
                              {
                                "type": "setVar",
                                "inputs": {
                                  "name": "markerText",
                                  "value": {
                                    "var": "markerComment"
                                  }
                                }
                              }
                            ]
                          },
                          {
                            "condition": {
                              "type": "textIsEmpty",
                              "inputs": {
                                "value": {
                                  "var": "markerComment"
                                }
                              }
                            },
                            "body": [
                              {
                                "type": "setVar",
                                "inputs": {
                                  "name": "markerText",
                                  "value": {
                                    "var": "markerName"
                                  }
                                }
                              }
                            ]
                          }
                        ],
                        "elseBody": [
                          {
                            "type": "setVar",
                            "inputs": {
                              "name": "markerText",
                              "value": {
                                "type": "textJoin",
                                "inputs": {
                                  "items": [
                                    {
                                      "var": "markerName"
                                    },
                                    " - ",
                                    {
                                      "var": "markerComment"
                                    }
                                  ]
                                }
                              }
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              ],
              "elseBody": [
                {
                  "type": "setVar",
                  "inputs": {
                    "name": "markerText",
                    "value": {
                      "var": "markerName"
                    }
                  }
                }
              ]
            }
          },
          {
            "type": "if",
            "inputs": {
              "branches": [
                {
                  "condition": {
                    "type": "textIsEmpty",
                    "inputs": {
                      "value": {
                        "var": "markerText"
                      }
                    }
                  },
                  "body": [
                    {
                      "type": "setVar",
                      "inputs": {
                        "name": "skippedEmptyTextCount",
                        "value": {
                          "type": "mathArithmetic",
                          "inputs": {
                            "op": "ADD",
                            "a": {
                              "var": "skippedEmptyTextCount"
                            },
                            "b": 1
                          }
                        }
                      }
                    }
                  ]
                }
              ],
              "elseBody": [
                {
                  "type": "if",
                  "inputs": {
                    "branches": [
                      {
                        "condition": {
                          "type": "logicCompare",
                          "inputs": {
                            "op": "GTE",
                            "a": {
                              "type": "prGetMarkerProperty",
                              "inputs": {
                                "marker": {
                                  "var": "markerRef"
                                },
                                "property": "START_SECONDS"
                              }
                            },
                            "b": 3600
                          }
                        },
                        "body": [
                          {
                            "type": "setVar",
                            "inputs": {
                              "name": "timeText",
                              "value": {
                                "type": "prFormatTime",
                                "inputs": {
                                  "timeSeconds": {
                                    "type": "prGetMarkerProperty",
                                    "inputs": {
                                      "marker": {
                                        "var": "markerRef"
                                      },
                                      "property": "START_SECONDS"
                                    }
                                  },
                                  "format": "H_MM_SS"
                                }
                              }
                            }
                          }
                        ]
                      }
                    ],
                    "elseBody": [
                      {
                        "type": "setVar",
                        "inputs": {
                          "name": "timeText",
                          "value": {
                            "type": "prFormatTime",
                            "inputs": {
                              "timeSeconds": {
                                "type": "prGetMarkerProperty",
                                "inputs": {
                                  "marker": {
                                    "var": "markerRef"
                                  },
                                  "property": "START_SECONDS"
                                }
                              },
                              "format": "M_SS"
                            }
                          }
                        }
                      }
                    ]
                  }
                },
                {
                  "type": "setVar",
                  "inputs": {
                    "name": "chapterLine",
                    "value": {
                      "type": "textJoin",
                      "inputs": {
                        "items": [
                          {
                            "var": "timeText"
                          },
                          " ",
                          {
                            "var": "markerText"
                          }
                        ]
                      }
                    }
                  }
                },
                {
                  "type": "if",
                  "inputs": {
                    "branches": [
                      {
                        "condition": {
                          "type": "logicCompare",
                          "inputs": {
                            "op": "GT",
                            "a": {
                              "var": "exportedChapterCount"
                            },
                            "b": 0
                          }
                        },
                        "body": [
                          {
                            "type": "setVar",
                            "inputs": {
                              "name": "fullText",
                              "value": {
                                "type": "textJoin",
                                "inputs": {
                                  "items": [
                                    {
                                      "var": "fullText"
                                    },
                                    "\n",
                                    {
                                      "var": "chapterLine"
                                    }
                                  ]
                                }
                              }
                            }
                          }
                        ]
                      }
                    ],
                    "elseBody": [
                      {
                        "type": "setVar",
                        "inputs": {
                          "name": "fullText",
                          "value": {
                            "var": "chapterLine"
                          }
                        }
                      }
                    ]
                  }
                },
                {
                  "type": "setVar",
                  "inputs": {
                    "name": "exportedChapterCount",
                    "value": {
                      "type": "mathArithmetic",
                      "inputs": {
                        "op": "ADD",
                        "a": {
                          "var": "exportedChapterCount"
                        },
                        "b": 1
                      }
                    }
                  }
                }
              ]
            }
          }
        ],
        "next": "b12"
      },
      "b12": {
        "type": "if",
        "inputs": {
          "branches": [
            {
              "condition": {
                "type": "logicCompare",
                "inputs": {
                  "op": "EQ",
                  "a": {
                    "var": "exportedChapterCount"
                  },
                  "b": 0
                }
              },
              "body": [
                {
                  "type": "if",
                  "inputs": {
                    "branches": [
                      {
                        "condition": {
                          "type": "logicCompare",
                          "inputs": {
                            "op": "EQ",
                            "a": {
                              "var": "totalMarkerCount"
                            },
                            "b": 0
                          }
                        },
                        "body": [
                          {
                            "type": "uiShowMessage",
                            "inputs": {
                              "message": "No sequence markers were found in the active sequence, so no YouTube chapter text was exported."
                            }
                          }
                        ]
                      }
                    ],
                    "elseBody": [
                      {
                        "type": "uiShowMessage",
                        "inputs": {
                          "message": "No YouTube chapter text was exported because all sequence markers resolved to empty chapter titles."
                        }
                      }
                    ]
                  }
                }
              ]
            },
            {
              "condition": {
                "type": "logicCompare",
                "inputs": {
                  "op": "EQ",
                  "a": {
                    "var": "outputMode"
                  },
                  "b": "Save to text file"
                }
              },
              "body": [
                {
                  "type": "writeTextFile",
                  "inputs": {
                    "path": {
                      "var": "exportPath"
                    },
                    "text": {
                      "var": "fullText"
                    },
                    "append": false,
                    "flushNow": true
                  }
                },
                {
                  "type": "uiShowMessage",
                  "inputs": {
                    "message": {
                      "type": "textJoin",
                      "inputs": {
                        "items": [
                          "Exported ",
                          {
                            "var": "exportedChapterCount"
                          },
                          " YouTube chapter line(s) to ",
                          {
                            "var": "exportPath"
                          },
                          ". Skipped empty chapter titles: ",
                          {
                            "var": "skippedEmptyTextCount"
                          },
                          "."
                        ]
                      }
                    }
                  }
                }
              ]
            }
          ],
          "elseBody": [
            {
              "type": "writeClipboardText",
              "inputs": {
                "text": {
                  "var": "fullText"
                }
              }
            },
            {
              "type": "uiShowMessage",
              "inputs": {
                "message": {
                  "type": "textJoin",
                  "inputs": {
                    "items": [
                      "Copied ",
                      {
                        "var": "exportedChapterCount"
                      },
                      " YouTube chapter line(s) to the clipboard. Skipped empty chapter titles: ",
                      {
                        "var": "skippedEmptyTextCount"
                      },
                      "."
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}
