Creating a .command file

Hello! I would like to know how can I create the equivalent of a "batch" file from windows in OSX.
I want it to run 2 simple commands:

1. cd to a specific folder

2. run a python command.


Thanks!

Posted on Apr 28, 2017 7:59 AM

Reply
6 replies

Apr 28, 2017 9:40 AM in response to snowK1ng

A .command file must be made executable before it is run. You do this in the Terminal, and in this instance, you are making it executable strictly for you:


chmod +x my.command


Now, providing the contents have been tested interactively beforehand in the Terminal, you can double-click this my.command file in the Finder, and it will pop a Terminal window, run, and then kill the Terminal session without quiting the Terminal window it created.


For your example, you need to make your my.command file a Bash script which will honor your change directory (cd) command in a Bash shell, before running the Python script:


#!/bin/bash

# change directory to /Users/myname/Test

cd ~/Test

# run the Python script located in your home directory

./my.py


If you just wanted to run a Python script in your my.command file, you could replace the Bash invocation line with:


#!/usr/bin/python

# coding: utf-8

Apr 28, 2017 10:00 AM in response to VikingOSX

I'm sorry I don't know how to adapt what you just said. So basically I have this folder "cd /Users/Bog/Desktop/Rock"and I want this python command to be executed inside the folder "python ./runserver.py". And then the Terminal would take over. That's it!


Doing this manually, I would type cd /Users/Bog/Desktop/Rock in a terminal window, and then run the python command. Having a lot of instances in that folder is really annoying and I would like to have one file to click on it and do the job. TY!

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Creating a .command file

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.