Parsing infos/numbers

Hello. I still have some trouble parsing my file…

The begening is working ok, but for my script i have to convert hour:min:seconds to seconds; and that part is not working…
Applescript tells me : cant set “23” as a number…

set bigtext to read "my file.log"
set nbPar to the number of paragraphs of bigtext
set previous_end_time to 0

repeat with i from 1 to nbPar - 2
	
	
	set AppleScript's text item delimiters to ","
	set enregistrement_a_traiter to paragraph i of bigtext
	set enregistrement_a_traiter2 to text items of enregistrement_a_traiter
	
	set nom_clip to item 6 of enregistrement_a_traiter2
	
	set duration to item 8 of enregistrement_a_traiter2
	
	
	set duration to my replaceString(duration, ".", ":")
	
	set duration to convert_to_seconds(duration)
	
	set point_out to (previous_end_time + duration)
	set point_in to previous_end_time + 9
	
	--my coupage_export(nom_clip, duration, point_in, point_out)
	
	
	
	set previous_end_time to point_out
	set i to i + 1
	
	set AppleScript's text item delimiters to ""
	
end repeat

on coupage_export(nom_clip, duree_clip, point_in, point_out)
	
	tell application "QuickTime Player"
		
		activate
		open "archive.mxf"
		if not (exists document 1) then error number -128
		
		trim document 1 from point_in to point_out
		set the target_file to ((path to movies folder as string) & nom_clip & "-" & ".mxf")
		export document 1 in file target_file using settings preset "Computer"
		delay 1
	end tell
	
	tell application "System Events"
		tell process "QuickTime Player"
			keystroke "z" using command down
			
			
		end tell
	end tell
	delay 2
	
	
end coupage_export

on convert_to_seconds(duree)
	
	set AppleScript's text item delimiters to ":"
	
	set duree_a_traiter to text items of duree
	
	
	set frames to item 4 of duree_a_traiter
	set sec to item 3 of duree_a_traiter
	
	
	set min to item 2 of duree_a_traiter
	set hour to item 1 of duree_a_traiter
	
	set dur_sec to 1 + sec + 60 * min + 3600 * hour
	set AppleScript's text item delimiters to ""
	display dialog dur_sec
	return dur_sec
end convert_to_seconds

on replaceString(theText, oldString, newString)
	set AppleScript's text item delimiters to oldString
	set tempList to every text item of theText
	set AppleScript's text item delimiters to newString
	set theText to the tempList as string
	set AppleScript's text item delimiters to ""
	return theText
end replaceString

The probleme is within the convert to seconds routine

when i sendr a duration in the code , like : “00:00:12:05”, it works; but as soon as i send the variable duration into my routine, it doesnt work anymore and cant use the numbers as numbers…

Would you have any idee?

Here is a bit of my log file

12/04/12,15:04:00.00,00:00:00.00,12/13 mardi ,7,1813215_OFF_PERTURBATIONS_SNCF_LIGNE_ORLEANS_TOURS,00001E5Y,00:00:23.04
12/04/12,15:04:33.00,00:00:00.00,12/13 mardi ,12,1813328_ITV_RESP_DE_L_EXERCICE_AIRNUC,00001E64,00:00:25.16

Your log file points out that the last number of the time expression is separated by dot instead of colon.
On Lion and higher you can use

set AppleScript’s text item delimiters to {“:”, “.”}

yes i know, thats wy i use the

set duration to my replaceString(duration, ".", ":")

routine, to get rid of the dots and get 00:00:00:00 forms :wink:

Doesnt solve my probleme

The error message says that the string can not be treated as number.
Try to coerce the text items explicitly to integer


on convert_to_seconds(duree)
	
	set AppleScript's text item delimiters to ":"
	
	set duree_a_traiter to text items of duree
	
	set frames to item 4 of duree_a_traiter as integer
	set sec to item 3 of duree_a_traiter as integer
	set min to item 2 of duree_a_traiter as integer
	set hour to item 1 of duree_a_traiter as integer
	
	set dur_sec to 1 + sec + 60 * min + 3600 * hour
	set AppleScript's text item delimiters to ""
	display dialog dur_sec
	return dur_sec
end convert_to_seconds

Hello

You pass three integers.
What is the routine supposed to use ?
Maybe it require floating numbers.

I tested your code with :


--set bigtext to read "my file.log"
set bigtext to "12/04/12,15:04:00.00,00:00:00.00,12/13 mardi ,7,1813215_OFF_PERTURBATIONS_SNCF_LIGNE_ORLEANS_TOURS,00001E5Y,00:00:23.04
12/04/12,15:04:33.00,00:00:00.00,12/13 mardi ,12,1813328_ITV_RESP_DE_L_EXERCICE_AIRNUC,00001E64,00:00:25.16

"
set nbPar to the number of paragraphs of bigtext
set previous_end_time to 0

repeat with i from 1 to nbPar - 2
	
	
	set AppleScript's text item delimiters to ","
	set enregistrement_a_traiter to paragraph i of bigtext
	set enregistrement_a_traiter2 to text items of enregistrement_a_traiter
	
	set nom_clip to item 6 of enregistrement_a_traiter2
	
	set duration to item 8 of enregistrement_a_traiter2
	
	set duration to my replaceString(duration, ".", ":")
	
	set duration to convert_to_seconds(duration)
	
	set point_out to (previous_end_time + duration)
	
	set point_in to previous_end_time + 9
	
	my coupage_export(nom_clip, duration, point_in, point_out)
	
	
	
	set previous_end_time to point_out
	set i to i + 1
	
	set AppleScript's text item delimiters to ""
	
end repeat

on coupage_export(nom_clip, duree_clip, point_in, point_out)
	log {nom_clip, duree_clip, point_in, point_out}
	log class of duree_clip
	log class of point_in
	log class of point_out
	(*
	tell application "QuickTime Player"
		
		activate
		open "archive.mxf"
		if not (exists document 1) then error number -128
		
		trim document 1 from point_in to point_out
		set the target_file to ((path to movies folder as string) & nom_clip & "-" & ".mxf")
		export document 1 in file target_file using settings preset "Computer"
		delay 1
	end tell
	
	tell application "System Events"
		tell process "QuickTime Player"
			keystroke "z" using command down
			
			
		end tell
	end tell
	delay 2
	*)
	
end coupage_export

on convert_to_seconds(duree)
	
	set AppleScript's text item delimiters to ":"
	
	set duree_a_traiter to text items of duree
	
	
	set frames to item 4 of duree_a_traiter
	set sec to item 3 of duree_a_traiter
	
	
	set min to item 2 of duree_a_traiter
	set hour to item 1 of duree_a_traiter
	
	set dur_sec to 1 + sec + 60 * min + 3600 * hour
	set AppleScript's text item delimiters to ""
	--display dialog dur_sec
	return dur_sec
end convert_to_seconds

on replaceString(theText, oldString, newString)
	set AppleScript's text item delimiters to oldString
	set tempList to every text item of theText
	set AppleScript's text item delimiters to newString
	set theText to the tempList as string
	set AppleScript's text item delimiters to ""
	return theText
end replaceString

Yvan KOENIG (VALLAURIS, France) mardi 4 décembre 2012 17:15:42

I tryed to coerce as interger, as number, doesnt work…

@Yvan Koenig i tryed your solution, doesnt work either. Cant parse 23 as number.

What did you mean by “you pass three integer” ?

this works :

on convert_to_seconds(duree)
   set duree to "00:02:10:10"
   set AppleScript's text item delimiters to ":"
   
   set duree_a_traiter to text items of duree
   
   
   set frames to item 4 of duree_a_traiter
   set sec to item 3 of duree_a_traiter
   
   
   set min to item 2 of duree_a_traiter
   set hour to item 1 of duree_a_traiter
   
   set dur_sec to 1 + sec + 60 * min + 3600 * hour
   set AppleScript's text item delimiters to ""
   display dialog dur_sec
   return dur_sec
end convert_to_seconds

this doesnt

on convert_to_seconds(duree)
   
   set AppleScript's text item delimiters to ":"
   
   set duree_a_traiter to text items of duree
   
   
   set frames to item 4 of duree_a_traiter
   set sec to item 3 of duree_a_traiter
   
   
   set min to item 2 of duree_a_traiter
   set hour to item 1 of duree_a_traiter
   
   set dur_sec to 1 + sec + 60 * min + 3600 * hour
   set AppleScript's text item delimiters to ""
   display dialog dur_sec
   return dur_sec
end convert_to_seconds

The script can’t use the parsed data for the number of seconds i’m trying to calculate

In which line does the error occur and what is the exact error message and error number?

Error is -1700

The error is comming when i coerce as integer for the first time

set frames to item 4 of duree_a_traiter as integer

or if i dont coerce, it happens when i’m doing maths

 set dur_sec to 1 + sec + 60 * min + 3600 * hour

The exact error in english i can’t tell you because my system is in french, but it could be translated to somthing like : it’s is impossible to render "04"\ to an integer type. number -1700 from “04” to integer.

Thank you for your help! I’m working on this error for hours…

insert the line

log item 4 of duree_a_traiter 

before the set frames line and look into the event log how the value looks like
I guess there is an additional character which cannot be coerced to number

Wow thanks for the tip. Didnt know about the log command.

I loged my variables and they look like this :
(04)
(23)
(00)
(00)

I tryed to log other integers and they look the same…

please log also length of item 4 of duree_a_traiter. It must be 2.

we might have somthinh the log of the lenght is (5)

then there are probably some additional control characters at the end of the string

Try this

set frames to text 1 thru 2 of item 4 of duree_a_traiter as integer

It returns 0 when i do this… or 2… it returns a single digit and still dosnt accept it as integer

But when i log the leght, i now have 2

Another way is to look into the text file with a hex editor (like HexFiend), the hex dump shows all characters

It looks like i have an extra char in my hex dump… what do you think?
The duration is : 00:00:31.07

How can i get rid of this.

It looks like the text is UTF-16
Try

set bigtext to read "my file.log" as Unicode text

ahah this is the result when i read as unicode :stuck_out_tongue:

I think the text is utf-8

i tried to open with this :

as «class utf8»

The text seems correct but it still doesnt work with my integers

It looks from the hex dump as if the text is little-endian UTF-16. ‘read . as Unicode text’ assumes big-endian unless it starts reading at a little-endian BOM.

Edit: For instance, does this produce a readable result?

Read the log data as data.
set utfData to (read "my file.log" as data) -- Use your actual log file path here

-- Create an experimental file on the desktop.
set fRef to (open for access file ((path to desktop as text) & "my file copy.log") with write permission)
try
	set eof fRef to 0
	write «data rdatFFFE» to fRef -- Write a little-endian UTF-16 BOM to the experimental file.
	write utfData to fRef -- then the previously obtained log data.
	set bigtext to (read fRef from 1 as Unicode text) -- Read the whole lot back together.
on error msg
	display dialog msg buttons {"OK"}
end try
close access fRef

bigtext