[Share Experiences] 【V23】微信Linux 版本快捷键显隐窗口 (2025-3-14 更新) Resolved
Tofloor
poster avatar
NoahLiu
deepin
2024-11-11 20:27
Author

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

let { exec } = require('child_process');
const { promisify } = require("util");
exec = promisify(exec);

async function run (cmdstr) {
  let { stdout } = await exec(cmdstr);
  let results = stdout.split('\n').filter(item => !!item);
  if (results.length === 0) {
    return null;
  } else if (results.length === 1) {
    return results[0];
  } else {
    return results;
  }
}

async function getWechatPID () {
  return await run(`pgrep -x "wechat" || pgrep -f "/usr/bin/wechat"`);
}

async function searchWindow () {
  return await run(`xdotool search -name "微信"`);
}

async function getWechatWindow (pid) {
  return await run(`xdotool search -pid ${pid}`);
}

async function activateWindow (pid) {
  await run(`dbus-send --session --type=method_call \
    --dest="org.kde.StatusNotifierItem-${pid}-1" \
    /StatusNotifierItem \
    org.kde.StatusNotifierItem.Activate int32:0 int32:0`);
}
async function deactivateWindow (id) {
  await run(`xdotool windowminimize ${id}`);
}

async function getActivateWindow () {
  return await run(`xdotool getactivewindow`);
}
async function main () {
  let pid = await getWechatPID();
  let activeId = await getActivateWindow();
  let ids = await searchWindow();
  let wechatIds = await getWechatWindow(pid);
  let hasActive = false;
  if (!Array.isArray(wechatIds)) {
    wechatIds = [wechatIds];
  }
  if (!Array.isArray(ids)) {
    ids = [ids];
  }

  ids.filter(id => {
    return wechatIds.includes(id);
  }).forEach(async (id) => {
    if (activeId === id) {
      hasActive = true;
      await deactivateWindow(id);
    }
  });

  if (!hasActive) {
    await activateWindow(pid);
  }
}

main();

假设你的 node 有一个 全局的快捷连接 在 /usr/bin/node 这个位置,上面的js 文件在 /home/user/window.js

/usr/bin/node ~/wechat.js

现阶段,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
DtTheme
deepin
2024-11-11 20:47
#1

我借楼稍微补充下:

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

Reply View the author
NoahLiu
deepin
2024-11-11 21:41
#2
DtTheme

我借楼稍微补充下:

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

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

Reply View the author
把一切操作变成GUI
deepin
Backbone of ecological co-construction group
2024-11-11 21:58
#3

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

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

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

Reply View the author
NoahLiu
deepin
2024-11-11 22:05
#4
把一切操作变成GUI

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

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

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

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

Reply View the author
NoahLiu
deepin
2024-11-11 22:07
#5
把一切操作变成GUI

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

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

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

貌似可以基于进程搜索

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

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

感谢分享

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

like like

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

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

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

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

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

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

like like

Reply View the author
NoahLiu
deepin
2024-11-14 09:12
#12
我是昵称

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

你这个命令更简洁

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

你这个命令更简洁

就是少了个toggle效果

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

就是少了个toggle效果

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

Reply View the author
Oaklight
deepin
2024-12-03 12:34
#15
我是昵称

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

核心是这一条:

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

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

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

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

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

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

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

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

shell 版本

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

核心是这一条:

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

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

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

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

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

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

shell 版本

我怎么就忘了呢🤤

Reply View the author
1 / 2
To page