Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const app = require("photoshop").app;
- const currentDocument = app.activeDocument
- var userFolder = null;
- var listener = null;
- var isEnabled = false;
- var isDirty = true;
- var CurrentIndex = -1;
- var DocumentName;
- var SaveType = 'Interval';
- var SaveQuality = 10;
- var SaveInterval = 12;
- var IntervalTimer;
- var TimeLeft;
- var NextSaveTime;
- var fs = require('fs');
- var files;
- var FileFormat_Selected = "jpg";
- const FileFormats = {
- jpg: 0, png: 1
- }
- const FileFormatsByIndex = [
- "jpg", "png"
- ]
- var SETTINGS = [
- { "TLREC_SaveInterval": SaveInterval },
- { "TLREC_SaveQuality": SaveQuality },
- { "TLREC_FileFormat_Selected": FileFormat_Selected },
- ];
- //console.log(time);
- function GetTime() {
- return Date.now();
- }
- function GetStorageItem(name) {
- return localStorage.getItem(name);
- }
- function SetStorageItem(name, value) {
- localStorage.setItem(name, value);
- }
- function StoreSettings() {
- for (var i = 0; i < SETTINGS.length; i++) {
- var obj = SETTINGS[i];
- for (var key in obj) {
- var value = obj[key];
- if (value) {
- SetStorageItem(key, value)
- }
- }
- }
- }
- function GetConfigValue(name) {
- for (var i = 0; i < SETTINGS.length; i++) {
- var obj = SETTINGS[i];
- for (var key in obj) {
- if (key == name) {
- return obj[key];
- }
- }
- }
- }
- function SetConfigValue(name, value) {
- console.log("Saving Config:", name, value);
- for (var i = 0; i < SETTINGS.length; i++) {
- var obj = SETTINGS[i];
- for (var key in obj) {
- if (key == name) {
- obj[key] = value;
- }
- }
- }
- StoreSettings();
- }
- function LoadSettings() {
- for (var i = 0; i < SETTINGS.length; i++) {
- var obj = SETTINGS[i];
- for (var key in obj) {
- var value = obj[key];
- var StorageValue = GetStorageItem(key)
- console.log(key, "Stored Value:", StorageValue)
- if (StorageValue) {
- SETTINGS[i][key] = StorageValue;
- }
- }
- }
- SaveInterval = GetConfigValue("TLREC_SaveInterval") * 1000;
- SaveQuality = GetConfigValue("TLREC_SaveQuality");
- FileFormat_Selected = GetConfigValue("TLREC_FileFormat_Selected");
- }
- function GetDocumentFileName() {
- const currentDocument = app.activeDocument;
- const currentTitle = currentDocument.title;
- const DocumentName = currentTitle.replace(/\.[^/.]+$/, "")
- //console.log(FileName);
- return DocumentName;
- }
- function GetFileIndex(FileName="") {
- const parts = FileName.split("_");
- const last = parts[parts.length-1];
- const num = last.match(/(\d+)/)[1];
- //console.log("INDEX:", num);
- return num;
- }
- function CheckFileAgainstDocument(FileName='') {
- const name = FileName.substring(0, FileName.lastIndexOf("_"));
- //console.log(name);
- var num = 0;
- if (name == DocumentName) {
- //console.log("PASSED:", name);
- num = parseInt(GetFileIndex(FileName));
- //console.log("INDEX:", num);
- if (num > CurrentIndex) {
- CurrentIndex = num;
- }
- }
- }
- function GetStoredFileIndexForDocument() {
- files.forEach((key, val) =>
- CheckFileAgainstDocument(key)
- );
- // Increment so we have the correct value
- // CurrentIndex = parseInt(CurrentIndex) + 1;
- }
- function UpdateTimerText() {
- if (TimeLeft > 0) {
- GetElement("lbl_SaveTime").innerHTML = ` ${Math.ceil(TimeLeft / 1000)}s`;
- GetElement("lbl_SaveTime").style.display = "";
- GetElement("lbl_SaveTimeDesc").style.display = "";
- GetElement("lbl_ChangesPending").style.display = "none";
- } else {
- GetElement("lbl_SaveTime").innerHTML = ` 0s`;
- if (TimeLeft < -1000) {
- GetElement("lbl_ChangesPending").style.display = "";
- GetElement("lbl_SaveTime").style.display = "none";
- GetElement("lbl_SaveTimeDesc").style.display = "none";
- }
- }
- }
- async function saveSnapshot() {
- TimeLeft = NextSaveTime - GetTime();
- UpdateTimerText();
- if (!isEnabled || !isDirty || TimeLeft > 0) {return;}
- // Prevent new save from being started to avoid missing single files when the saving process takes longer than the timer itself
- clearInterval(IntervalTimer);
- CurrentIndex = CurrentIndex + 1;
- const file = await userFolder.createFile(`${DocumentName}_${CurrentIndex}.${FileFormat_Selected.toLowerCase()}`);
- console.log("Writing . . .", `${DocumentName}_${CurrentIndex}.${FileFormat_Selected.toLowerCase()}`, parseInt(SaveQuality));
- const Snapshot = await app.activeDocument.save(file, { embedColorProfile: true, quality: parseInt(SaveQuality)});
- isDirty = false;
- UpdateIntervalTimer();
- }
- function UpdateIntervalTimer() {
- if (!isEnabled) {return;}
- if (IntervalTimer) {
- clearInterval(IntervalTimer);
- IntervalTimer = null;
- }
- console.log("Scheduled next update for:", (SaveInterval) + GetTime())
- console.log("Save Interval:", SaveInterval);
- NextSaveTime = SaveInterval + GetTime();
- // Start Timer with failsafe time
- IntervalTimer = setInterval(saveSnapshot, 500);
- }
- function UpdateSaveInterval() {
- SaveInterval = document.getElementById("sld_SaveInterval").value;
- // Failsafe for abnormal values
- if (SaveInterval >= 1 && SaveInterval <= 30) {
- SaveInterval = SaveInterval * 1000;
- } else {
- SaveInterval = 12000;
- }
- SetConfigValue("TLREC_SaveInterval", SaveInterval / 1000);
- UpdateIntervalTimer();
- }
- function UpdateQualitySetting() {
- SaveQuality = document.getElementById("sld_SaveQuality").value;
- if (SaveQuality < 1 || SaveQuality > 12) {
- SaveQuality = 10;
- }
- SetConfigValue("TLREC_SaveQuality", SaveQuality);
- }
- function QueueUpdate() {
- isDirty = true;
- }
- function GetElement(id) {
- return document.getElementById(id);
- }
- function SetEnabledState(state) {
- if (state) {
- GetElement("btnStartRecording").style.display = "none";
- GetElement("btnStopRecording").style.display = "";
- GetElement("lbl_SaveTimeDesc").style.display = "";
- GetElement("lbl_SaveTime").style.display = "";
- } else {
- GetElement("btnStartRecording").style.display = "";
- GetElement("btnStopRecording").style.display = "none";
- GetElement("lbl_SaveTimeDesc").style.display = "none";
- GetElement("lbl_SaveTime").style.display = "none";
- GetElement("lbl_ChangesPending").style.display = "none";
- }
- }
- async function StartRecording() {
- DocumentName = GetDocumentFileName();
- userFolder = await require('uxp').storage.localFileSystem.getFolder();
- if (userFolder != null) {
- isEnabled = true;
- files = fs.readdirSync(userFolder);
- GetStoredFileIndexForDocument();
- console.log("Retrieved Index:", CurrentIndex);
- if (SaveType == 'Interval') {
- SetEnabledState(true);
- UpdateIntervalTimer();
- }
- }
- }
- function StopRecording() {
- isEnabled = false;
- userFolder = null;
- files = null;
- CurrentIndex = -1;
- SaveType = 'Interval';
- clearInterval(IntervalTimer);
- IntervalTimer = null;
- SetEnabledState(false);
- }
- function Initialize() {
- // Initialize Settings
- LoadSettings();
- listener = (e, d) => { QueueUpdate(); }
- require('photoshop').action.addNotificationListener([
- {
- event: "historyStateChanged"
- }
- ], listener);
- // Apply Config Defaults
- GetElement("sld_SaveInterval").value = GetConfigValue("TLREC_SaveInterval");
- GetElement("sld_SaveQuality").value = GetConfigValue("TLREC_SaveQuality");
- // Events
- GetElement("btnStartRecording").addEventListener("click", StartRecording);
- GetElement("btnStopRecording").addEventListener("click", StopRecording);
- GetElement("sld_SaveInterval").addEventListener("change", UpdateSaveInterval);
- GetElement("sld_SaveQuality").addEventListener("change", UpdateQualitySetting);
- // Style Defaults
- GetElement("lbl_SaveTimeDesc").style.display = "none";
- GetElement("lbl_SaveTime").style.display = "none";
- // Format Dropdown
- GetElement("formatSelection").selectedIndex = FileFormats[GetConfigValue("TLREC_FileFormat_Selected")];
- FileFormat_Selected = FileFormatsByIndex[GetElement("formatSelection").selectedIndex];
- GetElement("formatSelection").addEventListener("change", evt => {
- FileFormat_Selected = FileFormatsByIndex[evt.target.selectedIndex];
- SetConfigValue("TLREC_FileFormat_Selected", FileFormat_Selected);
- });
- }
- Initialize();
Advertisement
Add Comment
Please, Sign In to add comment