I can see several issues with your code. Here's a corrected version with proper indentation and fixes:
def launch_profile(name, private=False):
firefox_path = "/Applications/Firefox.app/Contents/MacOS/firefox"
if firefox_path is None:
raise ValueError("Firefox binary path could not be resolved. Check your Firefox installation.")
args = [firefox_path, "-P", name]
if private:
args.append("--private-window")
subprocess.Popen(args)
The main issues were:
-
Indentation problems: Python requires consistent indentation. Your code had mixed indentation levels.
-
Undefined variable: firefox_path was being checked but never defined in the function. I've added it at the beginning of the function.
-
Inconsistent path usage: You were checking firefox_path but then hardcoding the path in the args list.
Additionally, make sure you have imported the subprocess module at the top of your file:
import subprocess
If you want to make the Firefox path more flexible (for different operating systems or custom installations), you could pass it as a parameter or use a path-finding utility:
def launch_profile(name, private=False, firefox_path=None):
if firefox_path is None:
firefox_path = "/Applications/Firefox.app/Contents/MacOS/firefox" # Default for macOS
if not firefox_path or not os.path.exists(firefox_path):
raise ValueError("Firefox binary path could not be resolved. Check your Firefox installation.")
args = [firefox_path, "-P", name]
if private:
args.append("--private-window")
subprocess.Popen(args)