6

I want to write a script that translates devilspie's window rules into compiz' rules. Compiz settings can be changed by gsettings keys in path org.compiz.profiles.unity.plugins.place. There are three keys: viewport-matches, viewport-x-values and viewport-y-values. Unfortunately each key is an array, and the index of the elements matters.

Gsettings lacks any support of array types other than rewriting whole array at once, and I need to place item in the array at the specific location (say, at the beginning, index '1').

I know, that if I don't care about the index, I can use

gsettings set ${schema} ${key} \"`gsettings get ${schema} ${key} | sed s/.$//`, ${value}]\"

The question complements How to remove element from gsettings array in script?

1
  • @SylvainPineau I know it. But I wouldn't accept it before I know it works. And some other things forced me to divide me spare time even thinner. And, dear Sylvian, I am very grateful for your work: your answer actually exceeded my expectations; it is rare on Stack Exchange to get a custom-made scripts for an answer. And you did them in good style, too! In my opinion +1 is way to little to fully express the amount of help you've given me. May 13, 2014 at 9:46

2 Answers 2

3

The following python3 script will insert new element(s) at a given index:

#!/usr/bin/env python3

import argparse
import subprocess

parser = argparse.ArgumentParser()
parser.add_argument("schema", help="gsettings shema", metavar="SCHEMA")
parser.add_argument("key", help="gsettings key", metavar="KEY")
parser.add_argument("index",
                    help="KEY array index where VALUE(s) need to be inserted",
                    metavar="INDEX", type=int)
parser.add_argument("value",
                    help="gsettings VALUE(s) to add to the KEY array",
                    metavar="VALUE", nargs='*')

args = parser.parse_args()

array = eval(subprocess.check_output(["gsettings", "get", args.schema, args.key]))
for v in sorted(args.value, reverse=True):
    try:
        value = eval(v)
    except NameError:
        value = v
    array.insert(args.index, value)
subprocess.call(["gsettings", "set", args.schema, args.key, str(array)])
1
  • Can you explain a little bit more, how I should use your script? ;)
    – A.B.
    Jul 9, 2015 at 5:57
3

You can also do this via a bash script:

#!/bin/bash

SCHEMA="your.wanted.schema"
KEY="your-wanted-key"

STATUS=$(gsettings get ${SCHEMA} ${KEY})

# Optional proof, whether value to add already exsists
if [[ $STATUS == *"'to.add.value'"* ]]; then
    echo "to.add.value is already in the list!"
else
    gsettings set ${SCHEMA} ${KEY} "${STATUS%]*}, 'to.add.value']"
    echo "Added to.add.value to the list."
fi

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .