copyq script export tab to json

JavaScript
[Command]
Command="
    /*
    
    Exports items from a tab to given directory:
    
      copyq 'exportJSON(\"./exported/\")'
      copyq tab url 'exportJSON(\"./url/\")'
    
    */
    
    function fromData(data)
    {
      var text = str(data)
      if ( data.equals(new ByteArray(text)) ) {
        if (text.indexOf('\\\\n') == -1)
          return text
        return { lines: text.split('\\\\n') }
      }
      return { base64: toBase64(data) }
    }
    
    global.exportJSON = function(path) {
      var indentation = '    '
    
      var dir = new Dir(path)
    
      if ( !dir.mkpath('.') )
        throw 'Failed to create directory: ' + path
    
      for (row = 0; row < count(); ++row) {
        var itemData = getItem(row)
    
        var item = {}
        for (var format in itemData)
          item[format] = fromData(itemData[format])
    
        var file = new File(dir.filePath(row + '.json'))
        if ( !file.openWriteOnly() )
          throw 'Failed to open file: ' + file.fileName()
    
        var json = JSON.stringify(item, null, indentation)
        if ( file.write(json) == -1 )
          throw 'Failed to write file: ' + file.fileName()
      }
    }"
Icon=\xf3b8
IsScript=true
Name=exportJSON()
Source

Also in JavaScript: