How woud I extract the data from a shortcut in windows to a python file?

How would I go about extracting the data of a shortcut into a script to be executed within the script?

I have Alienware Command Center on my computer and executing the actual executable is causing me an issue but the shortcut doesn’t give me any trouble.I’ve compiled the script and right now my code is set up to os.startfile the shortcut and the shortcut is separate from the executable. I want to embed the shortcut into the script itself and execute the shortcut from the script.

Are you talking about a Windows shortcut file? You’d need pywin32, or some similar third-party library, in order to interact with the Windows API. Normal functionality doesn’t treat the shortcut file as a separate file that contains actual data. But that’s assuming that “extract the data from a shortcut” is actually the problem you need to solve, or indeed makes any sense to think about here.

On Linux (assuming a window manager supporting XDG) this is straightforward; you can open a .desktop file directly just like any other. But since you mentioned os.startfile I assume you’re talking about Windows.

If you’re talking about a .bat file, then what you’re really trying to do is get the effect of the commands in that script, using Python code. To solve that, we’d need to see the script.

It’s hard to imagine why using os.startfile on an actual Desktop shortcut (.lnk file, although normally Windows won’t show you the extension nor let you treat the file as an ordinary file) would work but the actual executable path wouldn’t. The other way around is much easier to imagine (because shortcut links can get broken).

It’s hard to follow your description without a concrete example of what you have so far.

I don’t know exactly how to explain this. Alienware command center sits in the application folder, the secure folder in Windows that has massive restrictions to it. That’s where the shortcut sits. It is part of Windows 11 start menu interface. The shortcuts in the applications folder don’t directly point to an executable. You can’t even change the target path in the properties of the shortcut.

The Alienware command center sits running in the background. When you double click on the shortcut to launch Alienware command center it just moves it from the background to the foreground. Alienware command center also has services that have to be loaded for the program to run.

What I’ve done with my code is set it up to launch Alienware command center when my computer starts. I’ve had complications with executing awcc.exe from python. I was forced to use the shortcut from the windows 11 start menu with os.startfile instead of using os.startfile to load the awcc.exe file.

I have no idea what Alienware command center is doing in the background but for some reason launching the executable itself sometimes doesn’t work but the shortcut works every single time.

from datetime import datetime
from os import startfile
from time import sleep
from typing import Optional

from psutil import Process
from pyvda import AppView, VirtualDesktop
from win32con import HWND_TOP
from win32gui import GetWindowRect, SetWindowPos

from dependencies import get_object, LoadingWindow, Monitor

Monitor = Monitor()


class AlienwareCommandCenter:
    def __init__(self):
        self._awcc: Optional[AppView] = self()

    def __call__(self) -> Optional[AppView]:
        try:
            getattr(self._awcc, 'hwnd')
        except:  # noqa
            self._awcc = None

            while self._awcc is None:
                if 'timestamp' not in locals() or datetime.now().timestamp() >= timestamp:  # noqa
                    timestamp = datetime.now().timestamp() + 1
                    startfile(r'dependencies\AlienwareCommandCenter.lnk')

                self._awcc = get_object(object_name = 'Alienware Command Center',  object_type = AppView, current_desktop = False)
                sleep(1)
            sleep(1)
        return self._awcc


# window = LoadingWindow()
# window.read(timeout = 60000)
# window.close()
# exit()


def main():
    window: LoadingWindow | None = None

    if get_object(object_name = 'AWCC.Service.exe', parent_name = 'services.exe', object_type = Process) is None:
        window = LoadingWindow()

        while (get_object(object_name = 'AWCC.Service.exe', parent_name = 'services.exe', object_type = Process) is None or
               get_object(object_name = 'AWCC.Background.Server.exe', parent_name = 'AWCC.Service.exe', object_type = Process) is None or
               get_object(object_name = 'OCControl.Service.exe', parent_name = 'AWCC.Service.exe', object_type = Process) is None or
               get_object(object_name = 'AWCC.exe', parent_name = 'svchost.exe', object_type = Process) is None):
            window.read(timeout = 1000)

        sleep(10)

    if window is not None:
        window.close()

    awcc = AlienwareCommandCenter()
    Desktop: Optional[VirtualDesktop] = get_object(object_name = 'Alienware Command Center', parent_name = 'svchost.exe', object_type = VirtualDesktop)

    if Desktop is not None and VirtualDesktop.current().id != Desktop.id:
        Desktop.go()

    if awcc() is not None and Desktop is not None:
        if awcc().desktop.name != Desktop.name:
            awcc().move(Desktop)

        if 3840 in Monitor:
            if GetWindowRect(awcc().hwnd) != (530, 20, 3310, 1580):
                SetWindowPos(awcc().hwnd, HWND_TOP, 530, 20, 2780, 1560, 0)
        else:
            if 1080 not in Monitor.WorkingArea:
                SetWindowPos(awcc().hwnd, HWND_TOP, int((Monitor.WorkingArea.Width - 1745) / 2), 10, 1745, Monitor.WorkingArea.Height - 20, 0)
            else:
                SetWindowPos(awcc().hwnd, HWND_TOP, 10, 10, Monitor.Width - 20, Monitor.Height - 20, 0)


if __name__ == '__main__':
    main()

What I want to do is load the binary data from the shortcut into the script and use python to execute the shortcut from the binary data in the file.

What I’m trying to do is compile the script and make it so that the executable doesn’t need the shortcut to be able to launch Alienware Command Center.

Maybe this will help. I can’t use the shortcut in the application folder because of the restrictions on it or I’d be able to point os.startfile to the shortcut in the applications folder.

I’ve had to drag the shortcut off of the start menu and put the copy of shortcut in the same folder as an executable to be able to use the shortcut to launch Alienware Command Center.

I think I might have discovered my error in why the executable sometimes doesn’t load.

I’ll post an update later on when I get home from work.