What is my current desktop environment?
Tested in Ubuntu 9.10:
>>> import os
>>> os.environ.get('DESKTOP_SESSION')
'gnome'
Edit 2: As comments say, this is beginning to be even less reliable with newer GNOME versions. I'm now also running Ubuntu 18.04, and it returns 'ubuntu' instead of the prior 'gnome'.
Edit 1: As mentioned in comments below, this approach will not work for more some OSes. The other two answers provide workarounds.
What's the environment variable for the path to the desktop?
I found that the best solution is to use a vbscript together with the batch file.
Here is the batch file:
@ECHO OFF
FOR /F "usebackq delims=" %%i in (`cscript findDesktop.vbs`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%
Here is findDesktop.vbs file:
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
wscript.echo(strDesktop)
There may be other solutions but I personally find this one less hackish.
I tested this on an English PC and also a French PC - it seems to work (Windows XP).
PowerShell Desktop Variable
You can use the Environment.GetFolderPath()
method to get the full path to special folders:
$DesktopPath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::Desktop)
This can be shortened to:
$DesktopPath = [Environment]::GetFolderPath("Desktop")
You can also get the "AllUsers" shared desktop folder (if the shortcut file is shared among all users):
[Environment]::GetFolderPath("CommonDesktopDirectory")
Check out the full list of values for the SpecialFolder
Enum.
Web desktops - do you find it interesting?
I find it an interesting experiment, but I don't really find much added value in it.
The desktop concept works for an operating system. Most people use one operating system. They are familiar with how it works and what to expect in terms of navigation. That being said, having a desktop at an application (or site) level adds another navigation model that the user must learn. If they are expecting it to be the same at their OS (which it won't likely be), then it can lead to confusion because it doesn't work exactly like what they're used to - although it looks like it should.
How to get a path to the desktop for current user in C#?
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
How to get Desktop location?
You can use os.environ["HOMEPATH"]
to get the path. Right now it's literally trying to find %HOMEPATH%/Desktop
without substituting the actual path.
Maybe something like:
shutil.copy(txtName, os.path.join(os.environ["HOMEPATH"], "Desktop"))
python, Windows 10: launching an application on a specific virtual desktop environment (work-spaces)
How do I tell python to launch an app but in Desktop 2 and 3?
This can be achieved by launching your applications with subprocess.Popen()
, then changing virtual desktop by calling GoToDesktopNumber()
from VirtualDesktopAccessor.dll
with the help of ctypes
, and launching your applications again. Tested with 64-bit Windows 10 Version 10.0.18363.720.
VirtualDesktopAccessor.dll
by Jari Pennanen exports the functions a part of the mostly undocumented (by Microsoft) Virtual Desktop API. Put the dll in the current working directory.
import ctypes, time, shlex, subprocess
def launch_apps_to_virtual_desktops(command_lines, desktops=3):
virtual_desktop_accessor = ctypes.WinDLL("VirtualDesktopAccessor.dll")
for i in range(desktops):
virtual_desktop_accessor.GoToDesktopNumber(i)
time.sleep(0.25) # Wait for the desktop to switch
for command_line in command_lines:
if command_line:
subprocess.Popen(shlex.split(command_line))
time.sleep(2) # Wait for apps to open their windows
virtual_desktop_accessor.GoToDesktopNumber(0) # Go back to the 1st desktop
command_lines = r"""
"C:\Program Files (x86)\Google\Chrome Beta\Application\chrome.exe"
"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" "C:\StudyGuide.pdf"
"C:\Program Files\Mozilla Firefox\firefox.exe"
"C:\Program Files\VideoLAN\VLC\vlc.exe"
""".splitlines()
launch_apps_to_virtual_desktops(command_lines)
The time.sleep()
calls are needed because Windows doesn't change virtual desktops instantly (presumably because of animations), and to give the processes time to create windows. You might need to tweak the timings.
Note that some applications only allow one instance/process, so you can't get multiple separate windows for each virtual desktop (e.g. Adobe Reader with default settings).
Another strategy I tried was launching the applications, sleeping for a bit to allow the windows to be created, then calling MoveWindowToDesktopNumber()
to move every window created by the new processes to different virtual desktops. The problem with that is, for applications like Chrome or Firefox, the new process is immediately closed if an existing process already exists, so it doesn't move the new windows (which actually belong to another, older process) to another desktop.
import ctypes, time, shlex, subprocess
from ctypes.wintypes import *
from ctypes import windll, byref
def get_windows(pid):
current_window = 0
pid_local = DWORD()
while True:
current_window = windll.User32.FindWindowExA(0, current_window, 0, 0)
windll.user32.GetWindowThreadProcessId(current_window, byref(pid_local))
if pid == pid_local.value:
yield current_window
if current_window == 0:
return
def launch_apps_to_virtual_desktops_by_moving(command_lines, desktops=3):
virtual_desktop_accessor = ctypes.WinDLL("VirtualDesktopAccessor.dll")
for i in range(desktops):
pids = []
for command_line in command_lines:
if command_line:
args = shlex.split(command_line)
pids.append(subprocess.Popen(args).pid)
time.sleep(3)
for pid in pids:
for window in get_windows(pid):
window = HWND(window)
virtual_desktop_accessor.MoveWindowToDesktopNumber(window, i)
command_lines = r"""
"C:\Program Files (x86)\Google\Chrome Beta\Application\chrome.exe"
"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" "C:\StudyGuide.pdf"
"C:\Program Files\Mozilla Firefox\firefox.exe"
"C:\Program Files\VideoLAN\VLC\vlc.exe"
""".splitlines()
launch_apps_to_virtual_desktops_by_moving(command_lines)
Keep a application window always on current desktop on linux and mac
Thanks to Jan Kundrát for his help (Previous comment https://stackoverflow.com/a/16777496/1045832 ).
Solution for linux X11 :
#ifdef Q_WS_X11 //only define on Qt 4.X
#include <QX11Info> //Only on Qt 4.X , return expected in Qt 5.1
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#endif
YourWidget::YourWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::YourWidget)
{
#ifdef Q_WS_X11 //only define on Qt 4.X
unsigned long data = 0xFFFFFFFF;
XChangeProperty (QX11Info::display(),
winId(),
XInternAtom(QX11Info::display(), "_NET_WM_DESKTOP", False),
XA_CARDINAL,
32,
PropModeReplace,
reinterpret_cast<unsigned char *>(&data), // all desktop
1);
#endif
}
Put this on your .pro
unix:!macx {
LIBS += -lX11
}
Solution for macos X :
#include <objc/objc-runtime.h>
WId windowObject = this->winId();
objc_object * nsviewObject = reinterpret_cast<objc_object *>(windowObject);
objc_object * nsWindowObject = objc_msgSend(nsviewObject, sel_registerName("window"));
int NSWindowCollectionBehaviorCanJoinAllSpaces = 1 << 0;
objc_msgSend(nsWindowObject, sel_registerName("setCollectionBehavior:"), NSWindowCollectionBehaviorCanJoinAllSpaces);
Put this on your .pro
macx {
LIBS += -lobjc
}
Related Topics
Why Is Signal.Sigalrm Not Working in Python on Windows
Standard Way to Open a Folder Window in Linux
Correct Daemon Behaviour (From Pep 3143) Explained
A Function Callback Every Time a Key Is Pressed (Regardless of Which Window Has Focus)
Kill Python Interpeter in Linux from the Terminal
Differencebetween C.Utf-8 and En_Us.Utf-8 Locales
Capturing Output of Python Script Run Inside a Docker Container
How to Default Python3.8 on My MAC Using Homebrew
How to Delete a Directory Created with Tempfile.Mkdtemp
How to Get Each Dependent Command Execution Output Using Paramiko Exec_Command
Attribute Bold Doesn't Seem to Work in My Curses
How to Cleanly Kill Subprocesses in Python
Why Not Just Use 'Shell=True' in Subprocess.Popen in Python
Typeerror: Argument 1 Must Be Pygame.Surface, Not Str How to Fix
How to Install a Script to Run Anywhere from the Command Line