다음을 수행하는 다음 파이썬 스크립트를 만들었습니다.
- ID와 상관없이 기본 장치를 목록의 다음 장치 (랩 어라운드)로 전환
- 실행중인 모든 응용 프로그램을이 장치로 이동합니다.
- 장치 이름을 사용하여 GUI에 알림을 보냅니다.
#!/usr/bin/env python3
import subprocess
# Toggle default device to the next device (wrap around the list)
cards_info = subprocess.run(['pacmd','list-sinks'], stdout=subprocess.PIPE)
card_indexes = subprocess.run(['grep', 'index'], stdout=subprocess.PIPE, input=cards_info.stdout)
indexes_list = card_indexes.stdout.decode().splitlines()
card_descriptions = subprocess.run(['grep', 'device.description'], stdout=subprocess.PIPE, input=cards_info.stdout)
indices = [i for i, s in enumerate(indexes_list) if '*' in s]
if (len(indices) != 1):
print("Error finding default device")
exit(1)
default_index = indices[0]
next_default = 0
if (default_index != (len(indexes_list) - 1)):
next_default = default_index + 1
next_default_index = (indexes_list[next_default].split("index: ",1)[1])
subprocess.run(['pacmd','set-default-sink %s' %(next_default_index)], stdout=subprocess.PIPE)
# Move all existing applications to the new default device
inputs_info = subprocess.run(['pacmd','list-sink-inputs'], stdout=subprocess.PIPE)
inputs_indexes = subprocess.run(['grep', 'index'], stdout=subprocess.PIPE, input=inputs_info.stdout)
inputs_indexes_list = inputs_indexes.stdout.decode().splitlines()
for line in inputs_indexes_list:
input_index = (line.split("index: ",1)[1])
subprocess.run(['pacmd','move-sink-input %s %s' %(input_index, next_default_index)], stdout=subprocess.PIPE)
# Send notification to the GUI
descriptions_list = card_descriptions.stdout.decode().splitlines()
if (len(descriptions_list) == len(indexes_list)):
description = (descriptions_list[next_default].split("= ",1)[1])[1:-1]
args = ["notify-send", "Default audio card changed", 'Default audio card was set to %s' %(description)]
subprocess.run(args, stdout=subprocess.PIPE)
스크립트에 키보드 단축키를 할당했으며 이제 내 인생은 행복합니다.