5 Ways To Write Simple Programs On Your Mac

If you want to learn some basic programming skills, or want to use skills you already have to perform calculations without needing to learn a whole development tool, there are many ways to do that on your Mac with what you already have, or what you can get directly from Apple for free.
You can also watch this video at YouTube.
Watch more videos about related subjects: JavaScript (14 videos), Shortcuts (69 videos).

Video Transcript

Hi, this is Gary with MacMost.com. Let me show you some simple ways that you can write programs on your Mac. 
MacMost is brought to you thanks to a great group of more than 1000 supporters. Go to MacMost.com/patreon. There you can read more about it. Join us and get exclusive content and course discounts.
Now whether you want to learn some programming or you already know how to program and you want to write some simple programs say to run some calculations, well there are some easy ways to do it on your Mac. Most of these don't even include installing anything else. 
So the first app we're going to use is already installed with your Mac. It's been installed for years. So even if you've got an older system you should be able to do this. So I'm going to run Script Editor. When you run Script Editor and create a new document you'll get a little window that looks like this. Now Script Editor could actually use one of two programming languages. You'll see it right here and it is very important that you set it to the right one. You can use AppleScript or JavaScript. Now AppleScript is only on a Mac and it is very difficult to learn and use. It's really not that useful outside of just doing things on your Mac. On the other hand JavaScript is very useful in all sorts of situations and a pretty modern programming language. So we're going to switch to that. 
Now we've got this little window here that we can just write JavaScript programs. For instance if I were to create a base function called Run here and just have this standard declaration for that function and in that just return something, like this, and then close the function like that I can Run it and you can see the result is the output there. So that's a very simple JavaScript program. 
Here's something a little bit more complex that's actually kind of useful. This takes two numbers here, in this case 45 and 27, and then runs a simple little algorithm to find the greatest common denominator. So when I Run this you can see the result here is 9. The greatest common denominator between 45 and 27. All I need to do to calculate another one is to change one of these numbers here to something else, like that, and Run this and you can see it gives me that result. Of course there's a lot more you can do. You can make this into a proper function. You can have it ask for input. All sorts of things. But in this tutorial we're going to stick to writing very simple programs like this that can help you learn programming as a skill or if you need to actually calculate something you can do it here inside of an app like Script Editor. 
Now I know a lot of you thought I probably was going to jump right into using the Terminal for this. But Script Editor is actually even easier if you're going to do JavaScript. But of course you can use Terminal to do this. For instance you can write what is called a ShellScript in Terminal. Now the default Shell for Macs today is Z-Shell. You can see it here at the top of a standard Terminal window. So we'll stick with that. You can write a simple ShellScript. You don't even have to write the ShellScript in Terminal. I'm actually going to go and use TextEdit here and I'm going to create a new file in TextEdit. I'm actually going to write the ShellScript in TextEdit. 
So here's a little script. It's actually the same thing we did before. Finding the greatest common denominator just doing it with a different programming language, in this case Z-Shell. I'm going to Save this document and I'm going to put it in my Documents Folder and into a folder called Programs that I've created. I'm going to Save this as GCD.SH for Shell. I'm going to Save it and then Z-Shell here I can actually go to that folder, like that, and when I list the contents I can see it here. Now I don't even have to make it executable. I can actually use the command Z-Shell, like that (zsh), and then type the name of the file and when I Run it you can see I get the result. So I can go back into TextEdit and change the numbers there to get a different result if I want. Of course you can expand on it even asking it to prompt you for a number and things like that. 
So far we've covered JavaScript and using a ShellScript. But what about something like Python. Well you can actually do Python programs on your Mac fairly easily. But Python doesn't come installed with your Mac. It is fairly easy to get it. You can of course go to the official Python site and install it on your Mac from there. You can also get it if you simply install Xcode's Command Line Tools. Xcode  is, of course, a whole development environment from Apple that allows you to build apps. It is Free! You don't even need a developer account. You can just get Xcode and play around with it. You need a developer account to actually distribute apps.  But once you've got Xcode, if you run Xcode for the first time it should prompt you to install Command Line Tools. Command Line Tools will include having Python in the Terminal. As a bonus, of course, you've got Xcode which is another way to write programs, one I won't even talk about here. 
So after installing Command Line Tools in the Terminal check to make sure you've got Python. You want to type Python and then the number 3 all as one word like that, and then two dashes, and version. You a see here, indeed, I've got Python installed. So now that I've got Python installed I can write a Python script. Here's one I created. I just named this file gcd.py. I put it in the same folder and this is how this same greatest common denominator code looks in Python. So now to Run it I would type python3 and then the name of the file, which is this, I'm already in that directory where it is located so it will find it easily. Run the code and there's the result. 
Now there is another app that you can get, again for free, and again from Apple called Swift Playgrounds. This allows you to use the Swift programming language which is the primary language used today to create Mac Apps and also iPhone, iPad, and other apps like on the Apple Watch or AppleTV. So Swift is a very modern programming language and you can use Swift Playgrounds to simply write simple Swift programs or even complete apps. We're just going to use it to run some code just like we've been doing so far except in Swift. 
So when you Run Swift Playgrounds in order to just test out some code what you want to do is create a new book. Then you'll get this new playground here and you can enter code. I'm going to paste in some code here. Here's how that same greatest common denominator code looks in Swift. To actually test this out all you need to do is Run my code here and it is going to run and I can click here to see the output and there it is. Now if I change one of these things here, like this, I can then run it again and I can get the result for that. So this is a great way to learn the basics of programming and Swift without having to build an entire app in Xcode. You can just use this environment here in the Swift Playgrounds App and play around with some Swift code. 
Now finally you can use Shortcuts to write simple pieces of code. I'm not going to use a new programming language here. I'm going to go back to languages we've already used. If you Run the Shortcuts App on your Mac and then create a new shortcut, one of the actions you can use is Run JavaScript for Mac Automation here then you could see I've got this little JavaScript function that looks a lot like what we saw before. So I can paste in my JavaScript code, like that, and you can see it is exactly the same thing I was running in Script Editor and now when I run this in the Shortcuts App you can see the output is right there. I can expand on this a lot but I could just also use it right here in the Shortcuts App which is really handy and it should be noted that you can do exactly the same with a very similar run JavaScript for Automation in Automator. So if you're on an older Mac you can use Automator to do essentially the same thing. But if you're running an up-to-date Mac then you probably want to do this in Shortcuts and learn about Shortcuts which is the future as opposed to Automator. 
Now you also can search for Shell and you'll find you've got Run ShellScript. You add that and you can paste in that ShellScript code we had before exactly the same lines here. You can run ShellScript inside of Shortcuts. So you can do a ShellScript without ever having to use the Terminal App and likewise there is a way to run ShellScripts in Automator too. 
So there you go. There are a bunch of different ways to write simple programs and Run them on your Mac, mostly without having to install anything, or install something from Apple for Free and you don't have to use Xcode and a complicated development environment to do it. I hope you found this useful. Thanks for watching. 

Comments: 8 Comments

    Michael
    1 year ago

    Excellent, thank you! My main takeaway: Convert my little AppleScript helpers to JXA.

    Sheldon
    1 year ago

    Thanks bunches

    Ralf
    1 year ago

    hi Gary,
    last week i told a college that applescript isnt hard to learn at all..😀
    can u imagine, i am using applescript for years to do scripts for me as a mac administrator. i think AS is still powerful but maybe a bit outdated though.. but just talking.. thanks as always for the great tipps! greetings from Germany and best wishes from Ralf

    Tim
    1 year ago

    what language is recommended for programming repetitive commands in Numbers (like macros in Excel)?

    1 year ago

    Tim: You really only have two choices: AppleScript and JavaScript JXA. They are both available in Script Editor, Automator and Shortcuts which can control Numbers. I prefer JavaScript because it is modern and used by millions of people in different environments. AppleScript feels really old 1980s-style "scripting" to me and can be difficult to writes (as a computer scientist who can program in almost anything).

    Tim
    1 year ago

    Thank you Gary. Do you have a course on JavaScript or do you know of one you would recommend?

    1 year ago

    Tim: No, sorry. I suppose you could look on Udemy and see what is there.

    Tim
    1 year ago

    Thank you again Gary. Udemy has several courses on the subject.

Comments are closed for this post.