Add Duration To Txt File

I’m trying to merge a few script and I want to add the time to the text file log.

How can I link them please?

do shell script "echo  'Files not processed in Photoshop :'  > ~/Desktop/TIME.txt"
set noError to true
set startTime to (current date)

delay 2

set EndTime to (current date)
my myLogs((TimeToText(EndTime - startTime)) as text)
log myLogs((TimeToText(EndTime - startTime)) as text)

on myLogs(t)
	try
		do shell script "echo " & (quoted form of t) & ">> ~/Desktop/TIME.txt'"
	end try
end myLogs

on TimeToText(TheTime)
	(*
converts a number to a time string
parameters - TheTime [integer]: the time in seconds
returns [text]: the time in the format hh:mm:ss
*)
	if (class of TheTime) as text is "integer" then
		set TimeString to 1000000 + 10000 * (TheTime mod days div hours) -- hours
		set TimeString to TimeString + 100 * (TheTime mod hours div minutes) -- minutes
		set TimeString to (TimeString + (TheTime mod minutes)) as text -- seconds
		tell TimeString to set TheTime to (text -6 thru -5) & ":" & (text -4 thru -3) & ":" & (text -2 thru -1)
	end if
	return TheTime
end TimeToText

Hello.

I really don’t know what you mean, but here are two handlers, that you may use, you may even add a parameter
for the filename, and what you are logging to logtofile, as an excercise. :slight_smile:

set a to (current date)
delay 2
set b to (current date)

logToFile(a, b)
to logToFile(startDate, endDate)
	local c
	set c to endDate - startDate
	set lstr to date string of endDate & " time logged: " & (TimeToText(c))
	
	do shell script "echo  " & quoted form of lstr & ">>~/Desktop/Time.txt"
end logToFile

to TimeToText(anInt)
	local thehours, theMinutes, theseconds, tmstr
	set thehours to (anInt div 3600)
	set theMinutes to (anInt mod 3600) div 60
	set theseconds to anInt - ((thehours * 3600) - (theMinutes * 60))
	set tmstr to "" & text -2 thru -1 of ("00" & thehours) & ":" & text -2 thru -1 of ("00" & theMinutes) & ":" & text -2 thru -1 of ("00" & theseconds)
	return tmstr
end TimeToText


All I’m looking to do is add the result of (TimeToText(EndTime - startTime) to the end of the the already existing txtfile.

so I can see how long the script takes the “delay 2” is just part of the test.

Matt

Having said that your script works for me!! Many Thanks

Hello.

I think my approach, is simpler, and easier to get right, it seems to me that you should have had some “of it” statements inside your tell TimeString block.

General advice: Stay away from such Tell variable constructs, unless you absolutely need to make your code run as fast as possible, and don’t start using such stuff before you have something that works.

Most of the time, start as simple as possible, and have something that works, before you start creating an optimized version, this is very important for the motiviation, when you start making bigger things. Because then you know, that you have something that works, maybe not with the full functionality, but something that works.
When you have something that works, it is also much easier to identify what you did to make something not work. :slight_smile:

I went back to look at my code, to see if I could optimize it, as an educational exercise, just for you. It turned out, that there wasn’t anything to subtract, or add, as what costs here, is the mathematical operations, and I can nor add any, nor remove any. I started simple, and got a result that is almost optimal.

And it doesn’t matter either, because this code isn’t time critical in any sense, so there is no real need for optimizing it. Having said that, there is no reason for having bloated code either.