You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Is it possible to create a batch file in Mac OS?

In MS-DOS, a batch file is a text file containing a series of commands intended to be executed by the command interpreter. It's very convenient and handy. Is it possible to do the same in Mac OS? I tried the automator of Mac OS and copied the workflow, but it is quite clumzy and painfully slow, and stops working afte a relaunch.

MacBook Pro 15 inch, Mac OS X (10.6.6)

Posted on Mar 10, 2011 6:02 PM

Reply
Question marked as Top-ranking reply

Posted on Mar 10, 2011 11:46 PM

it's a no brainer using bash

#!/bin/bash
#this is a comment-the first line sets bash as the shell script
cd /Applications;
cd ./localproxy;
python ./proxy.py;
exit;

Give it a name (eg: myscript), save the script, set it executable.
example:
chmod +x ~/Desktop/myscript

If you want to click it to run it, add '.command' to the end of it.
eg; myscript.command
#
Here is a simple script I wrote that reads text from a file and
pastes it to the clipboard.

#!/bin/bash
# catx - a script to read text
# from a text file and paste it
# to the clipboard.
FNAME=$1
if [[ -z "$FNAME" ]]; then
echo "catx <name of file>"
echo " catx is a script to read text from a"
echo " text file and paste it to the clipboard."
exit;
fi
cat "$FNAME" | pbcopy
exit;


Kj
9 replies
Question marked as Top-ranking reply

Mar 10, 2011 11:46 PM in response to xieqiao

it's a no brainer using bash

#!/bin/bash
#this is a comment-the first line sets bash as the shell script
cd /Applications;
cd ./localproxy;
python ./proxy.py;
exit;

Give it a name (eg: myscript), save the script, set it executable.
example:
chmod +x ~/Desktop/myscript

If you want to click it to run it, add '.command' to the end of it.
eg; myscript.command
#
Here is a simple script I wrote that reads text from a file and
pastes it to the clipboard.

#!/bin/bash
# catx - a script to read text
# from a text file and paste it
# to the clipboard.
FNAME=$1
if [[ -z "$FNAME" ]]; then
echo "catx <name of file>"
echo " catx is a script to read text from a"
echo " text file and paste it to the clipboard."
exit;
fi
cat "$FNAME" | pbcopy
exit;


Kj

Mar 10, 2011 6:39 PM in response to xieqiao

Having come from a Apple ][ & DOS background, it took me a while to learn Automator. It's more graphical so some concepts took a "AHA!" moment. You don't mention what "stops working" but likely it's "just" a step that is missing from your workflow. For example, in one of my workflows, the "AHA!" was realizing I needed to specify a name by "Get Specified Finder Item" before I could work with it.

Perhaps you'd be more comfortable with Applescript, especially if you've done any C or other programming language beyond batch files.

If you're up for it, or if your needs are that complex, you could create shell scripts. In fact, my latest Automator work flow calls a shell script because it's actually the KeePass Windows executable run in Mono. I know I probably could have done the whole thing as a shell script, but Automator was easier and would have been simple if it wasn't for this one shell script. 🙂

Mar 10, 2011 6:55 PM in response to xieqiao

In MS-DOS, a batch file is a text file containing a series of commands intended to be executed by the command interpreter. It's very convenient and handy. Is it possible to do the same in Mac OS?


Since Mac OS X is a Unix system, yes, that's certainly possible. You'll need to know how to write Unix shell scripts in order to do that kind of thing, which is not something for the faint of heart. If you're not familiar with Unix, you'll have quite a bit of learning to do.

I tried the automator of Mac OS and copied the workflow, but it is quite clumzy and painfully slow, and stops working afte a relaunch.


Automator is very limited. It's good for some things, but not others. AppleScript is more powerful, but, as is always the case, has a steeper learning curve. It may be worth a try.

However, there's no reason for Automator to stop working after a relaunch. Are you saving your workflow?

Mar 10, 2011 7:16 PM in response to thomas_r.

I'm not a programer. But in Linux, it is very easy to turn a text file into an executable file by just right-clicking the created text file and changing the file's configuration from read to execute. Could I turn a comand line into a executable file in Mac OS? Or what kind of file should I create for "python /Applications/localproxy/proxy.py" first?

I know there is no need for a heavyweight like Automator in my simple case.

Mar 10, 2011 9:23 PM in response to thomas_r.

Thank you so much. I think I have resolved the question. I simply open the Terminal, open the Preferences, Click Settings, Click + to create a new "localproxy" thumbnail image as default, Click Shell, and type in the command line at Startup and check both "Run command" and "Run inside shell", at "Prompt before closing", click "Never".

Now move the Terminal from System Preferences to the Dock of my MacBook Pro. Now I only need to click Terminal in the Dock every time I want to run the command.

Is it possible to create a batch file in Mac OS?

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