Script runs on Windows and OS X

This commit is contained in:
Daniel Wolf 2017-07-01 23:12:19 +02:00
parent 8093258e76
commit 2f1586624a
1 changed files with 81 additions and 46 deletions

View File

@ -106,8 +106,7 @@ function isFrameVisible(compItem, frameNumber) {
return Boolean(result); return Boolean(result);
} }
// On Windows, this is C:\ProgramData var settingsFilePath = Folder.userData.fullName + '/rhubarb-ae-settings.json';
var settingsFilePath = Folder.appData.fullName + '/rhubarb-ae-settings.json';
function readTextFile(fileOrPath) { function readTextFile(fileOrPath) {
var filePath = fileOrPath.fsName || fileOrPath; var filePath = fileOrPath.fsName || fileOrPath;
@ -156,17 +155,51 @@ function writeSettingsFile(settings) {
} }
} }
function exec(command, options) {
var showWindow = (options || {}).showWindow;
var osIsWindows = (system.osName || $.os).match(/windows/i); var osIsWindows = (system.osName || $.os).match(/windows/i);
// On Windows, calling a console application directly will hide the console window. Calling it // Depending on the operating system, the syntax for escaping command-line arguments differs.
// through cmd.exe will show it. function cliEscape(argument) {
// I don't know whether there's something similar for OS X. I only own the Windows version of return osIsWindows
// After Effects. ? '"' + argument + '"'
return system.callSystem(showWindow && osIsWindows ? 'cmd /C "' + command + '"' : command); : "'" + argument.replace(/'/g, "'\\''") + "'";
} }
function exec(command) {
return system.callSystem(command);
}
function execInWindow(command) {
if (osIsWindows) {
system.callSystem('cmd /C "' + command + '"');
} else {
// I didn't think it could be so complicated on OS X to open a new Terminal window,
// execute a command, then close the Terminal window.
// If you know a better solution, let me know!
var escapedCommand = command.replace(/"/g, '\\"');
var appleScript = '\
tell application "Terminal" \
-- Quit terminal \
-- Yes, that\'s undesirable if there was an open window before. \
-- But all solutions I could find were at least as hacky. \
quit \
-- Open terminal \
activate \
-- Run command in new tab \
set newTab to do script ("' + escapedCommand + '") \
-- Wait until command is done \
tell newTab \
repeat while busy \
delay 0.1 \
end repeat \
end tell \
quit \
end tell';
exec('osascript -e ' + cliEscape(appleScript));
}
}
var rhubarbPath = osIsWindows ? 'rhubarb.exe' : '/usr/local/bin/rhubarb';
// ExtendScript's resource strings are a pain to write. // ExtendScript's resource strings are a pain to write.
// This function allows them to be written in JSON notation, then converts them into the required // This function allows them to be written in JSON notation, then converts them into the required
// format. // format.
@ -396,7 +429,7 @@ function createDialogWindow() {
selectByTextOrFirst(controls.mouthComp, settings.mouthComp); selectByTextOrFirst(controls.mouthComp, settings.mouthComp);
extendedMouthShapeNames.forEach(function(shapeName) { extendedMouthShapeNames.forEach(function(shapeName) {
controls['mouthShape' + shapeName].value = controls['mouthShape' + shapeName].value =
settings.extendedMouthShapes[shapeName.toLowerCase()]; (settings.extendedMouthShapes || {})[shapeName.toLowerCase()];
}); });
selectByTextOrFirst(controls.targetFolder, settings.targetFolder); selectByTextOrFirst(controls.targetFolder, settings.targetFolder);
controls.frameRate.text = settings.frameRate || ''; controls.frameRate.text = settings.frameRate || '';
@ -510,20 +543,22 @@ function createDialogWindow() {
} }
// Check for correct Rhubarb version // Check for correct Rhubarb version
var version = exec('rhubarb --version', { showWindow: false }) || ''; var version = exec(rhubarbPath + ' --version') || '';
var match = version.match(/Rhubarb Lip Sync version ((\d+)\.(\d+).(\d+))/); var match = version.match(/Rhubarb Lip Sync version ((\d+)\.(\d+).(\d+))/);
if (!match) { if (!match) {
var isWindows = (system.osName || $.os).match(/windows/i); var instructions = osIsWindows
return 'Cannot find executable file "' + (isWindows ? 'rhubarb.exe' : 'rhubarb') + '". ' ? 'Make sure your PATH environment variable contains the Rhubarb Lip-Sync '
+ 'Make sure your PATH environment variable contains the Rhubarb Lip-Sync ' + 'application directory.'
+ 'application directory.'; : 'Make sure you have created this file as a symbolic link to the Rhubarb Lip-Sync '
+ 'executable (rhubarb).';
return 'Cannot find executable file "' + rhubarbPath + '". \n' + instructions;
} }
var versionString = match[1]; var versionString = match[1];
var major = Number(match[2]); var major = Number(match[2]);
var minor = Number(match[3]); var minor = Number(match[3]);
if (major != 1 || minor < 6) { if (major != 1 || minor < 6) {
return 'This script requires Rhubarb Lip-Sync 1.6.0 or a later 1.x version. ' return 'This script requires Rhubarb Lip-Sync 1.6.0 or a later 1.x version. '
'Your installed version is ' + versionString + ', which is not compatible.'; + 'Your installed version is ' + versionString + ', which is not compatible.';
} }
} }
@ -539,17 +574,17 @@ function createDialogWindow() {
writeTextFile(dialogFile, dialogText); writeTextFile(dialogFile, dialogText);
// Create command line // Create command line
var commandLine = 'rhubarb' var commandLine = rhubarbPath
+ ' --dialogFile "' + dialogFile.fsName + '"' + ' --dialogFile ' + cliEscape(dialogFile.fsName)
+ ' --exportFormat json' + ' --exportFormat json'
+ ' --extendedShapes "' + extendedMouthShapeNames.join('') + '"' + ' --extendedShapes ' + cliEscape(extendedMouthShapeNames.join(''))
+ ' --logFile "' + logFile.fsName + '"' + ' --logFile ' + cliEscape(logFile.fsName)
+ ' --logLevel fatal' + ' --logLevel fatal'
+ ' --output "' + jsonFile.fsName + '"' + ' --output ' + cliEscape(jsonFile.fsName)
+ ' "' + audioFileFootage.file.fsName + '"'; + ' ' + cliEscape(audioFileFootage.file.fsName);
// Run Rhubarb // Run Rhubarb
exec(commandLine, { showWindow: true }); execInWindow(commandLine);
// Check log for fatal errors // Check log for fatal errors
if (logFile.exists) { if (logFile.exists) {