Hi all. New to AppleScript, so I’d really appreciate some help. I have the script below to help me copy precise chunks from QuickTime files, by specifying times in a dialog box.
--copies a specified amount of a saved audio/video file using using 10.6.4 and QuickTime Player Pro 7.6.3
--Use e.g. Spark to assign this to a keybinding to work from inside QuickTime.
--a hack to get two variables from a single dialog box
set usertimes to text returned of (display dialog "Enter 2 times (in seconds) separated by comma and space" default answer "1, 2")
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set tParts to text items of usertimes
set AppleScript's text item delimiters to tid
set {times1, times2} to tParts
set startTime to times1
set endTime to times2
--gets the name and duration of the current file
tell application id "com.apple.quicktimeplayer"
set selectedPath to path of front document as string
set dur to (duration of front document)
end tell
tell application "System Events"
set timescale to time scale of contents of movie file selectedPath
set dursecs to dur / timescale
end tell
--CODE FOR WARNING HERE?????
tell application id "com.apple.quicktimeplayer"
activate
open selectedPath
tell front document
select selectedPath at timescale * startTime to timescale * endTime
copy
end tell
end tell
To avoid (reduce) errors, I’d like it to prompt me when the startTime exceeds dursecs, and to stop the script going any further. My guess–and it is just a guess–is that some code to do this would go in where I have put a marker. But I don’t know what the code would be (an if statement of some kind?) I’ve been stuck on this for a while, so would really appreciate any suggestions. In fact, any other suggestions for improvements would be welcome, too: this is my first AppleScript.
Thanks in advance,
wrathkeg
Here’s how I would write it. I put in error checking as you requested. Take a look and let us know if you need help understanding it.
--copies a specified amount of a saved audio/video file using using 10.6.4 and QuickTime Player Pro 7.6.3
--Use e.g. Spark to assign this to a keybinding to work from inside QuickTime.
-- get the times, separate the times by a space character only
-- we validate that 1) there are only 2 values given, 2) that both values are numbers, and 3) that the second number is larger than the first number.
set dialogText to "Enter 2 times (in seconds) separated by a space."
set defaultAnswer to "1 2"
repeat
set usertimes to text returned of (display dialog dialogText default answer defaultAnswer)
set defaultAnswer to usertimes
set theWords to words of usertimes
if (count of theWords) is 2 then
try
set startTime to (item 1 of theWords) as number
set endTime to (item 2 of theWords) as number
if startTime is less than endTime then
exit repeat
else
set dialogText to "There was an error getting 2 times!" & return & return & "Please try again. Enter only 2 numbers, separate them by a space, and make sure the second number is larger than the first number."
end if
on error
set dialogText to "There was an error getting 2 times!" & return & return & "Please try again. Enter only 2 numbers and separate them by a space."
end try
else
set dialogText to "There was an error getting 2 times!" & return & return & "Please try again. Enter only 2 times separated by a space!"
end if
end repeat
-- select and copy from startime to endtime of the front movie in quicktime
try
tell application "QuickTime Player 7"
activate
set theMovie to front document
tell theMovie
set timescale to time scale
set dursecs to duration / timescale
if dursecs is less than endTime then error "The ending time you entered is larger than the length of the current movie!"
select at timescale * startTime to timescale * endTime
copy
end tell
end tell
on error theError number errorNumber
tell me
activate
display dialog "There was an error: " & (errorNumber as text) & return & return & theError buttons {"OK"} default button 1 with icon stop
end tell
end try
Hank: that’s fantastic. Really, thanks very much. It seems to do everything I wanted when I began writing this script. While I couldn’t have written it so well as you, I think I follow it. And working through it has taught me a lot, too (I didn’t know about repeat loops for one thing).
Best wishes,
wrathkeg