1+ #!/usr/bin/env python3
2+
3+ # SPDX-FileCopyrightText: 2023 Melissa LeBlanc-Williams for Adafruit Industries
4+ #
5+ # SPDX-License-Identifier: MIT
6+ # Desktop Icon from <a href="https://www.flaticon.com/free-icons/book" title="book icons">Book icons created by Freepik - Flaticon</a>
7+
8+ import os
9+
10+ def create_folders (file_path ):
11+ path = os .path .dirname (file_path )
12+ if not os .path .exists (path ):
13+ os .makedirs (path )
14+
15+ def write_file (path , contents ):
16+ create_folders (path )
17+ with open (path , "w" ) as f :
18+ f .write (contents )
19+
20+ print (f"Shortcut created at { path } " )
21+
22+ def main ():
23+ APP_TITLE = "Magic Storybook"
24+ RUN_IN_TERMINAL = True
25+ APP_PATH = "~/Magic_AI_Storybook/story.py"
26+ APP_ICON = "~/Magic_AI_Storybook/images/magic_book_icon.png"
27+ FILENAME = "storybook.desktop"
28+ AUTO_START = True
29+
30+ if os .geteuid () == 0 :
31+ username = os .environ ["SUDO_USER" ]
32+ else :
33+ username = os .getlogin ()
34+ user_homedir = os .path .expanduser (f"~{ username } " )
35+
36+ print ("Username is " , username )
37+ print ("User home directory is " , user_homedir )
38+
39+ APP_PATH = APP_PATH .replace ("~" , user_homedir )
40+ APP_ICON = APP_ICON .replace ("~" , user_homedir )
41+
42+ shortcut_template = f"""[Desktop Entry]
43+ Comment=Run { APP_TITLE }
44+ Terminal={ "true" if RUN_IN_TERMINAL else "false" }
45+ Name={ APP_TITLE }
46+ Exec=sudo python { APP_PATH }
47+ Type=Application
48+ Icon={ APP_ICON }
49+ """
50+
51+ write_file (f"{ user_homedir } /Desktop/{ FILENAME } " , shortcut_template )
52+ if AUTO_START :
53+ write_file (f"{ user_homedir } /.config/autostart/{ FILENAME } " , shortcut_template )
54+
55+ if __name__ == "__main__" :
56+ main ()
0 commit comments