Python script to replace audio device in VCV patch files

I didn’t know what category this belonged on. It came up in the VCVRack Discord, someone wanted to change the audio device in a bunch of patches.

VCV patch files are JSON, and plenty of languages can manipulate JSON. This is a python version.

One could use this basic strategy for other ‘find and modify’ operations on VCV files.

EDIT: If anyone cares, the ‘indent’ of 2 matches what Rack writes out. If you use an indent of 2, then you can use diff to see what’s changed & it just shows modified lines.

import json # for json
import sys   # for argv & exit
# expect a filename and devicename on command line
if len(sys.argv) < 3:
    print('missing arguments: filename newDevice')
filename = sys.argv[1]
device = sys.argv[2]
with open(filename, 'r') as f:   # open the VCV file
    data = json.load(f);              # parse it into a JSON dictionary
    for module in data['modules']:       # look for the module
        # This works for core AudioInterface, would need to change for any
        # other variety of audio interface module
        if module['plugin'] == 'Core' and module['model'] == 'AudioInterface':
            module['data']['audio']['deviceName'] = device

    outfile = filename + '.new' # write output
    with open(outfile, 'w') as fout:   # open output
        json.dump(data,fout,indent=2); # dump the json