关于获取“显示应用程序”按钮及Dash to Dock侧边栏中应用名称的技术问询
Hey there! Great question—your initial approach of checking /usr/share/applications makes sense, but as you noticed, that directory includes every desktop app installed on your system (even hidden ones), which is why it’s way more than what shows up in "Show Applications" or Dash to Dock. Let’s break down how to get exactly the apps you’re looking for:
1. 获取“显示应用程序”网格中的所有应用名称
The apps in the "Show Applications" grid are the ones that aren’t marked as hidden. Desktop files can have a NoDisplay=true line to hide them from the app list. To filter out these hidden apps, we can use grep to find desktop files that don’t have this flag:
命令示例:
# 列出系统级可见应用 grep -L "NoDisplay=true" /usr/share/applications/*.desktop | xargs basename -s .desktop # 列出用户级可见应用(比如你自己安装的非系统应用) grep -L "NoDisplay=true" ~/.local/share/applications/*.desktop 2>/dev/null | xargs basename -s .desktop
解释:
-Lingrepreturns files that don’t contain the matching line ("NoDisplay=true").basename -s .desktopstrips the.desktopextension to get just the app name.- The
2>/dev/nullin the second command suppresses errors if the user applications directory is empty.
2. 获取Dash to Dock侧边栏中的应用名称
Dash to Dock shows two types of apps: pinned apps (the ones you’ve manually added to the dock) and frequently used apps. Let’s cover both:
获取固定在Dock上的应用
GNOME stores pinned apps in its dconf database. You can retrieve them with gsettings, then parse the output to get clean app names:
gsettings get org.gnome.shell favorite-apps | tr -d "[]'," | xargs -I {} basename {} .desktop
解释:
gsettings get org.gnome.shell favorite-appsoutputs a list like['firefox.desktop', 'org.gnome.Nautilus.desktop'].tr -d "[]',"removes the brackets, quotes, and commas to clean up the list.basename {} .desktopagain strips the.desktopextension for readable names.
获取Dash to Dock中的常用应用
Unfortunately, retrieving frequently used apps is a bit trickier—GNOME Shell tracks usage stats in dconf, but the data is stored in a less straightforward format. If you really need this, you can check the dconf key org.gnome.shell.app-switcher.pinned-apps (though this might not be exactly what Dash to Dock uses for frequent apps). Alternatively, you’d need to parse usage logs, which isn’t as clean as the pinned app command.
总结
- Use the
grepcommands to get all visible apps in "Show Applications". - Use the
gsettingscommand to get pinned apps in Dash to Dock. - Your original
ls -l /usr/share/applicationsshows all desktop files, including hidden ones—hence the extra entries.
备注:内容来源于stack exchange,提问作者rtciv

