Skip to content

Instantly share code, notes, and snippets.

@flow4u
Last active January 27, 2021 12:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flow4u/bf27f88733f8133ce7733f764a6674e6 to your computer and use it in GitHub Desktop.
Save flow4u/bf27f88733f8133ce7733f764a6674e6 to your computer and use it in GitHub Desktop.
Python: MyDRE easy open session with 2 monitors (mutlimonitor is restricted to 2), also works with 1
"""
Version 0.5.2
After compyling with pyinstaller the exe picks up the latest downloaded RDP file, adds a few lines of code
to MyDRE.rdp to ensure 2 monitors (and only 2 monitors) are used (monitor 0 and monitor 1). Leaving a
3rd free for other things.
It runs MyDRE.rdp and it also removes the downloaded RDP file.
The exe can be added to the windows bar, download an RDP and press the shortcut on the windows bar.
"""
import os
from os import walk
import pathlib
def get_download_path():
"""Returns the default downloads path for linux or windows"""
if os.name == 'nt':
import winreg
sub_key = r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
downloads_guid = '{374DE290-123F-4565-9164-39C4925E467B}'
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, sub_key) as key:
location = winreg.QueryValueEx(key, downloads_guid)[0]
return location
else:
"""Linux and Mac"""
return os.path.join(os.path.expanduser('~'), 'downloads')
def latest_rdp():
"""Return the latest RDP folder+file in downloads"""
# f = []
mypath = get_download_path()
target = ''
for (dirpath, dirnames, filenames) in walk(mypath):
for filename in filenames:
fname = pathlib.Path(dirpath + '/' + filename)
mtime = fname.stat().st_mtime
try:
if pathLib.Path(target).stat().st_mtime < mtime:
target = dirpath + '/' + filename
except:
target = dirpath + '/' + filename
return target
def read_latest_rdp(rdp_file):
"""Returns the lines in the rdp_file"""
with open(rdp_file) as f:
lines = f.read().splitlines()
return lines
# rdp_lines are the lines in the latest RDP file
last_rdp = latest_rdp()
if last_rdp != '':
if last_rdp != 'mydre.rdp':
rdp_lines = read_latest_rdp(last_rdp)
# additional lines add special power to the RDP
additional_lines = ['span monitors:i:1',
'use multimon:i:1',
'selectedmonitors:s:0,1']
rdp_lines.extend(additional_lines)
# creating the mydre.rdp
mydre_rdp = get_download_path() + '/' + 'mydre.rdp'
with open(mydre_rdp, 'w') as f:
f.writelines('\n'.join(rdp_lines) + '\n')
# Open de RDP-file with prompt for password
os.system('mstsc ' + mydre_rdp)
os.remove(last_rdp)
@flow4u
Copy link
Author

flow4u commented Jan 27, 2021

You can download a nice icon: app.ico.

With Pyinstaller it can be made in executable for Mac or Linux.

A windows .bat version can be found here.
A Powershell version can be found here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment