[Share Experiences] 【V25】微信 / 钉钉 Linux 版本快捷键显隐窗口 ( 2026-05-17更新) Resolved
Tofloor
poster avatar
NoahLiu
deepin
2024-11-11 20:27
Author

基于 Opencode 转译,将 js 脚本换为 shell 脚本:

支持窗口关闭之后,通过 dbus 唤出隐藏的微信窗口

#!/bin/bash

pid=$(pgrep -x "wechat" || pgrep -f "/usr/bin/wechat")
active_id=$(xdotool getactivewindow)
ids=$(xdotool search -name "微信")
wechat_ids=$(xdotool search -pid "$pid")

has_active=false

for id in $ids; do
  for wid in $wechat_ids; do
    if [ "$id" = "$wid" ]; then
      if [ "$active_id" = "$id" ]; then
        has_active=true
        xdotool windowminimize "$id"
      fi
    fi
  done
done

if [ "$has_active" = false ]; then
  dbus-send --session --type=method_call \
    --dest="org.kde.StatusNotifierItem-${pid}-1" \
    /StatusNotifierItem \
    org.kde.StatusNotifierItem.Activate int32:0 int32:0
fi

补充一个钉钉的

#!/bin/bash

tmp_path="/tmp/dingtalkpid"

get_pid_and_service() {
  if [ -f "$tmp_path" ]; then
    local pid
    pid=$(cat "$tmp_path")
    if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
      echo "$pid"
      return
    fi
  fi

  local pids
  pids=$(pgrep -f com.alibabainc.dingtalk)
  if [ -z "$pids" ]; then
    return 1
  fi

  local dbus_names
  dbus_names=$(dbus-send --session --dest=org.freedesktop.DBus --type=method_call --print-reply /org/freedesktop/DBus org.freedesktop.DBus.ListNames 2>/dev/null | grep 'org.kde.StatusNotifierItem-' | awk -F'"' '{print $2}')

  local found=""
  for p in $pids; do
    if echo "$dbus_names" | grep -q "org.kde.StatusNotifierItem-${p}-1"; then
      found="$p"
      break
    fi
  done

  if [ -z "$found" ]; then
    return 1
  fi

  echo "$found" > "$tmp_path"
  echo "$found"
}

pid=$(get_pid_and_service)
if [ -z "$pid" ]; then
  exit 0
fi

active_id=$(xdotool getactivewindow)
ids=$(xdotool search -name "钉钉")
dingtalk_ids=$(xdotool search -pid "$pid")

has_active=false

for id in $ids; do
  for did in $dingtalk_ids; do
    if [ "$id" = "$did" ] && [ "$active_id" = "$id" ]; then
      has_active=true
      xdotool windowminimize "$id"
    fi
  done
done

if [ "$has_active" = false ]; then
  dbus-send --session --type=method_call \
    --dest="org.kde.StatusNotifierItem-${pid}-1" \
    /StatusNotifierItem \
    org.kde.StatusNotifierItem.Activate int32:0 int32:0
  dbus-send --session --type=method_call \
    --dest="org.kde.StatusNotifierItem-${pid}-1" \
    /StatusNotifierItem \
    org.kde.StatusNotifierItem.Activate int32:0 int32:0
fi

前置依赖

sudo apt install xdotool

给予脚本执行权限

chmod +x ~/.local/bin/toggle-dingtalk.sh

现阶段,QQ 、微信、钉钉都有原生linux版本了,只不过他们对快捷键的支持堪忧,所以用下面的方法,配合 deepin 窗口管理器最小化窗口的快捷键提升使用体验。

经验主体

本条经验基于 xdotool ,原理是使用 xdotool 根据窗口名称查找到对应 id 并根据 id 激活窗口。

xdotool 很强大,我刚刚接触,还有很多不懂,有兴趣的小伙伴可以自行学习探索

sudo apt install xdotool

根据窗口管理器上的窗口名称来搜索窗口ID

image.png

进入控制中心,点击下面的 + 添加新的快捷键
image.png

其中的 命令如下 字符串里面的名称来自于窗口管理器预览图上的名称,最新的 4.0 测试版 名称是 '微信(测试版)'

xdotool windowactivate $(xdotool search -name '微信(测试版)')

不足之处

因为是基于活动窗口查找的,当窗口被关闭 但是软件打开缩回托盘区的时候无解~

原理(更新)

基于进程查找窗口,跟激活的窗口对比,如果是微信窗口激活,就隐藏(最小化),如果激活窗口不是微信的窗口就通过 Dbus 通知微信激活窗口。

Reply Favorite View the author
All Replies
2 / 2
To page
avatar
NoahLiu
deepin
2025-03-14 13:34
#21
Oaklight
~$ xdotool windowactivate $(xdotool search -name '微信(测试版)')
XGetWindowProperty[_NET_WM_DESKTOP] failed (code=1)
~$ xdotool search --name '微信(测试版)' windowactivate
XGetWindowProperty[_NET_WM_DESKTOP] failed (code=1)

实际的窗口名称请鼠标悬浮窗口查看哈

Reply View the author
avatar
176******28
deepin
2025-03-14 22:40
#22
Reply View the author
2 / 2
To page