0

I'm VERY new to JXA, so I'm sure there's a lot I don't know yet. I'm a Javascript developer, but have only been researching this for a day. Here's what I'm trying to do:

I have a ton of folders on my MacBookPro with video courses in them, in mp4 format. In order for Plex to see them as TV shows (which seems to be the best way to browse these), they need to be renamed and moved into a "Season 1" folder. I've tried several script examples to accomplish this, but I continue to get errors that I cannot understand. Here's the most recent iteration of my script (doesn't address renaming the files at all):

var currentApp = Application.currentApplication();
currentApp.includeStandardAdditions = true;
var finder = Application("Finder");
var systemEvents = Application("System Events");
var folderName = "Season 1";
var extensionsToProcess = ["mp4","mkv","mpg","mov"] // For example: {"jpg", "jpeg"}, NOT: {".jpg", ".jpeg"}

// Ask a folder to process
var sourcePath = currentApp.chooseFolder ({
    withPrompt: "Choose the source folder"
}).toString();
var sourceFolder = systemEvents.aliases.byName(sourcePath);

// Create the destination folder
var container = sourceFolder.container();
var containerPath = container.path();
var destinationFolder = finder.make({
  new: "folder", at: containerPath,
  withProperties: { name: folderName }
});

// Create an array of items to be processed
var items = systemEvents.aliases.byName(sourcePath).diskItems;
var selectedItems = [];

for (var i = 0; i < items.length; i++) {
  var item = items[i];
  if (item.class() != "folder" && item.visible()) {
    selectedItems.push(item.name());
  }
}

// Process every item
var createdFolders = [];
selectedItems.forEach(function(itemName) {
  var item = systemEvents.aliases.byName(sourcePath + "/" + itemName);
  var extension = item.nameExtension();
  var name = item.name().match(/(.*) - (.*)/);

  if (extensionsToProcess.includes(extension)) {
    try {
      var folderName = sourceFolder;
      if (!createdFolders.includes(folderName)) {
        var newFolder = finder.make({
          new: "folder", at: destinationFolder, withProperties: { name: folderName }
        });
        createdFolders.push(folderName);
      }
      item = finder.move(item.path(), { to: destinationFolder.folders[folderName] });
      item.name = name[2];
    } catch (error) {
      console.log("Error (item: " + item.name() + "): " + error);
    }
  }
});

I'm currently getting an error on the line where I try to create a new folder (destinationFolder) that says the folder already exists, which it doesn't, so I'm obviously a bit lost here. Any help would be greatly appreciated!

Eddie
  • 1,228
  • 3
  • 17
  • 31
  • JXA is a dud. [NodeJS](https://nodejs.org) is infinitely better in every respect. – foo Jun 15 '19 at 19:43
  • Ok, I'm open to that. Do you have a link to some sample code that prompts for a folder, then grabs the files that match a filter, create a new subfolder, move the files into that folder and rename them? I've been googling around and I see there are a couple good filesystem modules, but I'm not clear on how they work on the local system since all of my experience is building web apps. Thanks! – Eddie Jun 16 '19 at 18:00
  • I've done more googling and I don't see any way to set a starting directory in nodejs, other than editing the app.js file every time I want to traverse a new folder. That's not really workable for my purposes. If you have a better way (like a dialog box where I can choose the folder), then I will have to go back to JXA. – Eddie Jun 16 '19 at 18:53
  • Moving and renaming [files](https://nodejs.org/docs/latest/api/fs.html) is simple enough; e.g. `const fs = require('fs'); fs.renameSync('/path/to/file', '/path/to/renamed-file')`. Personally I’d just pass the starting path as a command line argument (see [`process.argv`](https://nodejs.org/docs/latest/api/process.html#process_process_argv)), but if you prefer a GUI dialog have a [rummage](https://www.npmjs.com/package/cocoa-dialog) [around](https://www.npmjs.com/package/native-dialogs) npm. – foo Jun 18 '19 at 19:52

0 Answers0