Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save berniebernie/c493e1d13a8aa5a4f161 to your computer and use it in GitHub Desktop.

Select an option

Save berniebernie/c493e1d13a8aa5a4f161 to your computer and use it in GitHub Desktop.
Function that allows to write text to a temp location (set by maya's internalVar -userTempDir) or a given file, then opens it in a notepad (Notepad for Win, Gedit for linux, and TextEdit for macos)
proc writeToTextFileAndOpen(string $textToWrite, string $fileLocation, int $openAfterWriting){
//cross platform write to textfile or temp textfile
string $tmpfile = "";
if($fileLocation == ""){
string $file = `file -q -sn -shn`;
int $r = `rand 1000 10000`;
$file = ($file=="")?"tmp_maya_file."+$r+".txt":$file+"."+$r+".txt";
string $myScriptDir = `internalVar -utd`;
$tmpfile = $myScriptDir+$file;
}else{
$tmpfile = $fileLocation;
}
int $outFileId = fopen($tmpfile,"w");
if ($outFileId == 0 ) {
scriptEditorInfo -ch;
print $textToWrite;
warning ("\n\n// Could not open output file " + $tmpfile + " wrote to the script editor window instead.");
}else{
fprint $outFileId $textToWrite;
fclose $outFileId;
if($openAfterWriting){
if(`about -win` == 1){
system("load " + `toNativePath($tmpfile)`); //win
}else if(`about -li` == 1){
system("gedit "+$tmpfile+">/dev/null 2>&1 &"); //linux
}else if(`about -mac` == 1){
system("TextEdit "+$tmpfile+">/dev/null 2>&1 &"); //macOs, untested
}else{
scriptEditorInfo -ch;
print $textToWrite;
warning ("\n\n// Could not figure out system, wrote to the script editor window instead.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment