Friday, April 26, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 2338  / 2 Years ago, fri, may 6, 2022, 2:58:20

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?


More From » command-line

 Answers
5

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)])

[#25398] Friday, May 6, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
fieldcheon

Total Points: 24
Total Questions: 105
Total Answers: 109

Location: Western Sahara
Member since Tue, Feb 16, 2021
3 Years ago
fieldcheon questions
Fri, Apr 1, 22, 05:58, 2 Years ago
Wed, Feb 2, 22, 03:31, 2 Years ago
Sat, Aug 14, 21, 01:24, 3 Years ago
;