paint-brush
Javascript for Automation in macOSby@einancunlu
15,511 reads
15,511 reads

Javascript for Automation in macOS

by Emin Inanc UnluAugust 8th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Sometimes we come across with simple tasks that consist of many similar small steps. After a point, doing it manually isn’t reasonable. This is where you need to start using automation tools.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Javascript for Automation in macOS
Emin Inanc Unlu HackerNoon profile picture

How did I automate a long task of foldering and renaming.

Sometimes we come across with simple tasks that consist of many similar small steps. After a point, doing it manually isn’t reasonable. This is where you need to start using automation tools.

In macOS there are two ways that you can do that without installing anything — or you can use third-party tools.

The easy way is using Automator app. It has a drag-and-drop interface that allows you to setup a workflow easily by combining predefined actions. The possibilities are almost limitless, but it’s not easy to setup really complex, multi-step, nonlinear workflows.

The harder but really advanced way is using JXA (Javascript for Automation). It’s a programming language based on Javascript. It took place of AppleScript since macOS Yosemite. You can automate almost anything from clicking on the buttons with a specific order to opening a group of apps when you say a command. You can also use JXA in Automator app, that would be a nice mix.

My Case Needed Automation

I had a bunch of video courses that I downloaded from Tutsplus during my yearly subscription period. The downloaded files weren’t grouped and they had really long names. So they were needed to be renamed and foldered. I could have done it one by one, but it would take unreasonably long time. So I decided to automate it.

The Script Explained

It is really simple and mostly self-explanatory. Let’s go over it together.

Firstly, we create a couple of variables that we will use frequently: currentApp for prompting something in the current application, finder for creating and moving files around and systemEvents for accessing to files.

The OS has a set of standard scripting additions that provide the ability to speak text, display user interaction dialogs, and more. To use them, an application must explicitly set the includeStandardAdditions flag to true. The third line stands for that. We’re declaring that we want to use these additions.

After that, we prompt a folder chooser panel to get the folder to be processed. Then we create a new folder in the same directory to move the processed files into it.

Later, we traverse in the folder with a for statement and create an array from the names of the files. The aim is accessing to these files later again by using their names. Because the content of the folder will change dynamically as we move the processed files out. Using file names will ensure that we are accessing to correct files.

Normally we should be able to do that without using file names. I tried to do that but couldn’t manage it. There must be a way of creating pointers to files that don’t corrupt when the content of the folder is changed. I will update this part if someone shows me how to do it.

Finally, we process every file in the array. Firstly we get the file by using the file name and use regular expressions to divide the name into two parts: before the hyphen and after the hyphen. The first part will be the folder name and the second part will be the file name.

After that, we create a new folder with that name if there is no one already, move the file into it and rename it.

That’s all!

Download the script file.

Tips & Tricks

Use Atom Editor to Write and Run the Script

The default editor for JXA is Apple’s Script Editor. But it’s really basic and not so usable. I recommend you to use Atom by installing two packages for writing and running JXA.

First of the packages is language-javascript-jxa for automatically de/recompiling .scpt files and the second one is script package for running scripts in Atom Editor including JXA. I should add that I’m not completely sure if script package is able to give as extensive outputs as Script Editor, but in my case, it was certainly enough.

I also would like to mention the theme I use in Atom: new-moon-atom-syntax. I really like its colors and the contrast!

Application Scripting Dictionary / Property List

There is a panel in Script Editor for viewing property list of the applications. This is for helping you to figure out how to command macOS applications via JXA. You can view it by opening Script Editor and clicking on one of these menu items: File > Open Dictionary or Window > Library.

Resources

Because of insufficient resources, writing this script took longer than I expected. Most of the tutorials and books are for AppleScript even though it has been years since JXA was introduced.

There is a great list of resources at this link. Other than that, I would recommend you to search code snippets in Github. There are many public scripts, sometimes you come across with something really close to what you want to do.

Hope these would help you to start automating things and help you to save some time! Please click or hold on the 👏 button as much as you can! 🙂