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 there are two ways that you can do that without installing anything — or you can use third-party tools. macOS The easy way is using . 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. Automator app The harder but really advanced way is using . It’s a programming language based on Javascript. It took place of AppleScript since macOS Yosemite. You can also use JXA in Automator app, that would be a nice mix. JXA ( Javascript for Automation) 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. My Case Needed Automation I had a bunch of video courses that I downloaded from 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. Tutsplus 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: for prompting something in the current application, for creating and moving files around and for accessing to files. currentApp finder systemEvents 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 flag to . The third line stands for that. We’re declaring that we want to use these additions. includeStandardAdditions true 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 Editor to Write and Run the Script Atom The default editor for JXA is Apple’s Script Editor. But it’s really basic and not so usable. I recommend you to use by installing two packages for writing and running JXA. Atom First of the packages is for automatically de/recompiling files and the second one is 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. language-javascript-jxa .scpt script I also would like to mention the theme I use in Atom: . I really like its colors and the contrast! new-moon-atom-syntax 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: or . File > Open Dictionary 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 . Other than that, I would recommend you to . There are many public scripts, sometimes you come across with something really close to what you want to do. at this link search code snippets in Github 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!
Share Your Thoughts