0

I want to add a python script to startup using another python script. Is there a way to do that?

Jacob Vlijm
  • 83,767

1 Answers1

2

Acc. to this specification, placing a .desktop file to ~/.config/autostart should work. So basically the task of your python script is

  1. Place a python script somewhere
  2. Place a .desktop file under ~/.config/autostart.

Here's an example of such a script

import os

autostart_path      = os.path.expanduser('~/.config/autostart/')
nameofmyscript      = 'myscript.py'
nameofmydesktopfile = 'myscript.desktop'

mypythonscript      = """#!/usr/bin/python
print("hello")"""
desktopfile         = """[Desktop Entry]
Type=Application

# The version of the desktop entry specification to which this file complies
Version=1.0

# The name of the application
Name=Script

# A comment which can/will be used as a tooltip
Comment=My cool python script

# The path to the folder in which the executable is run
Path=%s

# The executable of the application, possibly with arguments.
Exec=%s

# Describes the categories in which this entry should be shown
Categories=Education;Languages;Python;

""" % (autostart_path, nameofmyscript)

# write the desktop file
with open(autostart_path + nameofmydesktopfile, 'w+') as script:
    script.write(desktopfile)

# write the python script; you can place it anywhere actually, just be sure to correct the desktop
# file accordingly
with open(autostart_path + nameofmyscript, 'w+') as script:
    script.write(mypythonscript)

os.system('chmod +x ' + autostart_path + nameofmyscript)
Hi-Angel
  • 3,702
  • 1
  • 29
  • 36
  • That's not what OP is asking, he wants to do it by another script (so add it to Startup Applications by a script. – Jacob Vlijm Nov 17 '17 at 19:29
  • @JacobVlijm err… I don't understand, isn't that what I wrote? Except I didn't write the actual code, which would basically be 1. Open the file 2. Write the string, 3. Call chmod +x (or something python-specific of a similar functional). – Hi-Angel Nov 17 '17 at 19:32
  • @JacobVlijm ok, thanks, this one should work ↑ ☺ – Hi-Angel Nov 17 '17 at 20:06
  • My bad, I only skimmed your answef when I made my second comment. ~/.config/autostart isn't really the place for scripts, but for .desktop files, calling whatever should run. The .desktop file then doesn't need to be executable.:) – Jacob Vlijm Nov 17 '17 at 20:16
  • Is the path ~/.config/autostart/ same for windows too? – ajinzrathod May 15 '21 at 09:39
  • @ajinzrathod I'm not sure I understood your question correctly, but if you're asking about MS Windows OS series, then no, AFAIK those are unfortunately one of the rare operating systems that don't follow any of Freedesktop standards. In fact, they don't even have ~/.config directory (they have $HOME directory though the ~ symbol isn't expanded to its path, but the .config directory there is not present on their systems). – Hi-Angel May 15 '21 at 10:33
  • Yeah, that was what I was asking. Thanks btw – ajinzrathod May 15 '21 at 10:57