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);
}
// On Windows, this is C:\ProgramData
var settingsFilePath = Folder.appData.fullName + '/rhubarb-ae-settings.json';
var settingsFilePath = Folder.userData.fullName + '/rhubarb-ae-settings.json';
function readTextFile(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);
// On Windows, calling a console application directly will hide the console window. Calling it
// through cmd.exe will show it.
// I don't know whether there's something similar for OS X. I only own the Windows version of
// After Effects.
return system.callSystem(showWindow && osIsWindows ? 'cmd /C "' + command + '"' : command);
// Depending on the operating system, the syntax for escaping command-line arguments differs.
function cliEscape(argument) {
return osIsWindows
? '"' + argument + '"'
: "'" + 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.
// This function allows them to be written in JSON notation, then converts them into the required
// format.
@ -396,7 +429,7 @@ function createDialogWindow() {
selectByTextOrFirst(controls.mouthComp, settings.mouthComp);
extendedMouthShapeNames.forEach(function(shapeName) {
controls['mouthShape' + shapeName].value =
settings.extendedMouthShapes[shapeName.toLowerCase()];
(settings.extendedMouthShapes || {})[shapeName.toLowerCase()];
});
selectByTextOrFirst(controls.targetFolder, settings.targetFolder);
controls.frameRate.text = settings.frameRate || '';
@ -510,20 +543,22 @@ function createDialogWindow() {
}
// 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+))/);
if (!match) {
var isWindows = (system.osName || $.os).match(/windows/i);
return 'Cannot find executable file "' + (isWindows ? 'rhubarb.exe' : 'rhubarb') + '". '
+ 'Make sure your PATH environment variable contains the Rhubarb Lip-Sync '
+ 'application directory.';
var instructions = osIsWindows
? 'Make sure your PATH environment variable contains the Rhubarb Lip-Sync '
+ '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 major = Number(match[2]);
var minor = Number(match[3]);
if (major != 1 || minor < 6) {
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);
// Create command line
var commandLine = 'rhubarb'
+ ' --dialogFile "' + dialogFile.fsName + '"'
var commandLine = rhubarbPath
+ ' --dialogFile ' + cliEscape(dialogFile.fsName)
+ ' --exportFormat json'
+ ' --extendedShapes "' + extendedMouthShapeNames.join('') + '"'
+ ' --logFile "' + logFile.fsName + '"'
+ ' --extendedShapes ' + cliEscape(extendedMouthShapeNames.join(''))
+ ' --logFile ' + cliEscape(logFile.fsName)
+ ' --logLevel fatal'
+ ' --output "' + jsonFile.fsName + '"'
+ ' "' + audioFileFootage.file.fsName + '"';
+ ' --output ' + cliEscape(jsonFile.fsName)
+ ' ' + cliEscape(audioFileFootage.file.fsName);
// Run Rhubarb
exec(commandLine, { showWindow: true });
execInWindow(commandLine);
// Check log for fatal errors
if (logFile.exists) {