[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
1 / 2
To page
avatar
大铁棍子医院仝主任
deepin
2024-11-11 20:47
#1

我借楼稍微补充下:

x11 下也可以考虑用 wmctrl
wayland 下可以考虑用 wlrctl

Reply View the author
avatar
NoahLiu
deepin
2024-11-11 21:41
#2
大铁棍子医院仝主任

我借楼稍微补充下:

x11 下也可以考虑用 wmctrl
wayland 下可以考虑用 wlrctl

感谢补充,之前论坛搜索关键字,并没有找到相关分享。这下应该是凑齐了。现在我还是简单的使用单条命令后续有功夫 研究下 shell 脚本,或者nodejs脚本(这个好像不大通用,但是我没有学习障碍😂 )。希望能够有更好的体验。

Reply View the author
avatar
Ligocut光剪视频剪辑软件
deepin
Backbone of ecological co-construction group
2024-11-11 21:58
#3

貌似可以基于进程来搜索?

为什么你用的微信是测试版?

现在不是都有原声版的微信了吗?

Reply View the author
avatar
NoahLiu
deepin
2024-11-11 22:05
#4
Ligocut光剪视频剪辑软件

貌似可以基于进程来搜索?

为什么你用的微信是测试版?

现在不是都有原声版的微信了吗?

商店首页推荐的 4.0 Linux 原生版本哟,这个测试版字样是软件给窗口加上的,你观察下你的预览窗口(鼠标悬浮图标),可能也是一样的。

Reply View the author
avatar
NoahLiu
deepin
2024-11-11 22:07
#5
Ligocut光剪视频剪辑软件

貌似可以基于进程来搜索?

为什么你用的微信是测试版?

现在不是都有原声版的微信了吗?

貌似可以基于进程搜索

xdotool 还没深入了解使用,感谢提醒,到时候去仔细研究下,看能否提高运行效率。

Reply View the author
avatar
okk~
deepin
2024-11-12 01:16
#6

感谢分享

Reply View the author
avatar
Oli
deepin
2024-11-12 03:03
#7

like like

Reply View the author
avatar
NoahLiu
deepin
2024-11-12 08:20
#8
NoahLiu

商店首页推荐的 4.0 Linux 原生版本哟,这个测试版字样是软件给窗口加上的,你观察下你的预览窗口(鼠标悬浮图标),可能也是一样的。

今天来公司发现,公司的微信窗口不叫微信测试版~ 就是微信,不知道是不是安装适合先现在了老版本的微信 linux

Reply View the author
avatar
xuqi
deepin testing team
2024-11-13 15:36
#9
  • 感谢分享该方法~👍
Reply View the author
avatar
9136
deepin
2024-11-13 18:05
#10

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

Reply View the author
avatar
小鱼贝壳
deepin
2024-11-14 06:47
#11

like like

Reply View the author
avatar
NoahLiu
deepin
2024-11-14 09:12
#12
9136

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

你这个命令更简洁

Reply View the author
avatar
码梦天涯
deepin
2024-11-20 14:24
#13
NoahLiu

你这个命令更简洁

就是少了个toggle效果

Reply View the author
avatar
NoahLiu
deepin
2024-11-22 09:57
#14
码梦天涯

就是少了个toggle效果

toggle 需要用到脚本了,一条命令搞不定呀

Reply View the author
avatar
Oaklight
deepin
2024-12-03 12:34
#15
9136

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

~$ 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
阿顺呀
deepin
2024-12-24 15:48
#16

核心是这一条:

xdotool windowactivate $(xdotool search -name '微信')

最小化到任务栏没问题,但是最小化到托盘就不行了

Reply View the author
avatar
码梦天涯
deepin
2024-12-25 21:16
#17
NoahLiu

toggle 需要用到脚本了,一条命令搞不定呀

我写了个脚本的,另开了个帖子🤤

Reply View the author
avatar
NoahLiu
deepin
2024-12-31 11:19
#18
码梦天涯

我写了个脚本的,另开了个帖子🤤

应该留下个连接的,shell更实用

shell 版本

Reply View the author
avatar
NoahLiu
deepin
2024-12-31 11:20
#19
阿顺呀

核心是这一条:

xdotool windowactivate $(xdotool search -name '微信')

最小化到任务栏没问题,但是最小化到托盘就不行了

目前只能通过窗口管理的方式,暂时没找到控制托盘程序的方式了

通过大模型搜索了,没找到可用的方案。如果有欢迎分享

Reply View the author
avatar
码梦天涯
deepin
2025-01-02 11:03
#20
NoahLiu

应该留下个连接的,shell更实用

shell 版本

我怎么就忘了呢🤤

Reply View the author
1 / 2
To page