I want to write a Js script which can import csv file. But i can not find a function to open a file explorer or browser to select a csv file to import.
i could use studio.system.getFile(filepath); to open a file, but how to get this filepath?
You can use the filesystem “Open” window with the following code:
var path = "";
studio.ui.showModalDialog({
windowTitle: "Open a file",
widgetType: studio.ui.widgetType.PathLineEdit,
text: "",
label: "Please select a file to open and close the window when done",
onEditingFinished: function() {
path = this.text();
}
});
On selecting a file and closing the Modal Dialog Window in FMOD Studio, the filepath will be assigned to the path variable.
Also, I happen to have a script on hand which opens a text file containing a list of events in folder delimited like so:
and generates the folder and events in the FMOD Studio project based on that. It’s not exactly what you need, since you’re using a CSV that is likely delimited differently, but it may serve as a helpful reference:
studio.menu.addMenuItem({
name: "Events From File",
execute: function() {
createEventsFromFile();
}
});
function createEventsFromFile(){
var filePath = studio.project.filePath;
filePath = filePath.substr(0, filePath.lastIndexOf("/") + 1) + "/eventList.txt";
// Open txt file at project path, returning early if file cannot be opened
var textFile = studio.system.getFile(filePath);
if (!textFile.open(studio.system.openMode.ReadOnly)) {
alert("Failed to open file {0}".format(filePath));
console.error("Failed to open file {0}.".format(filePath));
return;
}
// Read entire text file
var text = textFile.readText(textFile.size());
// Split text to lines
var lines = text.split('\n');
// For each line, split event paths into individual elements
lines.forEach(function(line){
var lineElements = line.split('/');
// For each folder/event
for(var i = 0; i < lineElements.length; i++) {
// Generate paths of folder/event and parent folder
var folderPath = lineElements.slice(0, i + 1).join('/');
var parentFolderPath = lineElements.slice(0, i).join('/');
// Try to retrieve existing folder/event and parent folder from project
var folder = studio.project.workspace.masterEventFolder.getItem(folderPath);
var parentFolder = studio.project.workspace.masterEventFolder.getItem(parentFolderPath);
// If existing folder/event doesn't exist, create new, and assign appropriately
if (typeof folder == 'undefined') {
if (i < lineElements.length - 1){
var folder = studio.project.create('EventFolder');
folder.name = lineElements[i];
if (parentFolder != 'undefined'){
folder.folder = parentFolder;
}
} else {
var folder = studio.project.create('Event');
folder.name = lineElements[i];
if (parentFolder != 'undefined'){
folder.folder = parentFolder;
}
}
}
}
});
textFile.close();
}
This Would be Great help! May ask another question?
If i know the file path right now, may i open the file folder? such as “reveal in file explorer”