Search code examples
pythonmacosspotlight

How Do I Run a Python Script from Mac's Spotlight (Instead of Having to Open Terminal or Pycharm)?


I'm on a MacBook Pro 16" (with MacOS Catalina). I want to run Python scripts directly via Spotlight search. I don't want to have to open any IDE or the Terminal. I have seen instructions that tell me to:

  1. Write and save my Python code e.g.: print("Hello World"), saved as hello.py in home folder Users/Gory

  2. Create a text file using TextEdit and save it with .command file extension (e.g.: samplescript.command). The file should contain the following

    #!/usr/bin/env bash
    python3 /Users/Gory/hello.py
    
  3. Make the shell script (samplescript.command) created above executable by running in the Terminal:

    chmod u+x samplescript.command
    

After following the above steps, I searched for samplescript.command via Spotlight and pressed enter. I expected to see "Hello World" printed on a terminal window. Instead I get the following message:

MacBook-Pro:~ Gory$ /Users/Gory/samplescript.command ; exit;
/Users/Gory/samplescript.command: line 1: {rtf1ansiansicpg1252cocoartf2511: command not found
/Users/Gory/samplescript.command: line 2: syntax error near unexpected token `}'
/Users/Gory/samplescript.command: line 2: `\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}'
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

What is wrong?


Solution

  • Your problem

    Your problem is caused by your second step:

    Create a text file using TextEdit and save it with .command file extension (e.g.: samplescript.command). The file should contain the following

    By default, TextEdit uses the Rich Text Format.

    Consequence: Your samplescript.command file does not contain what you expect

    #!/usr/bin/env bash
    python3 /Users/Gory/hello.py 
    

    but actually

    {\rtf1\ansi\ansicpg1252\cocoartf2513
    \cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
    {\colortbl;\red255\green255\blue255;}
    {\*\expandedcolortbl;;}
    \paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0
    \pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0
    
    \f0\fs24 \cf0 #!/usr/bin/env bash\
    python3 /Users/Gory/hello.py}
    

    Such content is not a valid command, and then results in the observed error when executed:

    line 1: {rtf1ansiansicpg1252cocoartf2513: command not found
    line 2: syntax error near unexpected token `}'
    line 2: `\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;}'
    

    How to fix it?

    If you want to use TextEdit then change the format from Rich Text to Plain Text before typing in anything.

    1. Create a new document
    2. Format menu ➜ Make Plain Text
    3. Insert your content, save

    Better fix:

    Do not use TextEdit or any other kind of text processor application to write code (scripts), so that these kind of problems are avoided from the start.

    In any case, whatever editor you use double-check that the (script) file content is actually the content you expect.