Executing system commands

 
 
 

Use the system command to pass a string to the operating system to be executed. The operating system command’s output is returned.

For example:

system "ls -l";

On UNIX-based systems (not Windows) this will print the current directory to the Script Editor in long format. The Windows equivalent is:

system "dir";

Background processes (non-Windows only)

To run a command in the background (that is, do a non-blocking system call), you must redirect all of the command’s output:

system("cmd >/dev/null 2>&1 &");

Of course, you can send output to somewhere other than /dev/null if you like.

File names

You should always put pathnames in quotation marks. Especially on Mac OS, filenames can contain characters which have meaning on the command line, such as spaces, | (pipe), > (redirection), and & (run as background).

For example, instead of this:

string $fileName = {get this from somewhere}; system ("some_command " + $fileName);

...use this instead:

system ("some_command \"" + $fileName + "\"");

This makes the code platform-independent.

AppleScript and UNIX use different separators for folders. AppleScript uses colons (:) and UNIX uses forward slashes (/). Also, volumes in UNIX are prefixed with /Volumes/, while in AppleScript, the partition name begins the path.

In the following example, a scene file named eagle.ma is on a volume (partition) called Emerald.

The AppleScript representation is:

Emerald:projects:default:scenes:eagle.ma

The UNIX representation is:

/Volumes/Emerald/projects/default/scenes/eagle.ma

To open eagle.ma in TextEdit, you use:

tell application "TextEdit" open "Emerald:projects:default:scenes:eagle.ma" end tell

To open the file in TextEdit from the UNIX shell, you enter:

open -e /Volumes/Emerald/projects/default/scenes/eagle.ma

File name considerations for file referencing and importing

You cannot reference or import a file from the command line whose name is not a legal identifier unless you explicitly use the -rpr or -ns flags. This includes files whose file name begins with a number. To avoid this issue, it is recommended that you avoid naming files so that they begin with numbers. Otherwise, you will need to use the workaround below.

Workaround

  • Use the -rpr or -ns flags to explicitly specify a valid renaming prefix.
  • Rename the file to something starting with a letter (for example, a107953.sph.ma).

Line ends

To apply a UNIX command to the contents of a mayaAscii scene file on Mac OS X, convert the scene file to have UNIX line ends. You can do this with the Mac OS X tounix command.

For example, if your MEL code looks like this on another platform:

string $fileName = {get this from somewhere}; string $result = system ("grep something" + $fileName);

...use this instead:

if ('about -mac') { 	system("tounix \"" + $fileName + "\""); } string $result = system ("grep something \"" + $fileName + "\"");