Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows]Incorrect shortcut created when running from a path with spaces #60

Open
Alex-Gunn opened this issue May 27, 2024 · 0 comments

Comments

@Alex-Gunn
Copy link

Alex-Gunn commented May 27, 2024

Many thanks for the module!

Conditions to reproduce: Windows 10 SandBox
Install: python-3.12.3-amd64
Run commands in command prompt:

mkdir c:\TestProj
cd TestProj
py -m pip install --upgrade pip
python -m venv venv
venv\Scripts\activate.bat
pip install pyshortcuts

Full contents of requirements.txt (installed modules) - just for sanity check:
pyshortcuts==1.9.0
pywin32==306

=====main.py contents===============

import os
import sys
from pyshortcuts import make_shortcut

lnk_name = "My application.lnk"
slug = "my-app"
script_directory = os.path.dirname(os.path.abspath(sys.argv[0]))
icon_path = "C:\\Windows\\SystemApps\\MicrosoftWindows.Client.CBS_cw5n1h2txyewy\\WindowsBackup\\Assets\\WindowsBackup.ico"
make_shortcut(os.path.join(script_directory, f"main.exe {slug}"),name=lnk_name,
        icon=icon_path, 
        terminal=False, desktop=True, startmenu=False, executable='')

=======main.py end=====================

Expected result (C:\Users\WDAGUtilityAccount\Desktop\My application.lnk) when running from C:\TestProj\ folder: C:\TestProj\python main.py
Generated shortcut points to: C:\TestProj\main.exe my-app

Unexpected result (C:\Users\WDAGUtilityAccount\Desktop\my application.lnk) when running from: C:\Program Files (x86)\TestProj:
C:\Program Files (x86)\TestProj\python main.py
Generated shortcut points to: C:\Windows\system32 C:\Program Files (x86)\TestProj\main.exe my-app

Unexpected result (C:\Users\WDAGUtilityAccount\Desktop\my application.lnk) when running from: C:\Path with spaces\TestProj
Generated shortcut points to: C:\Windows\system32 C:\Path with spaces\TestProj\main.exe my-app

Suggested fix in windows.py:
replace line in make_shortcut function
wscript.Targetpath = '"%s"' % executable

with the following:

if executable == '':
    path_to_exe = full_script[:full_script.rfind(" ")]
    full_script = full_script[full_script.rfind(" "):]
    wscript.Targetpath = '"%s"' % path_to_exe
else:
    wscript.Targetpath = '"%s"' % executable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant