Python or JavaScript as a general use language for automation

Hey guys, after a joyful ride on AppleScript, I’ve started to hit into some limitations and now I am looking for the next step into my “programming” formation.

I was looking into the most commom languages and now am between Python (Automating the Boring Stuff with Python seems to be a good start) or JavaScript (Programming by Stealth episodes look like a good start point, are they?).

From a quick look into the topics on this Forum, I’ve formed an impression that:

  • Javascript seems to be more used in this iOS/macOS automation corner of the internet and is great for small tweaks on strings, fetching data on JSON dictionaries that are everywhere, etc.

  • Python is great for its modules and its application for handling with big chunks of data in general (analysis, web crawling etc.), it looks like its less suited to a quick-and-dirty-on-the-moment script though (perhaps I’m just a bit more intimidated by it).

I’m beginning to realize that the diferences on languages (from a learning stand point) tend to reduce the more you know about programming in general, as the basic concepts (variables, arrays, dictionaries, etc) tend to repeat with some name and/or syntax changes.

The real diference seems to be the community and the use cases they develop around it.

Therefore I thought it was a good idea to ask to this community their opinions on the pros and cons of those languages, and help me and others decide which one to learn first.

P.S.: While I intend this post to be helpful to anyone with the same doubts I have, for those who ask about which will be my use cases, I’m both a professor of tax law and lawyer so, beside being interested in some cool automation tricks, on my day job I deal with a lot of emails, text, word documents, PDFs, databases, excel and csv spreadsheets, and the likes.

1 Like

I think Automating the Boring Stuff with Python is quite specific, and not something that will give you a broader grounding in Python. Not saying it isn’t a good book, just perhaps not the best option for learning Python. Perhaps in combination with another book more geared to a broader introduction to Python.

Programming by Stealth is good, but is focused more on JavaScript for web than for automation (I’m very much enjoying Bart’s delve into Git on PbS at the moment).

JavaScript is becoming a lingua franca across apps and platforms, but Python I would say is going to give more options more easily on macOS given the level it can operate at and won’t require Node or the like to get to the deeper functionality with the OS. There is JXA, but if you’re comfortable with AppleScript, I don’t think you would get much benefit from trying to focus on that first - it can be hard to get into based on the levels of documentation and examples available; though if you search this forum you will find some.

For your use cases, I would suggest Python first, based on the code libraries and the amount of source code available, for working with the files and content you would want to automate against. I would say that it isn’t less suited to quick scripting. If you doubt that, try it for a few days and say how far you get. I expect you will be pleasantly surprised.

If you have particular apps you want to work with like Drafts, OmniFocus, Scriptable, etc. then JavaScript is absolutely where it is at. But, I think you’re more likely to get by with what others have done in the interim and dip into JavaScript for particular apps as you need to do something or once you have become a true Pythonista.

1 Like

I’ve done a lot of the latter with Python - here are some examples from one of my Alfred workflows in Python, to give you an idea of how easy it is - each of these defines a function (hardly necessary for the first two):

def to_upper(text):
	return text.upper()
	
def to_lower(text):
	return text.lower()

def to_capitalized(text):
	text = string.capwords(text)
	return text
	
def to_sentence_case(text):
	text = text[0].upper() + text[1:].lower()
	return text

Here’s the source:

The biggest issue you’ll likely face with Python is that Apple has been bundling Python 2 for many years and is now switching to Python 3. The differences aren’t huge but can complicate finding examples, code to borrow, etc. See also xkcd: Python Environment

1 Like

As a professional developer and fellow automator, I recommend you to use the right tool for the job. Therefore, it’s a good idea to strive towards being comfortable with both languages. Both languages are great and work for quick-and-dirty solutions.

To achieve this, I suggest you start by deciding on a problem to solve and select the language best suited for the job. If you, say, decide that you want to parse one or several csv file(s), filter the data and generate some kind of report, Python and its module “Pandas” are the best tools. A simplified solution would then be something like:

import pandas as pd

file_name = “myfile.csv”
data = pd.load_csv(file_name)

# filter the data
# …

# generate the report
# …

To learn basic Python, I suggest the free course at CodeAcademy (link).

A user-friendly way to use Python on mac is via Anaconda. Follow this guide to set it up: (link).

2 Likes

I like your “right tool for the job”. (My experience with using screwdrivers as hammers has been less successful than using a coin as a screwdriver.) :slight_smile:

But seriously, I choose the language based on the environment. So, for example:

  • When I want localhost web serving on Mac (and I have lots of code that does this) I use PHP and javascript (and Dojo).
  • When I want command-line stuff I use Python.
  • Lots of other places - such as Drafts and OmniFocus I use javascript.
  • I also use Shortcuts. (On Mac that would be Keyboard Maesgro and AppleScript.)
  • On the mainframe (less relevant here, perhaps) I use eg REXX and Assembler.

I could go on about language design but I consider that rather secondary. Most modern languages have most of the bits you would want to get the job done, especially if you use them in the right setting. Hence the point of this contribution.

1 Like

After years of meaning to get around to it, I’ve finally started trying to learn Python, and I settled on “Python Crash Course” by Eric Matthes¹ which I believe is the intro to python book in the series which then continues with “Automating the Boring Stuff with Python”.

I would also like to learn JavaScript at some point, and it will probably be next, but I wanted a new language that I could use to go beyond what I can do with shell scripts, so python seemed like the next reasonable step.


¹ That’s a link to the Kindle version. You can get DRM-free ebooks at No Starch Press but it will cost you more ($32 vs $24 in the US).

1 Like

Good for you! It’s a really useful tool! Have you found a practical use for it yet?

1 Like

Not at all, but I’m barely past the hello world stage.

1 Like

To sum up my reading on this and help fellow newbies that may have similar doubts:

Based on the examples above and in my own use cases, I guess Python would be a nicer next step in my amateur programming path.

Pros

And it is good to deal with general data with its pandas module

Issues

Resources

What about JavaScript

So it looks like a good idea to learn afterwards.

Thank you

Thank you all for these insightful answers!

Now it’s time to get som books and move past that hello world first program in Python!

2 Likes

Nice summary! :+1:

Regarding Python 2 vs 3: stick with Python 3, it has been the standard for a few years.

For learning Python I recommend you to start with the Code Academy course which I linked to. It presents relevant theory in a clear way AND is built around interactive lessons.

Good luck, have fun! :grin:

2 Likes

+1 on learn Python 3 (rather than 2), preferably 3.8 or higher. I’m about to prereq 3.8 for my projects as I deem it available enough while giving me something I want - the “walrus” operator.

1 Like

That walrus thing seems to be quite handy for if and elif statements!

1 Like

That’s exactly what I want it for: md2pptx has a battery of RegEx tests - where I want the match object it returns on a successful match. If it were one such test I could do without it but it’s the elifs that really need it. I reckon this will speed my Markdown parsing code up significantly.