Main

5 Ways To Write Simple Programs On Your Mac

https://macmost.com/e-3088 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. FOLLOW MACMOST FOR NEW TUTORIALS EVERY DAY ▶︎ YouTube — http://macmost.com/j-youtube (And hit the 🔔) ▶︎ YouTube Email Notifications — http://macmost.com/ytn ⭐️ ▶︎ MacMost Weekly Email Newsletter — https://macmost.com/newsletter ✉️ SUPPORT MACMOST AT PATREON ▶︎ https://macmost.com/patreon ❤️ 00:00 Intro 00:35 1. Use Script Editor To Write JavaScript 02:23 2. Use Terminal To Run Shell Scripts 03:53 3. Use Terminal To Run Python Scripts 05:29 4. Use Swift Playgrounds To Run Swift Code 06:47 5. Use Shortcuts To Run JavaScript Or Shell Scripts #macmost #mactutorial

macmostvideo

3 weeks ago

hi this is Gary with mmos.com let me show you some simple ways that you can write programs on your [Music] Mac macm is brought to you thanks to a great group of more than a thousand supporters go to mmos.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 and 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 and when you run script editor and create a new document you'll get a little window it looks like this now script editor can actually use one of two programming languages you'll see it right here and it's very important you se
t it to the right one you can use applescript or JavaScript now apples script is only difficult on a Mac and it's fairly difficult to learn and use and it's not really 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 and now we've got this little window here that we could just write JavaScript programs for instance if I were to create a base functio
n called run here and just have this standard declaration for that function and then 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 se
e the result here is nine the greatest common denominator between 45 and 27 and 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 and of course there's a lot more you can do you can make this into a proper function you could 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 a
s a skill or if you need to actually calculate something you could do it here inside of an app like script editor now I know a lot of you thought I probably 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's called a shell script in terminal Now the default shell for Max today is zshell you can see it here at the top of a standard terminal w
indow so we'll stick with that and you could write a simple shell script you don't even have to write the shell script in terminal I'm actually going to go and use text edit here and I'm going to create a new file in text edit and I'm going to actually write the shell script in text edit so here's a little script it's actually the same thing that we did before finding the greatest common denominator just doing it with a different programming language in this case zshell I'm going to save this do
cument and I'm going to put it in my documents folder and into a folder called programs that I've created and I'm going to save this as gcd Dosh for shell and I'm going to save it am in zshell here I can actually go to that folder like that and when I list the content I could see it here now I don't even have to make it executable I can actually use the command zshell like that and then type the name of the file and when I run it you could see I get the result so I could go back into text edit a
nd change the numbers there to get a different result if I want and of course you can expand on it even asking it to prompt you for numbers and things like that so so far we've covered JavaScript and using a shell script 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 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 i
t if you simply install xcodes commandline tools xcode is of course a whole development environment from Apple that allows you to build apps and it's 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 and command line tools will include having python in the terminal and as a bonus of cour
se 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 have python you want to type Python and then the number three all is one word like that and then two dashes and version and you can 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 and I put it in the same folder and thi
s is how the same greatest common denominator code looks in Python so now to run it I would type Python 3 and then the name of the file which is this I'm already in that directory where it's located so it'll find it easily and run the code and there's the result now there's another app that you can get again for free and again from Apple called Swift playgrounds and this allows you to use the Swift programming language which is the primary language used today to create Mac apps and also iPhone i
Pad and other apps like on the Apple watch or Apple TV so Swift is a very modern programming language and you can use Swift playgrounds to Simply write simple Swift programs or even complete apps but 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 and then you'll get this new playground here and you can enter code and I'm going to paste in s
ome code here and here's how that same greatest common denominator code looks in Swift and to actually test this out all I need to do is Click run my code here and it's 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 and 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 looks a lot like what we saw before so I can paste in
my JavaScript code like that and you can see it's 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 and I could 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 dat Mac then you want to probably 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 shell script you add that and you can paste in that shell script code we had before exactly the same lines here and you can run shell script inside of shortcuts so you can do a shell script without ever having to use the terminal app and likewise there's a way to run shell
scripts 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 velopment environment to do it I hope you found this useful thanks for [Music] watching if you like this video click the Thumbs Up Button below to let me know I publish new tutorials each weekday hit the Subscribe button so you don't miss
out

Comments

@DarkMatter727

Love the videos, always so precise, and helpful. I love how they are not overdrawn out and each video focuses on 1 or two things vs multiple unrelated tips. Nice to learn one thing at a time in a short amount of time, and be able to go back and find the specific video if I need to.

@loisskiathitis8926

A very useful and informative video tutorial today! Thank you, Gary!👏🏻❤️

@macbitz

Great video. Another bonus of Shortcuts is that it can (in many cases) convert Automator Actions to Shortcuts.

@carolinebarlow4795

Thanks very much, Gary, for this informative video! Some great options here.

@alexdimitracopoulos3387

Love your videos, do you have merch? I would love a black tee with your logo on the front left.

@garynagle3093

Very useful. Thanks

@disklamer

Great topic

@mojoblues66

If you give your script the file extension ".command" and enable execute permissions, a double-click in the finder will execute it.

@learningwithanubhav

Great content , I think this will help a lot of people , thankyou for sharing !Liked and Subscribed !

@bevintx5440

That’s a very nice intro.

@tamstutz921

Gary, are there any resident, desktop database possibilities? With ways to write script that includes SQL statements?

@lorensims4846

So many options! And that's not all! You can just as easily run Swift directly in the Terminal, as well as Python, Perl, and Ruby. You can even run AppleScript or JavaScript using the "osascript" Terminal command. I'm surprised you didn't have the Script Editor display a dialog, but that is getting a bit deep, like setting up any and all of these "little programs" as standalone, clickable "apps," which is easy with the #! construct. Anything you can run in the Terminal you can also run in Automator or Shortcuts.

@stephenbridges2791

Very cool. I'm not very good at Java, though. Useful, as always.

@danieledituccio3004

Hi, I recently discovered you and I already adore you, you made me very passionate about Apple Script, and thanks to you I learned a lot but not much, do you have any advice for Davinci? I would like to create Apple Scripts to automate some processes and control the DVR's video inspector, thanks

@kostasjazz

Great video

@InssiAjaton

I hope to find some guidance for reading in a data stream from the USB port(s) on my Macbook pro. Any hints where to start?

@bashful3009

What program are you using to record yourself while presenting your screen? I like how it's done in this video. Do you have a tutorial on creating presentation videos like this one?

@brettleach9281

Python has been on all Mac OS (and Mac OS X) systems since, I think, 2001. It's not always the most up to date version, however.

@ExpUndead

cool video, thx! any chances to run python code in playgrounds? interface is so nice and user-friendly