What is an alternative to a BAT file on Mac?

Solution 1:

Being pretty unfamiliar with batch files, I'm not sure of the functionality available, but there are two main scripting methods on OS X which are similar:

  • AppleScript

    AppleScript is a scripting language which lets you control OS X and many applications.

    AppleScripts are generally easy to understand and write whilst providing a lot of advanced functionality. Here is an AppleScript which counts the files in the Applications folder:

    tell application "Finder"
        if folder "Applications" of startup disk exists then
            return count files in folder "Applications" of startup disk
        else
            return 0
        end if
    end tell
    

    For more information about AppleScript, see:

    • https://developer.apple.com/library/mac/documentation/applescript/Conceptual/AppleScriptX/AppleScriptX.html
  • Shell script

    A shell script is a script for a Unix shell.

    The equivalent shell script to the above AppleScript is the following:

    if [ -d "/Applications/" ]; then
        ls -1 "/Applications/" | wc -l
    fi
    

    As you're probably going to use bash, this is a good guide to get you started:

    • http://www.tldp.org/LDP/abs/html/