如何在 YAD 中选择一个选项并按下“确定”按钮时执行命令?

如何在 YAD 中选择一个选项并按下“确定”按钮时执行命令?

我正在使用 YAD 创建带有 GUI 的“应用商店”,但我找不到任何关于如何在从列表中选择一个选项并单击“确定”按钮(“确定”将被替换为“安装”)时执行命令的教程。我只想使用 shell 脚本。

这是我写的脚本。这是一个简单的列表,包含 2 列和 2 个项目。Windows 图标只是一个测试。

yad --list --center --width=800 --height=600 
--title "apps4pi" \
--column "app name" --column "description" \
SimpleScreenRecorder " powerful feature packed yet simple and easy to use screen recorder" \
System-Tools "system maintenance commands all done for you"

答案1

以下代码显示如何对选定的行运行某些操作:

#!/bin/bash

trap 'rm -f "$tmpfile"' EXIT

# you can also double-click on a row to "install" it.

tmpfile=$(mktemp -p /dev/shm)
if output=$(yad \
--button='Exit!application-exit:1' \
--button='Exit and Install!system-run:0' \
--button="Install!system-run:/bin/sh -c \"cat $tmpfile >&2\"" \
--list \
--dclick-action="/bin/sh -c \"printf \%\s'\n' %s >&2\"" \
--select-action="/bin/sh -c \"printf \%\s'\n' %s >$tmpfile\"" \
--separator='\n' \
--center --width=800 --height=600 --title "apps4pi" \
--column "app name" --column "description" \
SimpleScreenRecorder "powerful feature packed yet simple and easy to use screen recorder" \
System-Tools "system maintanance commands all done for you"); then
    printf '%s\n' "$output" >&2
fi

示例命令在标准错误设备中逐列打印行的列。“退出并安装”按钮通过退出yad并使用其输出来执行命令。“安装”按钮从临时文件中获取选择,因此它不必等待输出;--select-action用于更新该文件。您还可以在选择时以双击操作的形式运行该操作;您不需要临时文件。man yad有关选项的更多文档,请参阅。

至于我用于按钮的图标名称,我从freedesktop.org 的图标命名规范

如果你希望“安装”仅在退出后发生yad,并且在选择“安装”时退出并安装,请使用

#!/bin/bash

if output=$(yad \
--button='Exit!application-exit:1' \
--button='Install!system-run:0' \
--list \
--separator='\n' \
--center --width=800 --height=600 --title "apps4pi" \
--column "app name" --column "description" \
SimpleScreenRecorder "powerful feature packed yet simple and easy to use screen recorder" \
System-Tools "system maintanance commands all done for you"); then
    printf '%s\n' "$output" >&2
fi

您可以使用选项将列分隔符更改为您喜欢的--separator,或者使用默认的|printf用您的命令替换该行。

相关内容