Scripts only run from editor need interface

Script only runs from editor

I have only one long evenings experience with applescripts so please forgive this long post.

What I am doing is editing out commercials from shows in QT 7. I have an Excel sheet with the in and out points for each commercial. (kmttg_v0p8k takes the TiVo file converts it to .mp4 and gives in and out points for commercials, I take those and convert them in Excel to seconds that QT understands. KMTTG has a commercial removal option but it is not that accurate which is why I need to adjust the in and out points)

The workflow is:
click on cell, select copy; run script (which takes the position and sets the play head and cut in point); visually check desiredPosition and adjust; repeat process with identical script except it sets the cut out point; cut the commercial using QT .

The scripts only run from the editor. I tired running them from the finder menu bar and as a macro in Execl with buttons on my spreadsheet. No luck. I have to have two editors open and press the run buttons at the appropriate time.

I would like to have the scripts run from a 2 button interface “IN” “OUT” that floats over the spreadsheet but since I can only run them from the editor and I don’t know how to make a 2 button floating dialog box nor the code to capture the result i’m stuck.

I would appreciate any help you could give me.



-- user copies contents of Excel cell then runs this script

--this section copies clipboard and sets desiredPosition (I copied the code from the net)


tell application "System Events"
	set cur_app to first process whose frontmost is true
	set x to (name of cur_app is "System Events")
	keystroke tab using command down
	repeat while (cur_app is frontmost)
		delay 0.2
	end repeat
	if x then
		set cur_app to first process whose frontmost is true
		keystroke tab using command down
		repeat while (cur_app is frontmost)
			delay 0.2
		end repeat
	end if
	keystroke "c" using command down
	delay 0.2
end tell


set desiredPosition to the clipboard -- this I wrote


--this section starts QT and sets play head to desiredPosition -- wrote this too from examples on net

tell application "QuickTime Player 7"
	activate
	tell application "QuickTime Player 7"
		set current time of document 1 to desiredPosition
	end tell
end tell

-- this section sets the cut in marker to desiredPosition

tell application "System Events"
	keystroke "i" -- "Cut out" script is the same except key stroke is "O"
end tell

Model: Mac Pro v1.1
AppleScript: 2.3 (118) 2.1.2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.6)

Hi,

the power of AppleScript is to exchange and process data between multiple applications.

This might be a point to start with:
The script assumes that Excel and QuickTime Player are launched and the appropriate files are opened.
Further it assumes that the cells A1 and B1 of the active Excel sheet contain the in and out information.

The script gets the in/out information and inserts them in the active QT document (the clipboard is not needed at all).
Then a dialog is displayed with the file name, in and out position.
Pressing the button “Cut” cuts the file, “Cancel” aborts the script.
The QT file is cut but not saved.

Of course it’s possible to implement a repeat loop to process multiple in/out pairs


tell application "Microsoft Excel"
	set in_out to value of range "A1:B1" of active sheet
end tell
set {inValue, outValue} to item 1 of in_out

tell application "QuickTime Player 7"
	activate
	tell document 1
		set selection start to inValue
		set selection end to outValue
	end tell
	set messageText to "cut document " & quote & name of document 1 & quote & return & ¬
		return & "selection start: " & inValue & ¬
		return & "selection end: " & outValue
	display dialog messageText buttons {"Cancel", "Cut"} default button 2
	tell document 1
		cut
	end tell
end tell

Thanks StephanK

This exactly what I pictured my final project to be. Your code is so compact. Beautiful!

Now I need to automate it

The in out points are in a spread sheet and need to be decremented, last set done first etc. shortening the movie from the end so the remaining times are correct.

I would have to input a start and stop range and decrement it. Is there something like a FOR NEXT or DO UNTIL command i could use.

My long past BASIC programing knowledge tends to interfere with this idea of scripting:

i.e. tell"-" end tell" looks like a loop but is not:“set {inValue, outValue} to item 1 of in_out” sets two variables from what looks like one variable and “set in_out to value of range “A1:B1” of active sheet” appears to put two values in one variable. I need to change my thinking can you recommend a basic applescript book?

Model: Mac Book 2.16 Core Duo
AppleScript: 2.3 (118) 2.1.2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.6)

in Excel the property value of a range returns a matrix (nested list).
In AppleScript a list is represented by curly brackets {}

The range in the script specifies two cells, the return value is {{value of cell1, value of cell2}}.

A repeat loop could look like the following, it assumes that the Excel sheet contains only two columns of data,
first in/out pair is in A1:B1, second is in A2:B2 etc.


tell application "Microsoft Excel"
	set inOutList to value of used range of active sheet
end tell

repeat with aValue in inOutList
	set {inValue, outValue} to aValue
	tell application "QuickTime Player 7"
		activate
		tell document 1
			set selection start to inValue
			set selection end to outValue
		end tell
		set messageText to "cut document " & quote & name of document 1 & quote & return & ¬
			return & "selection start: " & inValue & ¬
			return & "selection end: " & outValue
		display dialog messageText buttons {"Cancel", "Cut"} default button 2
		tell document 1
			cut
		end tell
	end tell
end repeat


a tell block is a reference “shortcut”


 tell document 1
           set selection start to inValue
           set selection end to outValue
 end tell

is the same as


 set selection start of document 1 to inValue
 set selection end of document 1 to outValue

there are some helpful AppleScript tutorials in the unScripted section

The problem I have is that the in and out points need to be adjusted before the cut, but with the script running I can’t move the points in QT, and if the script stops it no longer increments the list of ins and outs. Any thoughts on how to show in and out markers with the option to adjust them (transfer control to QT) and then return to the script?

That’s why I used the clipboard originally, to allow me to alter each in and out point and then cut. It would be better if both in and out markers were present but I can’t figure out how to do that. I can copy two cells to the clipboard but when I pass them to QT I only get the first 2 digits of the first cell.

here is the script and the result

tell application "System Events"
	set cur_app to first process whose frontmost is true
	set x to (name of cur_app is "System Events")
	keystroke tab using command down
	repeat while (cur_app is frontmost)
		delay 0.2
	end repeat
	if x then
		set cur_app to first process whose frontmost is true
		keystroke tab using command down
		repeat while (cur_app is frontmost)
			delay 0.2
		end repeat
	end if
	keystroke "c" using command down
	delay 0.2
end tell

set {inValue, outValue} to the clipboard

tell application "QuickTime Player 7"
	activate
	
	tell document 1
		
		set selection start to inValue
		set selection end to outValue
	end tell
end tell
/applescript]

Here is the result


tell application “System Events”
get process 1 whose frontmost = true
→ application process “AppleScript Editor”
get name of application process “AppleScript Editor”
→ “AppleScript Editor”
keystroke " " using command down
get frontmost of application process “AppleScript Editor”
→ true
get frontmost of application process “AppleScript Editor”
→ false
keystroke “c” using command down
end tell
tell application “AppleScript Editor”
the clipboard given «class Krtn»:{“inValue”, “outValue”}
→ “370146 540518”
end tell
tell application “QuickTime Player 7”
activate
set selection start of document 1 to “3”
set selection end of document 1 to “7”
end tell



Is there a way to pass both variables to QT and just end the script so I can make adjustments to the in and out points with my jog shuttle, cut and select the next set of points in Excel?

I hate to be a pest but I have a backlog of programs to edit and the scripts make it so much easier, even when I have to run both an in and an out script as in my original code.

I think I may have done it. This code uses in and out data that I highlight in Excel sets the in and out points and allows me to adjust them before the cut, then I have to highlight another set of in-out points and run the script again. I could use some advise polishing the code specifically I need to know how to move the play head to the in and out markers using applescript. “set selection start” moves the markers but not the playhead itself. I’ve found no help in the dictionary any ideas?

It would also be helpful to automate the Excel part but I can’t figure out how to decrement through the in-out array, leave the script to adjust the points and then start it against. But here’s the code that works with me highlighting the in out array going from high to low. I have to admit there is some code in there I don’t understand but it works.



tell application "Microsoft Excel"
	set DataSelection to the selection
	set myImportData to {}
	set RowCount to the count of rows of DataSelection
	
	set x to 1
	
	tell row x of DataSelection
		set inValue to the value of cell 1
		set outValue to the value of cell 2
	end tell
end tell


tell application "QuickTime Player 7"
	activate
	tell document 1
		set selection start to inValue
		set selection end to outValue
	end tell
	set messageText to "cut document " & quote & name of document 1 & quote & return & ¬
		return & "selection start: " & inValue & ¬
		return & "selection end: " & outValue
	--display dialog messageText buttons {"Adjust", "Cut"} default button 2
	
	set {button returned:buttonReturned} to display dialog "Select " buttons {"Adjust", "Cut"}
	
	if buttonReturned is "Cut" then
		--display dialog "Cut"
		tell document 1
			cut
		end tell
		error number -128
	else
		--display dialog "Adjust"
		error number -128
	end if
	
	--tell document 1
	--play
	--stop
	--end tell
	tell document 1
		cut
	end tell
end tell


The GUI of QT Player 7 is locked, because the dialog box is displayed modally.
This removes the focus from QT Player, so the window is editable.

The play head can be moved with the property current time.


tell application "Microsoft Excel"
	set inOutList to value of used range of active sheet
end tell

repeat with i from (count inOutList) to 1 by -1
	set {inValue, outValue} to item i of inOutList
	tell application "QuickTime Player 7"
		activate
		tell document 1
			set selection start to inValue
			set selection end to outValue
			set current time to inValue
		end tell
		set messageText to "cut document " & quote & name of document 1 & quote & return & ¬
			return & "selection start: " & inValue & ¬
			return & "selection end: " & outValue
		my displayMessage(messageText)
		tell document 1
			cut
		end tell
	end tell
end repeat

on displayMessage(msg)
	activate me
	display dialog msg buttons {"Cancel", "Cut"} default button 2
	activate application "QuickTime Player 7"
end displayMessage




To StefanK and everyone:

Its Thanksgiving Day here in the USA, a holiday where we give thanks for all things. I am giving thanks to StefanK and this entire site! You have sparked an intellectual joy for this retired old man (66 really is the new 40) So thanks to everyone here. Now I have to find a college course that teaches applescript. Anyone know of one near Cleveland, Ohio?

BTW I have deconstructed StefanK’s code, is it correct? The code works wonderfully and I plan to add a few tweaks as I learn more, for example: can you position the dialog box on the screen? Can a second subroutine be added called displayMessage2(msg) ? I want to add a dialog box when “i” get to 1 that allows you to quit or waits for the next document in QT.

What the heck I may even try to write a script that opens the file with the in-out points, converts them to QT numbers and runs the script and maybe even loads the next QT file! I’ll be busy for at least a month.

Thanks to all:

Here’s the annotated script, am I correct?



--finds the end of the in-out array 
tell application "Microsoft Excel"
	set inOutList to value of used range of active sheet
end tell

-- the loop that decrements array
--"i" is a pointer to array

repeat with i from (count inOutList) to 1 by -1
	set {inValue, outValue} to item i of inOutList -- puts in-out values to variables
	tell application "QuickTime Player 7"
		activate        --allow QT to be run by script
		tell document 1 --the active file in QT
			set selection start to inValue
			set selection end to outValue --set in out points
			set current time to inValue --show in point
			delay 2 --wait show out point
			set current time to outValue
			
		end tell
		
		--branch point	
		--make display for dialog box	

		set messageText to "cut " & quote & name of document 1 & quote & return & ¬
			return & " start: " & inValue & ¬
			return & "end: " & outValue
		my displayMessage(messageText) --call subroutine displayMessage
		
		--(msg) sets buttons and response
		-- if default do tell-cut-end tell
		--if not skip next 3 lines go to next item in in-out list

		tell document 1
			cut
		end tell
	end tell
end repeat


--subroutine when called activate and pass processing power to QT

on displayMessage(msg)
	activate me
	display dialog msg buttons {"Quit Script", "Cut"} default button 2
	activate application "QuickTime Player 7"
end displayMessage

/applescript]