Urgent help needed handling current date!

I’m not at all experienced in scripting, but after Leopard broke a favourite app of mine I attempted to build a replacement for it out with AppleScript. Its function is to make a video and allow me to take notes at the same time. Each note is inserted into the video as a chapter. I use it in theatre rehearsals so that I can take notes on complex dance routines without having to worry about which moment of the routine the note refers to. All I do is play the video back, go to each chapter heading and do my note. I know that the final stages of the script are very crude, and I have to do some things manually, as I couldn’t get the script to do them for me, but as I said, I’m not a scripter, and as soon as I got the script to work, I went back to my day job!

Now Snow Leopard has broken my script, and I have a technical rehearsal starting on Saturday! I know that date and time handling have changed, but I can’t for the life of me see how to fix it. Can anyone help with this? It only works with QT pro, btw. I hope I’ve posted this correctly, but please forgive me if I’ve got the posting wrong, it’s not clear to me how to work the applescript button.

Many thanks.

Ben


set listofnotes to {}

tell application “QuickTime Player 7”
activate
new movie recording
start first recording
end tell

set thought to “note”

set startTime to time of (current date)

set the_file_path to “June 10:Users:benormerod:movies: show movies:listofnotes.txt”
open for access file the_file_path with write permission
set eof file the_file_path to 0
write “{QTtext}{font:Geneva}{plain}{size:12}{textColor: 65535, 65535, 65535}{backColor: 0, 0, 0}{justify:center}{timeScale:1000}{width:160}{height:48}{timeStamps:absolute}{language:0}{textEncoding:0}” & return to file the_file_path
close access file the_file_path

repeat
set the_button to button returned of (display dialog “click to note” buttons {“stop”, “note”} default button 2)
if the_button is “stop” then exit repeat

set EndTime to time of (current date)
display dialog "enter your note" default answer thought
set thought1 to text returned of the result
set duration to EndTime - startTime - 1

--reformat duration

set x to date ("0" as string)
set hours of x to duration div hours
set minutes of x to duration mod hours div minutes
set seconds of x to duration mod hours mod minutes



set currentnote to "[" & time string of x & ".000" & "]" & return & thought1 & return
open for access file the_file_path with write permission
write currentnote to file the_file_path starting at eof
close access file the_file_path

end repeat

set EndTime to time of (current date)
set duration to EndTime - startTime - 1

–reformat duration

set x to date (“0” as string)
set hours of x to duration div hours
set minutes of x to duration mod hours div minutes
set seconds of x to duration mod hours mod minutes

set currentnote to “[” & time string of x & “.000” & “]” & return & “end” & return

open for access file the_file_path with write permission
write currentnote to file the_file_path starting at eof
close access file the_file_path

tell application “QuickTime Player 7”
stop first recording

activate
open the_file_path

select all

tell application "System Events" to keystroke "c" using command down



tell application "System Events" to keystroke "w" using command down



tell application "System Events" to keystroke "v" using {command down, option down}

tell application "System Events" to keystroke "j" using command down

end tell


then I have to manually click a a few buttons to associate the chapter text with the video. One day I’d like to fix that too, but at the moment the really urgent thing is this…



tell application “QuickTime Player 7”
activate
new movie recording current application
start recording 1
end tell
tell current application
current date
→ date “Wednesday, 16 March 2011 11:10:41”
open for access file “June 10:Users:benormerod:movies: show movies:listofnotes.txt” with write permission
→ 224
set eof file “June 10:Users:benormerod:movies: show movies:listofnotes.txt” to 0
write "{QTtext}{font:Geneva}{plain}{size:12}{textColor: 65535, 65535, 65535}{backColor: 0, 0, 0}{justify:center}{timeScale:1000}{width:160}{height:48}{timeStamps:absolute}{language:0}{textEncoding:0}
" to file “June 10:Users:benormerod:movies: show movies:listofnotes.txt”
close access file “June 10:Users:benormerod:movies: show movies:listofnotes.txt”
end tell
tell application “AppleScript Editor”
display dialog “click to note” buttons {“stop”, “note”} default button 2
→ {button returned:“note”}
end tell
tell current application
current date
→ date “Wednesday, 16 March 2011 11:11:05”
end tell
tell application “AppleScript Editor”
display dialog “enter your note” default answer “note”
→ {text returned:“note”, button returned:“OK”}
Result:
error “Invalid date and time date 0 of «script».” number -30720

Model: MacBook Intel core 2 duo
AppleScript: 2.1.2
Browser: Firefox 3.6.15
Operating System: Mac OS X (10.6)

Hi, Ben.

Some of the more quirky date interpretations are no longer possible. The only way to get the same result now is:

set x to (current date)
set x's time to 0

But since you’re setting all aspects of the time in the following lines anyway, there’s actually no need to zero it first.
¨

The ‘time string’ of a date depends on how your computer’s set up to display the time. But in fact there’s no need to use a date at all. You can replace everything quoted above with:

tell (1000000 + duration div hours * 10000 + duration mod hours div minutes * 100 + duration mod minutes div 1) as text
	set currentnote to "[" & text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7 & ".000" & "]" & return & thought1 & return
end tell

Thank you so much, you’ve completely mended that for me, and saved my life in the progress! If you have time, could you point me in the right direction as to how to finish this script off? There seems to be very little support in QTpro for applescripts…

Hi, Ben.

I think this works. It needs a bit more tidying up, but I’ll look at it again this evening.

Edit: Now tidied up and an error of my own corrected. :rolleyes: At this stage, not having QuickTime Pro, I’m not sure if QT has an internal AppleScript command for showing the movie info. I’ve commented in a likely contender ” set show detailed movie info window to true ” but have left Ben’s GUI solution in place.

on main()
	-- Start recording.
	tell application "QuickTime Player 7"
		launch -- Open in the background with no default windows.
		new movie recording
		start first recording
	end tell
	
	set thought to "note"
	
	set startTime to (current date) -- Use the complete date/time in case you're working past midnight!
	
	-- Create/open for access a text file.
	set the_file_path to (path to movies folder as Unicode text) & " show movies:listofnotes.txt"
	set fRef to (open for access file the_file_path with write permission)
	try -- Use a try block so that the file can be closed again in the event of an error.
		-- Write headers to the file.
		set eof fRef to 0
		write "{QTtext}{font:Geneva}{plain}{size:12}{textColor: 65535, 65535, 65535}{backColor: 0, 0, 0}{justify:center}{timeScale:1000}{width:160}{height:48}{timeStamps:absolute}{language:0}{textEncoding:0}" & return to fRef as string
		
		-- Write thoughts as they occur until the user clicks "stop".
		repeat while (button returned of (display dialog "click to note" buttons {"stop", "note"} default button 2) is "note")			
			set EndTime to (current date)
			display dialog "enter your note" default answer thought
			set thought1 to text returned of the result
			set duration to EndTime - startTime - 1			
			storeNote(duration, thought1, fRef)
		end repeat
		
		-- Write the end note to the file and close the access.
		set EndTime to (current date)
		set duration to EndTime - startTime - 1		
		storeNote(duration, "end", fRef)
		close access fRef
		
		tell application "QuickTime Player 7"
			-- Stop the recording, bring QT to the front, open the text file in it.
			stop first recording
			
			activate
			open file the_file_path
			
			-- Copy the text to the clipboard, close the text player, add the clipboard contents to the recording.
			tell front document
				-- The front document here is the text player.
				select all
				copy
				close
				-- The front document is now the recording.
				add
				
				-- The following line MAY show the movie properties in QT Pro when uncommented .
				(* set show detailed movie info window to true *)
			end tell
		end tell
		
		-- . otherwise use GUI Scripting.
		tell application "System Events" to keystroke "j" using {command down}
	on error errMsg
		-- Tidy up in the event of an error.
		try
			tell application "QuickTime Player 7" to stop first recording
		end try
		try
			close access fRef
		end try
		tell application (path to frontmost application as Unicode text)
			display dialog errMsg buttons {"OK"} default button 1
		end tell
	end try
end main

on storeNote(duration, thought, fRef)
	tell (1000000 + duration div hours * 10000 + duration mod hours div minutes * 100 + duration mod minutes div 1) as text
		set currentnote to "[" & text 2 thru 3 & ":" & text 4 thru 5 & ":" & text 6 thru 7 & ".000" & "]" & return & thought & return
	end tell
	write currentnote to fRef as string starting at eof
end storeNote

main()

That is extremely kind of you. I hadn’t realised that I could use select all and copy in Quicktime, I don’t know why, it seems obvious now, of course! I’ve added

tell application “System Events” to keystroke “j” using command down

in order to let me reveal the chapter dropdown quickly so I can get to work sorting out my notes as soon as the rehearsal is finished.

Again, many thanks for cleaning this up for me, I’m learning a lot just by reading your script.

Ben

Now done (see script and edit note in post #4 above).