Guest User

Untitled

a guest
Apr 22nd, 2022
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const app = require("photoshop").app;
  2. const currentDocument = app.activeDocument
  3. var userFolder = null;
  4. var listener = null;
  5. var isEnabled = false;
  6. var isDirty = true;
  7.  
  8. var CurrentIndex = -1;
  9. var DocumentName;
  10. var SaveType = 'Interval';
  11. var SaveQuality = 10;
  12. var SaveInterval = 12;
  13. var IntervalTimer;
  14. var TimeLeft;
  15. var NextSaveTime;
  16.  
  17. var fs = require('fs');
  18. var files;
  19.  
  20. var FileFormat_Selected = "jpg";
  21.  
  22. const FileFormats = {
  23.     jpg: 0, png: 1
  24. }
  25. const FileFormatsByIndex = [
  26.     "jpg", "png"
  27. ]
  28.  
  29. var SETTINGS = [
  30.     { "TLREC_SaveInterval":         SaveInterval },
  31.     { "TLREC_SaveQuality":          SaveQuality },
  32.     { "TLREC_FileFormat_Selected":  FileFormat_Selected },
  33. ];
  34.  
  35. //console.log(time);
  36.  
  37. function GetTime() {
  38.     return Date.now();
  39. }
  40.  
  41. function GetStorageItem(name) {
  42.     return localStorage.getItem(name);
  43. }
  44.  
  45. function SetStorageItem(name, value) {
  46.     localStorage.setItem(name, value);
  47. }
  48.  
  49. function StoreSettings() {
  50.     for (var i = 0; i < SETTINGS.length; i++) {
  51.         var obj = SETTINGS[i];
  52.         for (var key in obj) {
  53.             var value = obj[key];
  54.  
  55.             if (value) {
  56.                 SetStorageItem(key, value)
  57.             }
  58.         }
  59.     }
  60. }
  61.  
  62. function GetConfigValue(name) {
  63.     for (var i = 0; i < SETTINGS.length; i++) {
  64.         var obj = SETTINGS[i];
  65.         for (var key in obj) {
  66.             if (key == name) {
  67.                 return obj[key];
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73. function SetConfigValue(name, value) {
  74.     console.log("Saving Config:", name, value);
  75.     for (var i = 0; i < SETTINGS.length; i++) {
  76.         var obj = SETTINGS[i];
  77.         for (var key in obj) {
  78.             if (key == name) {
  79.                 obj[key] = value;
  80.             }
  81.         }
  82.     }
  83.  
  84.     StoreSettings();
  85. }
  86.  
  87. function LoadSettings() {
  88.     for (var i = 0; i < SETTINGS.length; i++) {
  89.         var obj = SETTINGS[i];
  90.         for (var key in obj) {
  91.             var value = obj[key];
  92.             var StorageValue = GetStorageItem(key)
  93.             console.log(key, "Stored Value:", StorageValue)
  94.  
  95.             if (StorageValue) {
  96.                 SETTINGS[i][key] = StorageValue;
  97.             }
  98.         }
  99.     }
  100.  
  101.     SaveInterval            = GetConfigValue("TLREC_SaveInterval") * 1000;
  102.     SaveQuality             = GetConfigValue("TLREC_SaveQuality");
  103.     FileFormat_Selected     = GetConfigValue("TLREC_FileFormat_Selected");
  104. }
  105.  
  106. function GetDocumentFileName() {
  107.     const currentDocument = app.activeDocument;
  108.     const currentTitle = currentDocument.title;
  109.    
  110.     const DocumentName = currentTitle.replace(/\.[^/.]+$/, "")
  111.    
  112.     //console.log(FileName);
  113.     return DocumentName;
  114. }
  115.  
  116. function GetFileIndex(FileName="") {
  117.     const parts = FileName.split("_");
  118.     const last = parts[parts.length-1];
  119.     const num = last.match(/(\d+)/)[1];
  120.    
  121.     //console.log("INDEX:", num);
  122.     return num;
  123. }
  124.  
  125. function CheckFileAgainstDocument(FileName='') {
  126.    
  127.     const name = FileName.substring(0, FileName.lastIndexOf("_"));
  128.     //console.log(name);
  129.    
  130.     var num = 0;
  131.     if (name == DocumentName) {
  132.         //console.log("PASSED:", name);
  133.         num = parseInt(GetFileIndex(FileName));
  134.         //console.log("INDEX:", num);
  135.         if (num > CurrentIndex) {
  136.             CurrentIndex = num;
  137.         }
  138.     }
  139. }
  140.  
  141. function GetStoredFileIndexForDocument() {
  142.     files.forEach((key, val) =>
  143.         CheckFileAgainstDocument(key)
  144.     );
  145.    
  146.     // Increment so we have the correct value
  147.     // CurrentIndex = parseInt(CurrentIndex) + 1;
  148. }
  149.  
  150. function UpdateTimerText() {
  151.     if (TimeLeft > 0) {
  152.         GetElement("lbl_SaveTime").innerHTML = ` ${Math.ceil(TimeLeft / 1000)}s`;
  153.         GetElement("lbl_SaveTime").style.display = "";
  154.         GetElement("lbl_SaveTimeDesc").style.display = "";
  155.         GetElement("lbl_ChangesPending").style.display = "none";
  156.     } else {
  157.         GetElement("lbl_SaveTime").innerHTML = ` 0s`;
  158.         if (TimeLeft < -1000) {
  159.             GetElement("lbl_ChangesPending").style.display = "";
  160.             GetElement("lbl_SaveTime").style.display = "none";
  161.             GetElement("lbl_SaveTimeDesc").style.display = "none";
  162.         }
  163.     }
  164. }
  165.  
  166. async function saveSnapshot() {
  167.     TimeLeft = NextSaveTime - GetTime();
  168.  
  169.     UpdateTimerText();
  170.  
  171.     if (!isEnabled || !isDirty || TimeLeft > 0) {return;}
  172.    
  173.     // Prevent new save from being started to avoid missing single files when the saving process takes longer than the timer itself
  174.     clearInterval(IntervalTimer);
  175.    
  176.     CurrentIndex = CurrentIndex + 1;
  177.     const file = await userFolder.createFile(`${DocumentName}_${CurrentIndex}.${FileFormat_Selected.toLowerCase()}`);
  178.     console.log("Writing . . .", `${DocumentName}_${CurrentIndex}.${FileFormat_Selected.toLowerCase()}`, parseInt(SaveQuality));
  179.    
  180.     const Snapshot = await app.activeDocument.save(file, { embedColorProfile: true, quality: parseInt(SaveQuality)});
  181.    
  182.     isDirty = false;
  183.     UpdateIntervalTimer();
  184. }
  185.  
  186. function UpdateIntervalTimer() {
  187.     if (!isEnabled) {return;}
  188.    
  189.     if (IntervalTimer) {
  190.         clearInterval(IntervalTimer);
  191.         IntervalTimer = null;
  192.     }
  193.  
  194.     console.log("Scheduled next update for:", (SaveInterval) + GetTime())
  195.     console.log("Save Interval:", SaveInterval);
  196.     NextSaveTime = SaveInterval + GetTime();
  197.  
  198.     // Start Timer with failsafe time
  199.     IntervalTimer = setInterval(saveSnapshot, 500);
  200. }
  201.  
  202. function UpdateSaveInterval() {
  203.     SaveInterval = document.getElementById("sld_SaveInterval").value;
  204.  
  205.     // Failsafe for abnormal values
  206.     if (SaveInterval >= 1 && SaveInterval <= 30) {
  207.         SaveInterval = SaveInterval * 1000;
  208.     } else {
  209.         SaveInterval = 12000;
  210.     }
  211.  
  212.  
  213.     SetConfigValue("TLREC_SaveInterval", SaveInterval / 1000);
  214.     UpdateIntervalTimer();
  215. }
  216.  
  217. function UpdateQualitySetting() {
  218.     SaveQuality = document.getElementById("sld_SaveQuality").value;
  219.  
  220.     if (SaveQuality < 1 || SaveQuality > 12) {
  221.         SaveQuality = 10;
  222.     }
  223.  
  224.     SetConfigValue("TLREC_SaveQuality", SaveQuality);
  225. }
  226.  
  227. function QueueUpdate() {
  228.     isDirty = true;
  229. }
  230.  
  231. function GetElement(id) {
  232.     return document.getElementById(id);
  233. }
  234.  
  235. function SetEnabledState(state) {
  236.     if (state) {
  237.         GetElement("btnStartRecording").style.display = "none";
  238.         GetElement("btnStopRecording").style.display = "";
  239.  
  240.         GetElement("lbl_SaveTimeDesc").style.display = "";
  241.         GetElement("lbl_SaveTime").style.display = "";
  242.     } else {
  243.         GetElement("btnStartRecording").style.display = "";
  244.         GetElement("btnStopRecording").style.display = "none";
  245.  
  246.         GetElement("lbl_SaveTimeDesc").style.display = "none";
  247.         GetElement("lbl_SaveTime").style.display = "none";
  248.  
  249.         GetElement("lbl_ChangesPending").style.display = "none";
  250.     }
  251. }
  252.  
  253. async function StartRecording() {  
  254.     DocumentName = GetDocumentFileName();
  255.    
  256.     userFolder = await require('uxp').storage.localFileSystem.getFolder();
  257.    
  258.     if (userFolder != null) {
  259.         isEnabled = true;
  260.  
  261.         files = fs.readdirSync(userFolder);
  262.         GetStoredFileIndexForDocument();
  263.        
  264.         console.log("Retrieved Index:", CurrentIndex);
  265.  
  266.         if (SaveType == 'Interval') {
  267.             SetEnabledState(true);
  268.             UpdateIntervalTimer();
  269.         }
  270.     }
  271. }
  272.  
  273. function StopRecording() {
  274.     isEnabled = false;
  275.    
  276.     userFolder = null;
  277.     files = null;
  278.    
  279.     CurrentIndex = -1;
  280.     SaveType = 'Interval';
  281.     clearInterval(IntervalTimer);
  282.     IntervalTimer = null;
  283.  
  284.     SetEnabledState(false);
  285. }
  286.  
  287. function Initialize() {
  288.     // Initialize Settings
  289.     LoadSettings();
  290.  
  291.     listener = (e, d) => { QueueUpdate(); }
  292.     require('photoshop').action.addNotificationListener([
  293.         {
  294.             event: "historyStateChanged"
  295.         }
  296.     ], listener);
  297.  
  298.     // Apply Config Defaults
  299.     GetElement("sld_SaveInterval").value = GetConfigValue("TLREC_SaveInterval");
  300.     GetElement("sld_SaveQuality").value = GetConfigValue("TLREC_SaveQuality");
  301.  
  302.     // Events
  303.     GetElement("btnStartRecording").addEventListener("click", StartRecording);
  304.     GetElement("btnStopRecording").addEventListener("click", StopRecording);
  305.     GetElement("sld_SaveInterval").addEventListener("change", UpdateSaveInterval);
  306.     GetElement("sld_SaveQuality").addEventListener("change", UpdateQualitySetting);
  307.  
  308.     // Style Defaults
  309.     GetElement("lbl_SaveTimeDesc").style.display = "none";
  310.     GetElement("lbl_SaveTime").style.display = "none";
  311.  
  312.     // Format Dropdown
  313.     GetElement("formatSelection").selectedIndex = FileFormats[GetConfigValue("TLREC_FileFormat_Selected")];
  314.  
  315.     FileFormat_Selected = FileFormatsByIndex[GetElement("formatSelection").selectedIndex];
  316.     GetElement("formatSelection").addEventListener("change", evt => {
  317.         FileFormat_Selected = FileFormatsByIndex[evt.target.selectedIndex];
  318.         SetConfigValue("TLREC_FileFormat_Selected", FileFormat_Selected);
  319.     });
  320. }
  321.  
  322. Initialize();
Advertisement
Add Comment
Please, Sign In to add comment