Last active
January 27, 2021 12:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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