Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2@file
3@brief Various helper about shortcuts and links
4"""
6import os
7import sys
8import platform
10if sys.version_info[0] == 2:
11 FileNotFoundError = Exception
14def add_shortcut_to_desktop(file, name, description="", arguments="", icon=None, workdir=None):
15 """
16 @see fn add_shortcut
17 """
18 return add_shortcut(file, name, description, arguments, icon, workdir)
21def add_shortcut(target, name, description="", arguments="",
22 icon=None, workdir=None, folder=None):
23 """
24 Adds a shortcut to the desktop.
26 @param target target
27 @param name name of the shortcut
28 @param description description
29 @param arguments arguments
30 @param icon icon
31 @param workdir working directory
32 @param folder where to save the shortcut (None for the desktop)
33 @return path to the shortcut
34 """
35 if not sys.platform.startswith("win"):
36 raise NotImplementedError(
37 "not implemented on this platform: " +
38 sys.platform)
40 try:
41 import winshell
42 except ImportError as e:
43 if "DLL load failed" in str(e):
44 os.environ["PATH"] = os.environ[
45 "PATH"] + ";" + os.path.split(sys.executable)[0] + r"\lib\site-packages\pywin32_system32"
46 try:
47 import winshell
48 except ImportError:
49 raise ImportError(
50 r"you should run the following in your current folder:\ncopy C:\%s\lib\site-packages\pywin32_system32\py*.dll %s" %
51 (os.path.split(
52 sys.executable),
53 os.getcwd())) from e
54 else:
55 raise e
57 if folder is None:
58 folder = winshell.desktop()
59 link_filepath = os.path.join(folder, name + ".lnk")
61 args = ["/c", "start", target, arguments]
63 with winshell.shortcut(link_filepath) as link:
64 link.path = r"%windir%\system32\cmd.exe"
65 link.description = description if description is not None else name
66 link.arguments = " ".join(args)
67 if icon is not None:
68 link.icon_location = (icon, 0)
69 if workdir is not None:
70 link.working_directory = workdir
72 if not os.path.exists(link_filepath):
73 raise FileNotFoundError(link_filepath)
74 return link_filepath
77def suffix():
78 """
79 add a suffix to a shortcut name = python version + architecture
81 @return string
82 """
83 ver = ".".join(str(_) for _ in sys.version_info[:2])
84 arc = platform.architecture()[0]
85 return "{0}.{1}".format(arc, ver)
88if __name__ == "__main__":
89 path = r"C:\github\pymyinstall\dist\win_python_setup"
90 target = r"tools\R\bin\R.exe"
91 name = "R Console"
92 icon = r"tools\icons\r.ico"
93 add_shortcut(target=target, folder=path, icon=icon, name=name)
95 path = r"C:\github\pymyinstall\dist\win_python_setup"
96 target = r"python\python.exe"
97 name = "Python Console"
98 icon = r"tools\icons\python.ico"
99 add_shortcut(target=target, folder=path, icon=icon, name=name)