Simple script to read preferences from a file and store them into globalState variables
// This script reads a tab delimited file and set globalstate variables to the values written in the file.
// The text file is jsut a TAB delimited file, but I decided to use a different extension (.dfp)
// to be sure that SF read the correct file. Obviously you can change that in the extCheck variable.
// This script looks in the text file for the names that match the switch/case statement, and assign those values to globalstate variables.
// My text file that matches this script look like this:
//
// boomstrip 1
// radiostrip 1
// fill 1
// toadr 1
// untreated 1
// orig 1
// edit 1
//
// This file will set all the variables below (that are set to default state at the beginning of the script) to 1.
var fullPath = decodeURIComponent(sf.appleScript.finder.selection.getItem(0).asItem.path); // get the path and file name of the selected file in finder
var filename = fullPath.split('/').slice(-1)[0];
var extension = filename.split('.').slice(-1)[0];
var extCheck = 'dfp' //extension of the tab delimited file to match
var i
var x
// set defualt values
globalState.boomStripTracks = 4
globalState.radioStripTracks = 6
globalState.fillTracks = 2
globalState.toBeAdrTracks = 6
globalState.untreatedTracks = 8
globalState.origTracks = 4
globalState.editTracks = 4
if (extension != extCheck) // Check if the text file is .dfp
{
log('Error', 'Please select a .dfp file');
throw 0; //exit script now
}
var nameList = sf.file.readLines({
path: fullPath
}).lines;
for (i = 0; i < nameList.length; i++) {
var thisLine = nameList[i];
var lineArray = thisLine.split('\t');
switch (lineArray[0]) {
case 'boomstrip':
globalState.boomStripTracks = parseInt(lineArray[1]);
break;
case 'radiostrip':
globalState.radioStripTracks = parseInt(lineArray[1]);
break;
case 'fill':
globalState.fillTracks = parseInt(lineArray[1]);
break;
case 'toadr':
globalState.toBeAdrTracks = parseInt(lineArray[1]);
break;
case 'untreated':
globalState.untreatedTracks = parseInt(lineArray[1]);
break;
case 'edit':
globalState.editTracks = parseInt(lineArray[1]);
break;
case 'orig':
globalState.origTracks = parseInt(lineArray[1]);
}
}
log('Boom Strips tracks:', globalState.boomStripTracks.toString());
log('Radio Strips tracks:', globalState.radioStripTracks.toString());
log('Fill tracks:', globalState.fillTracks.toString());
log('To Be ADR tracks:', globalState.toBeAdrTracks.toString());
log('Untreated tracks:', globalState.untreatedTracks.toString());
log('Original tracks:', globalState.origTracks.toString());
log('Edit tracks:', globalState.editTracks.toString());
- DDavide Favargiotti @dieffe
The preference file is going to stay in the same folder as my Pro Tools session.
Then I can feed other scripts with the variables this script has read.Christian Scheuer @chrscheuer2018-08-20 20:49:04.017Z
Nice!
Did you know that you can get the path to the currently open Pro Tools Session?This makes another variation on the script possible, one in which the filename is always the same - for instance "settings.txt", and it's supposed to be in the folder of the open Pro Tools project.
Then you could do:
var sessionPath = sf.ui.proTools.mainWindow.sessionPath; var sessionDir = sessionPath.split('/').slice(0, -1).join('/'); var settingsPath = sessionDir + '/settings.txt'; if (!sf.file.exists({ path: settingsPath }).exists) { log('Error', 'Could not find a settings.txt in the current session\'s folder'); throw 0; //exit script now }
Just a variation :)
- DDavide Favargiotti @dieffe
Ah very cool!
- In reply todieffe⬆:Christian Scheuer @chrscheuer2018-08-20 20:45:58.968Z
Great work, @dieffe!
A slight modification I would do is to try to avoid giant
if
blocks by using early escape.
Basically, if an if block is merely checking for a condition to exist and to fail if it doesn't, then it's better to simply stop the script at that point.
Imagine you had more than one condition - then you'd have many many nested if blocks which wouldn't be pretty. By simply stopping the script -throw 0
- you make the code easier to read.This is what I would put in instead of the big if block, before all the code that actually does the work.
if (extension != extCheck) // Check if the text file is .dfp { log('Error', 'Please select a .dfp file'); throw 0; //exit script now }
- DDavide Favargiotti @dieffe
Ah nice!
I didn't know:Throw 0
I'll edit the script now, thanks!
Christian Scheuer @chrscheuer2018-08-20 20:51:57.869Z
A variation of this pattern is also to throw the error directly, like this (no
log
call needed):throw 'Please select a .dfp file';
Christian Scheuer @chrscheuer2018-08-20 20:52:31.393Z
throw 0
will silently fail/stop, whilethrow 'some string'
will stop and print the string as the error message.
- Progresswith doing this idea
- DDavide Favargiotti @dieffe
Edited to implement @chrscheuer early escape suggestion.