Copy link to clipboard
Copied
The script process files, sometimes it runs for seconds, sometimes for several minutes depending on the number of files. The code snippet below calculates the script execution time in milliseconds. What kind of format is necessary to convert the milliseconds to hrs : min : seconds ?
var start = new Date();
main();
var time = new Date() - start;
var timeInSeconds = time / 1000;
alert ('Done! ' + timeInSeconds + ' sec.');
1 Correct answer
Assumedly that was very clear script to understand, but you're right. When I started programming,
I remember some basic stuff (that now seems to be so obvious for me) was quite hard for long time.
I use variable keywords (var) only when there is a must for it, otherwise I avoid them completely.
So you got 15 : 00 : 03? I have no idea why there is 15 at beginning for you. For me it's 00 : 00 : 03
It's because I used 3 seconds delay by $.sleep(3000), so in the place you normally would use main()
If you
...Explore related tutorials & articles
Copy link to clipboard
Copied
I wrote a script to time actions. An action would run the script as its first and last step to get the time. The script would message you the time the action took. Adobe added a bug to CC 2015.5 that brakes my run twice scripts. It easy to work around Adobe bug setting up a new document and open document events to add garbage ti the document info metadata using the script event manager. Here is the workaround.
if (app.activeDocument.info.instructions.indexOf("Garbage") == -1 ) app.activeDocument.info.instructions = app.activeDocument.info.instructions + "Garbage";
Here is my ActionTime.jsx you should see the code needed to get the start and end times and calculate the duration,
/* =============================================================================================
// 2010 John J. McAssey (JJMack) http://www.mouseprints.net/
//
// This script is supplied as is. It is provided as freeware.
// The author accepts no liability for any problems arising from its use.
//
// This script is designed to be used by a Photoshop Action twice
// A good pratice to use when creating an actions that use this scipt is for the action
// not to do a save or play some other action between its two useages of this Script.
//
// The first time this script is used by an action the currend date and time
// are saved into the document's meta-data Info Instructions field.
//
// The second time this script used by the action this script retreives the date and time
// that was saved in the meta-data during the first usage.
// The script Outputs an Action duration message to Alerts the user of the time it took.
// Logs the Action Times into the ActionTime.log file in the folder where the script resides.
// Then the saved date and time is removed from the document's meta-data Info Instructions field.
//
// ============================================================================================== */
/*
<javascriptresource>
<about>$$$/JavaScripts/ActionTime/About=JJMack's ActionTime^r^rCopyright 2010 Mouseprints.^r^rRun twice script utility for action.^rNOTE:Don't play other actions between runs!^r^rFirst Run records Actions Start Time.^rSecond Run removes start time recording and outputs an execution time message.</about>
<enableinfo>true</enableinfo>
<category>JJMack's Action Run Twice Utility</category>
</javascriptresource>
*/
if (app.documents.length > 0) { // LOGFile faild trying this --> app.activeDocument.suspendHistory('ActionTime','main()');
if (app.activeDocument.info.instructions.indexOf("<ActionTime>") == -1 ){ // no footprint fisrt useage
//alert("first");
// Retreive Date Time for Foot Print
var stime = new Date().getTime();
//alert("Time = " + stime );
StartTime = timeStamp() ;
// put footprint in metadata info instructions
app.activeDocument.info.instructions = app.activeDocument.info.instructions + "<ActionTime>" + StartTime + "</ActionTime>" + "<ClockTime>" + stime + "</ClockTime>";
//alert( "Saved =" + "<ActionTime>" + StartTime + "</ActionTime>"+ "<ClockTime>" + stime + "</ClockTime>");
}
else {
//alert("second");
var etime = new Date().getTime();
//alert("Time = " + etime );
EndTime = timeStamp();
// Retreive saved information
ActionTimeOffset = app.activeDocument.info.instructions.indexOf("<ActionTime>") + "<ActionTime>".length;
ActionTimeLength = app.activeDocument.info.instructions.indexOf("</ActionTime") -ActionTimeOffset;
StartTime = app.activeDocument.info.instructions.substr(ActionTimeOffset, ActionTimeLength);
ClockTimeOffset = app.activeDocument.info.instructions.indexOf("<ClockTime>") + "<ClockTime>".length;
ClockTimeLength = app.activeDocument.info.instructions.indexOf("</ClockTime") -ClockTimeOffset;
stime = app.activeDocument.info.instructions.substr(ClockTimeOffset, ClockTimeLength);
duration = ((etime - stime)/1000);
alert("ActionTime \rStart = " + StartTime + " \rEnd = " + EndTime + " \rTime = " + duration + " Seconds");
// Log Edit Session into Log File
var scriptLocation = findScript()+ "0";
var LOGFilePath = scriptLocation.slice(0,-4) + "log";
var LOGFile = File(LOGFilePath);
writeLOG(app.activeDocument.name + " Start=" + StartTime + " End=" + EndTime + " Time=" + duration + " Seconds\r")
// End log File
// Remove footprint from metadata info instructions
before = app.activeDocument.info.instructions.substr(0,app.activeDocument.info.instructions.indexOf("<ActionTime>"));
afterOffset = app.activeDocument.info.instructions.indexOf("</ClockTime>") + "</ClockTime>".length;
after = app.activeDocument.info.instructions.substr(afterOffset, app.activeDocument.info.instructions.length - afterOffset);
//alert ("before = " + before + " after = " + after);
app.activeDocument.info.instructions = before + after;
}
}
else { alert("You must have at least one open document to run this script!"); }
///////////////////////////////////////////////////////////////////////////////
// main function
///////////////////////////////////////////////////////////////////////////////
function main(){
}
///////////////////////////////////////////////////////////////////////////////
// END - main function
///////////////////////////////////////////////////////////////////////////////
function timeStamp(){
// Get the time and format it
var digital = new Date();
var hours = digital.getHours();
var minutes = digital.getMinutes();
var seconds = digital.getSeconds();
var amOrPm = "AM";
if (hours > 11) amOrPm = "PM";
if (hours > 12) hours = hours - 12;
if (hours == 0) hours = 12;
if (minutes <= 9) minutes = "0" + minutes;
if (seconds <= 9) seconds = "0" + seconds;
// Get the date and format it
var date = new Date();
var d = date.getDate();
var day = (d < 10) ? '0' + d : d;
var m = date.getMonth() + 1;
var month = (m < 10) ? '0' + m : m;
var yy = date.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;
// create a variable with the fully formatted the time and date
// todaysDate = hours + ":" + minutes + ":" + seconds + " " + amOrPm + " - " + day + "/" + month + "/" + year;
// todaysDate = hours + ":" + minutes + ":" + seconds + " " + amOrPm + " - " + month + "/" + day + "/" + year;
MonthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
todaysDate = hours + ":" + minutes + ":" + seconds + " " + amOrPm + " " + MonthNames[date.getMonth()] + " " + date.getDate() + ", " + year;
return todaysDate;
}
// Find the location where this script resides
function findScript() {
var where = "";
try {
FORCEERROR = FORCERRROR;
}
catch(err) {
// alert(err.fileName);
// alert(File(err.fileName).exists);
where = File(err.fileName);
}
return where;
}
// Write LOG file
function writeLOG(log) {
try {
if(LOGFile.exists) {
LOGFile.open ("e");
LOGFile.seek (0,2); // Move to EOF
} else {
LOGFile.open ("w"); // Add unicode marker if we change to LOG file format for this log file
}
LOGFile.encoding = "UTF8"; // set UTF8
LOGFile.write(log);
LOGFile.close();
} catch (e) {
alert(e);
} finally {
}
return;
}
Copy link to clipboard
Copied
ROBOT version:
function SLC(v1, v2, v3) {function slc(v) {return eval("('0' + (nD.get" + v + ")).slice(-2)")}; return slc(v1) + ' : ' + slc(v2) + ' : ' + slc(v3)}
nD = new Date(), $.sleep(3000), nD = new Date(new Date() - nD), SLC('Hours() - 1', 'Minutes()', 'Seconds()')
HUMAN version:
function SLC(v1, v2, v3) {
function slc(v) {
return eval("('0' + (nD.get" + v + ")).slice(-2)")
}
return slc(v1) + ' : ' + slc(v2) + ' : ' + slc(v3)
}
nD = new Date(), $.sleep(3000), nD = new Date(new Date() - nD)
SLC('Hours() - 1', 'Minutes()', 'Seconds()')
ROMAN version:
function SLC(v1, v2, v3) {
function slc(v) {
return eval("('0' + (new Date(new Date() - nD).get" + v + ")).slice(-2)")
}
return slc(v1) + ' : ' + slc(v2) + ' : ' + slc(v3)
}
nD = new Date(), $.sleep(3000), SLC('Hours() - 1', 'Minutes()', 'Seconds()')
Copy link to clipboard
Copied
OK, yes understood. I am not a programer and find the script difficult to interpret. For instance:
alert (SLC('Hours() - 1', 'Minutes()', 'Seconds()'))
prints 15 : 00 : 03
I am not sure where those values come from since the script has no variables with values.
Can you please advise how to pass the values to the Hours(), Minutes() and Seconds() ?
Copy link to clipboard
Copied
Assumedly that was very clear script to understand, but you're right. When I started programming,
I remember some basic stuff (that now seems to be so obvious for me) was quite hard for long time.
I use variable keywords (var) only when there is a must for it, otherwise I avoid them completely.
So you got 15 : 00 : 03? I have no idea why there is 15 at beginning for you. For me it's 00 : 00 : 03
It's because I used 3 seconds delay by $.sleep(3000), so in the place you normally would use main()
If you'd like to see how result will look like with more time than 3 seconds let me explain some rules:
1 second = 1'000
1 minute = 60'000
1 hour = 3'600'000
So if for ex. process of your script takes 2 hours 8 minutes 6 seconds you had to put:
7686000 ie. (2 * 3600000) + (8 * 60000) + (6 * 1000) instead of nD into this test script:
function SLC(v1, v2, v3) {
function slc(v) {
return eval("('0' + (new Date(new Date() - (new Date() - 7686000)).get" + v + ")).slice(-2)")
}
return slc(v1) + ' : ' + slc(v2) + ' : ' + slc(v3)
}
alert(SLC('Hours() - 1', 'Minutes()', 'Seconds()'))
As you see I removed nD = new Date(), $.sleep(3000), part. This way avoiding waiting I could get instant result.
Tell me will be your result 02 : 08 : 06. If I'm right for some reason it can be increased of 15 hours, so 17 : 08 : 06
If so then in original script (from previous post of me) just change 'Hours() - 1 to 'Hours() - 16
Copy link to clipboard
Copied
Thank you for the clear explanation. The script makes senses now and works well. I did get the 17:08:06 when replacing nD with 7686000 which I amended following your suggestion and got 02:08:06.
I am also not sure why originally I got 15:00:03. When running the - get date and time stamp function - I do get the correct date and time.
Copy link to clipboard
Copied
If that is what you wanted mark answer as correct so if anyone else has similar problem, solution can be found faster
Copy link to clipboard
Copied
Better use this.
$.hiresTimer;
main();
var timeInSeconds = $.hiresTimer/1000000;
alert ('Done! ' + timeInSeconds.toFixed(2) + ' sec.');
Copy link to clipboard
Copied
Thanks, I have been testing different timers. The simplicity of the hiresTimer looks great. I tested it and works well. Is it possible to see an example which uses minutes and hours? I don't how how to add minutes and hours to the hires timer.
Copy link to clipboard
Copied
You have 'minutes and hours' in my script I wrote for you, isn't that you asked for? You already got everything.
$.hiresTimer, $.sleep(3000);
($.hiresTimer / 1000).toFixed(0)
is the same what:
nD = new Date()
$.sleep(3000)
new Date() - nD
Copy link to clipboard
Copied
ok, try )
$.hiresTimer;
main();
var timeInSeconds = $.hiresTimer/1000000;
var h = Math.floor(timeInSeconds/3600);
var m = Math.floor((timeInSeconds - 3600*h)/60);
var s = Math.floor(timeInSeconds - 3600*h - 60*m);
var ms = Math.round((timeInSeconds - 3600*h - 60*m - s)*1000);
h = "0"+h; h = h.substr(h.length-2);
m = "0"+m; m = m.substr(m.length-2);
s = "0"+s; s = s.substr(s.length-2);
alert ('Done! ' + h + ":" + m + ":" + s + "." + ms);
Copy link to clipboard
Copied
Thanks, I need to run additional tests. The first test, was fine. It run for a few seconds and printed seconds and milliseconds. The second test which run for 1 hr and 7 minutes printed a large number in milliseconds only. I will run a detailed test and let you know what I find out.

