showing results for - "copyq script export tab to json"
Callan
18 Jan 2016
1[Command]
2Command="
3    /*
4    
5    Exports items from a tab to given directory:
6    
7      copyq 'exportJSON(\"./exported/\")'
8      copyq tab url 'exportJSON(\"./url/\")'
9    
10    */
11    
12    function fromData(data)
13    {
14      var text = str(data)
15      if ( data.equals(new ByteArray(text)) ) {
16        if (text.indexOf('\\\\n') == -1)
17          return text
18        return { lines: text.split('\\\\n') }
19      }
20      return { base64: toBase64(data) }
21    }
22    
23    global.exportJSON = function(path) {
24      var indentation = '    '
25    
26      var dir = new Dir(path)
27    
28      if ( !dir.mkpath('.') )
29        throw 'Failed to create directory: ' + path
30    
31      for (row = 0; row < count(); ++row) {
32        var itemData = getItem(row)
33    
34        var item = {}
35        for (var format in itemData)
36          item[format] = fromData(itemData[format])
37    
38        var file = new File(dir.filePath(row + '.json'))
39        if ( !file.openWriteOnly() )
40          throw 'Failed to open file: ' + file.fileName()
41    
42        var json = JSON.stringify(item, null, indentation)
43        if ( file.write(json) == -1 )
44          throw 'Failed to write file: ' + file.fileName()
45      }
46    }"
47Icon=\xf3b8
48IsScript=true
49Name=exportJSON()