我是JXA的新手,所以我确定还有很多我不知道的地方。我是一名Javascript开发人员,但只研究了一天。这是我想要做的:
我的MacBookPro上有很多文件夹,其中装有mp4格式的视频课程。为了使Plex可以将它们作为电视节目观看(这似乎是浏览这些节目的最佳方法),需要将它们重命名并移到“第1季”文件夹中。我尝试了几个脚本示例来完成此操作,但是我仍然遇到我无法理解的错误。这是我脚本的最新版本(根本不解决重命名文件的问题):
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);
}
}
});
我当前在尝试创建一个新文件夹(destinationFolder)的行上遇到错误,该文件夹说该文件夹已存在,但实际上不存在,因此显然我在这里有点迷失了。任何帮助将不胜感激!