Namespaces

Variants
Actions

Javascript in Max 09 - the Folder Object

From Cycling '74 Wiki
Jump to: navigation, search

The Folder object is a js “external object” defined in the Max object called jsfolder. It is used to iterate through files in a folder.

Contents

Folder Constructor

f = new Folder(pathname);

pathname can either be the name of a folder in the search path or a complete pathname using Max path syntax.

Example:

f = new Folder("patches");    
// would try to find the patches folder in the search path
f = new Folder("Disk:/folder1/folder2");  
// uses an absolute path

After creating a Folder object, you'll probably want to restrict the files you see while traversing it by setting the typelist property:

f.typelist = [ "iLaF" , "maxb" , "TEXT" ]; 
// typical max files

Check the file max-fileformats.txt inside the init folder in the Cycling ’74 folder for filetype codes and their associated extensions.

As a Folder object traverses through the files, you can find out information about the current file using its file properties. You can also determine whether you've looked at all properties by testing the end property. The following code prints the names of all files found in the folder.

while (!f.end) {
  post(f.filename);
  post();
  f.next();
}

To finish with the Folder object, you can either delete it, or send it the close message if you might want to reuse it.

f.close ();

Folder Properties

Two types of properties of a Folder are available: some refer to the current file within the folder, and some refer to the Folder object’s state. Most of these properties are read-only.

Folder State Properties:

end (Boolean, get)

Non-zero (true) if there are no more files to examine in the folder, or if the pathname argument to the Folder object didn’t find a folder.

count (Number, get)

The total number of files of the specified type(s) contained in the folder.

pathname (String, get)

The full pathname of the folder

typelist (Array of Strings, get/set)

The list of file types that will be used to find files in the folder. To search for all files (the default), set the typelist property to an empty array.

Current File Properties:

filename (String, get)

The name of the current file.

moddate (Array, get)

An array containing the values year, month, day, hour, minute, and second with the last modified date of the current file. These values can be used to create a Javascript Date object.

filetype (String, get)

The four-character code associated with the current file's filetype. These codes are listed in the file max-fileformats.txt, which is located at /Library/Application Support/Cycling ’74 on Macintosh and C:\Program Files\Common Files\Cycling ’74 on Windows. If there is no mapping for the file's extension, a nil value is returned.

extension (String, get)

The extension of the current file's name, including the period. If there are no characters after the period, a nil value is returned.

Folder Methods

reset ( )

Start iterating at the beginning of the list of files. Re-opens the folder if it was previously closed with the close() function.

next ( )

Moves to the next file.

close ( )

Closes the folder. To start using it again, call the reset() function.