Managing shortcuts¶
Read details from an existing shortcut¶
Open an existing shortcut and output its details: the target, the window mode, any arguments, &c.
import os, sys
import glob
import winshell
for lnk in glob.glob(os.path.join(winshell.programs(), "*.lnk")):
shortcut = winshell.shortcut(lnk)
shortcut.dump()
break
else:
print("None found")
Discussion¶
The shortcut()
factory function accepts various items as
its one argument. If the argument is a string and points to a
shell link on disk, the shortcut is wrapped and the result
Shortcut
object returned.
The Shortcut.dump()
function is a convenience function which
returns the values of each of the settable attributes in a dict-like
layout.
Create a shortcut to a file¶
Create a shortcut on the desktop which points to the Python executable, setting the working directory to c:temp.
import os, sys
import winshell
shortcut = winshell.shortcut(sys.executable)
shortcut.working_directory = "c:/temp"
shortcut.write(os.path.join(winshell.desktop(), "python.lnk"))
shortcut.dump()
Discussion¶
The shortcut()
factory function accepts various items as
its one argument. If the argument is a string and does not point
to a shell link on disk, the argument is taken to be the target
for a new Shortcut
object.
The desktop()
function returns the filesystem path to
the user’s desktop.
The Shortcut.write()
method creates a shortcut at the
location given.
Create a shortcut in multiple locations¶
Create the same shortcut on the user’s desktop and in the Programs folder.
import os, sys
import winshell
shortcut = winshell.shortcut(sys.executable)
shortcut.write(os.path.join(winshell.desktop(), "python.lnk"))
shortcut.write(os.path.join(winshell.programs(), "python.lnk"))
Walk the programs tree and list the shortcut targets¶
Walk the Start Menu Programs tree and list, for each shortcut, the shortcut name and its target along with any parameters.
import os, sys
import winshell
shortcuts = {}
user_programs = winshell.programs()
for dirpath, dirnames, filenames in os.walk(user_programs):
relpath = dirpath[1 + len(user_programs):]
shortcuts.setdefault(
relpath, []
).extend(
[winshell.shortcut(os.path.join(dirpath, f)) for f in filenames]
)
all_programs = winshell.programs(common=1)
for dirpath, dirnames, filenames in os.walk(all_programs):
relpath = dirpath[1 + len(all_programs):]
shortcuts.setdefault(
relpath, []
).extend(
[winshell.shortcut(os.path.join(dirpath, f)) for f in filenames]
)
for relpath, lnks in sorted(shortcuts.items()):
level = relpath.count("\\")
if level == 0:
print("")
print("%s+ %s" % (" " * level, relpath))
for lnk in lnks:
name, _ = os.path.splitext(os.path.basename(lnk.lnk_filepath))
print("%s* %s -> %s" % (" " * (level + 1), name, lnk.path))
Discussion¶
The Start Menu merges shortcuts from two folders: the user Programs folder and the common Programs folder. We first scan the user Programs folder, building up a dictionary mapping relative path name to a list of shortcut objects representing the icons within. We then carry out the equivalent action for the common Programs file, merging the results into the same dictionary.
The result is a simple ASCII tree of folders, links and subfolders, including links from user and common installs.