AppleScript C Perl Shell Xcode Other

Scripting iMovie compiling identical movies

Post Reply
coding / applescript     Views: 528Prev .. Next
Scripting iMovie compiling identical moviesPosted: Wednesday, October 26, 2011 [17:05:57] - 1
rootPosted by:rootMember Since:
June 16 2010
Posts: 356
Making identical movies, say for YouTube or for clients with pre-set length of a source movie and the same titles it makes sense to automate the process.
iMovie is not "Applescriptable", so they say.
But by joining a few programming languages (AppleScript, Python, Perl or PHP) this routine process could be automated.
Keep in-mind - program below rely on iMovie program position on the screen!
To get exact coordinates of the movie parts for Python to click we used XYLocator: http://mac.softpedia.com/get/Utilities/XYLocator.shtml



View Codetell application "iMovie" to activate
delay 2
-- folder where files of a source movies located - it could be any folder
set p to "/Users/User/Movies/"
set the_folder to POSIX file p
-- folder where the movie title text file is
set ReDir to "/Users/User/Desktop/VIdeo/"

set the_folder_list to list folder the_folder without invisibles

repeat with x from 1 to count of the_folder_list
set the_file to the_folder & item x of the_folder_list as string
set FileName to item x of the_folder_list as string
set goAhead to "no"
-- "making" in file name helps choose proper files
if FileName does not contain ".making.mov" then
set goAhead to "no"
else
set fileSave to FileName
set fileSaveto to do shell script "echo " & quoted form of fileSave & "|perl -n -e '$_=~ s/\\.making\\.mov//;print $_;'"

set FileName1 to do shell script "echo " & quoted form of FileName & "|perl -n -e '$_=~ s/\\.making\\.mov//;print $_;'"
set FileName1 to ReDir & FileName1 & ".text.txt"
set textFile to do shell script "echo " & quoted form of FileName1 & "|perl -n -e 'chomp $_;if(-f $_){print $_;}else{print \"badFile\";}'"
if textFile does not contain "badFile" then
-- Movie Title saved in "textFile" which is Perl file in variable $hdtext - so we used require
set manHeader to do shell script "echo " & quoted form of textFile & "|perl -n -e 'chomp $_;eval{require $_};print $hdtext;'"
set goAhead to "yes"
end if
-- skip if completed movie file already present
set testExist to "/Users/User/Desktop/uploads/" & fileSaveto & ".mov"
set doNotDo to do shell script "echo " & quoted form of testExist & "|perl -n -e 'chomp $_;if(-f $_){print \"notto\";}else{print \"goahead\";}'"
if doNotDo contains "notto" then
set goAhead to "no"
end if
end if

--exit repeat
if goAhead is "yes" then
-- remove finished old movie in iMovie and replace it with new one
-- coordinates should be updated
set Pcode to getPython("466, 212")
do shell script "python -c " & quoted form of Pcode
delay 1
tell application "System Events"
tell process "iMovie"
key code 51
delay 1
keystroke return
delay 1
end tell
end tell

-- remove finished old cross dissolve
set Pcode to getPython("359, 209")
do shell script "python -c " & quoted form of Pcode
delay 1
tell application "System Events"
tell process "iMovie"
key code 51
end tell
end tell
delay 1
-- remove finished source movie
set Pcode to getPython("568, 628")
do shell script "python -c " & quoted form of Pcode
delay 1

tell application "System Events"
tell process "iMovie"
keystroke "a" using {command down}
delay 0.5
key code 51
delay 1

-- open movie file
keystroke "o" using {command down, shift down}
delay 1
-- switch to the source folder
keystroke "g" using {command down, shift down}
delay 1
keystroke "/Users/User/Movies"
delay 0.5
keystroke return
delay 1
keystroke FileName
delay 1
keystroke return
delay 19
end tell
end tell

-- select source movie click on coordinates
set Pcode to getPython("568, 628")
do shell script "python -c " & quoted form of Pcode
delay 1

tell application "System Events"
tell process "iMovie"
keystroke "a" using {command down}
delay 0.5
end tell
end tell

-- Move new movie to a timeline
set Pcode to mvPython("568, 628", "350, 209")
do shell script "python -c " & quoted form of Pcode
delay 1

--select transitions
set Pcode to getPython("1540, 537")
do shell script "python -c " & quoted form of Pcode
delay 1

-- insert front cross dissolve
set Pcode to mvPython("1195, 750", "350, 209")
do shell script "python -c " & quoted form of Pcode
delay 1

-- insert end cross dissolve
set Pcode to mvPython("1195, 750", "221, 398")
do shell script "python -c " & quoted form of Pcode
delay 1

--select titles
set Pcode to getPython("1513, 539")
do shell script "python -c " & quoted form of Pcode
delay 1

-- move title "Your Movie Title"
set Pcode to mvPython("1414, 650", "566, 212")
do shell script "python -c " & quoted form of Pcode
delay 1

tell application "System Events"
tell process "iMovie"
-- type your "Movie Title"
keystroke "Your Movie Title here"
end tell
end tell
delay 1

-- trim "Movie title" beginning
set Pcode to mvPython("377, 163", "485, 163")
do shell script "python -c " & quoted form of Pcode
delay 1

-- trim "Movie title" end
set Pcode to mvPython("215, 376", "675, 163")
do shell script "python -c " & quoted form of Pcode
delay 1

--select Manual Name
set Pcode to getPython("210, 163")
do shell script "python -c " & quoted form of Pcode
delay 1

--select Movie Name text
set Pcode to getPython("1410, 210")
do shell script "python -c " & quoted form of Pcode
delay 1


tell application "System Events"
tell process "iMovie"
keystroke manHeader
delay 2
keystroke "e" using {command down}
delay 1
-- Save To Folder
keystroke "/Users/User/Desktop/uploads"
delay 1
keystroke return
delay 1
keystroke fileSaveto
delay 1
keystroke return
delay 15
end tell
end tell
delay 45
end if -- end good go ahead
--exit repeat
end repeat

-- Python sources
on mvPython(cfrom, cTo)
set Pcode to "import sys
import time
from Quartz.CoreGraphics import *# imports all of the top-level symbols in the module

def mouseEvent(type, posx, posy):
theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
mouseEvent(kCGEventMouseMoved, posx,posy);
def mouseclickdn(posx,posy):
mouseEvent(kCGEventLeftMouseDown, posx,posy);
def mouseclickup(posx,posy):
mouseEvent(kCGEventLeftMouseUp, posx,posy);
def mousedrag(posx,posy):
mouseEvent(kCGEventLeftMouseDragged, posx,posy);

ourEvent = CGEventCreate(None);
currentpos=CGEventGetLocation(ourEvent);# Save current mouse position
mouseclickdn(" & cfrom & " );
time.sleep(1);
mousedrag(" & cTo & ");
time.sleep(1);
mouseclickup(" & cTo & ");
"
return Pcode
end mvPython

on getPython(coord)
set PyThon to "import sys
import time
from Quartz.CoreGraphics import *

def mouseEvent(type, posx, posy):
theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
mouseEvent(kCGEventMouseMoved, posx,posy);
def mouseclickdn(posx,posy):
mouseEvent(kCGEventLeftMouseDown, posx,posy);
def mouseclickup(posx,posy):
mouseEvent(kCGEventLeftMouseUp, posx,posy);
def mousedrag(posx,posy):
mouseEvent(kCGEventLeftMouseDragged, posx,posy);

ourEvent = CGEventCreate(None);
currentpos=CGEventGetLocation(ourEvent);
time.sleep(1);
mouseclickdn(" & coord & ");
mouseclickup(" & coord & ");
"
return Python
end getPython


To create movies - we used Screen recording of a PDF files with the following AppleScript (this script creates source movies):
View Codeset p to "/Users/User/Desktop/VIdeo/"
set the_folder to POSIX file p
set rf to "/Users/User/Movies/"
set save_folder to POSIX file rf
set DoneManuals to ""
tell application "Terminal" to activate
delay 3
tell application "Adobe Acrobat Pro" to activate
delay 3
--set the_folder to f
set the_folder_list to list folder the_folder without invisibles

repeat with x from 1 to count of the_folder_list
set the_file to the_folder & item x of the_folder_list as string
set FileName to item x of the_folder_list as string
--display dialog "File: " & the_file
--set filePresent to do shell script "ls -la /Users/User/Movies/" & FileName & ".mov" as string
tell application "Finder"
set fileVer to save_folder & FileName & ".mov" as text
--if filePresent contains "No such file" then
if exists file fileVer then
set goAhead to "no"
else
set goAhead to "yes"
end if
end tell

if FileName does not contain ".pdf" then
set goAhead to "no"
end if

if goAhead is "yes" then
set PDFFile to POSIX path of the_file
-- get a number of pages in PDF file
set NumbOfPages to last word of (do shell script "mdls " & PDFFile & " | grep kMDItemNumberOfPages")
-- we needed to have one less page for our program
set NumbOfPages to NumbOfPages - 1

doOpenDocument(the_file)
set pagesList to numberRnd(NumbOfPages)
delay 4

-- PROCESS PDF FILE --

tell application "QuickTime Player" to activate
delay 2
tell application "System Events"
tell process "QuickTime Player"
keystroke "n" using {command down, control down}
delay 1
keystroke space
delay 1
end tell
end tell
do shell script "python /Users/User/Movies/select.py"
delay 1
tell application "Adobe Acrobat Pro" to activate
delay 0.5
tell application "System Events"
tell process "Adobe Acrobat Pro"
delay 10

repeat with m from 1 to count of pagesList
keystroke "n" using {command down, shift down}
delay 0.5
set toPage to item m of pagesList as string
keystroke toPage
delay 0.5
keystroke return
delay 8
end repeat
key code 53
delay 1
end tell
end tell
tell application "QuickTime Player" to activate
delay 1
tell application "System Events"
tell process "QuickTime Player"
key code 53
delay 1
keystroke space
end tell
end tell
delay 10

tell application "QuickTime Player" to quit
delay 1

do shell script "mv /Users/User/Movies/Screen\\ Recording.mov /Users/User/Movies/" & FileName & ".mov"
delay 1
-- PROCESS PDF FILE --

tell application "System Events"
tell process "Adobe Acrobat Pro"
keystroke "w" using {command down}
delay 0.5
end tell
end tell
set DoneManuals to DoneManuals + 1
--if DoneManuals > 2 then
--exit repeat
--end if
end if
end repeat

-- CUTTING FILES TO 48 SECONDS
delay 3
set p to "/Users/User/Movies/"
set the_folder to POSIX file p

set the_folder_list to list folder the_folder without invisibles
set NmList to ""
set NofDone to ""
repeat with x from 1 to count of the_folder_list
set the_file to the_folder & item x of the_folder_list as string
set FileName to item x of the_folder_list as string
set doRide to true
if FileName contains ".making" then
set doRide to false
end if
if doRide is true then
if FileName contains ".mov" then
if NofDone < 2 then
set FileName1 to do shell script "echo " & quoted form of FileName & "|perl -n -e '$_=~ s/\\.pdf/\\.making/;print $_;'"
set FromFile to p & FileName as string
set ToFile to p & FileName1 as string
set checkS to do shell script "echo " & quoted form of ToFile & "|perl -n -e 'chomp $_;if(-f $_){print \"bad\";}else{print\"good\";}'"
if checkS is equal to "good" then
--set NofDone to NofDone + 1
tell application "Terminal" to activate
delay 2
tell application "System Events"
tell process "Terminal"
keystroke "splitmovie " & FromFile & " -splitAt 0:57 -self-contained -o " & ToFile
delay 2
keystroke return
delay 5
end tell
end tell
set shone to do shell script "echo " & quoted form of ToFile & "|perl -n -e '$k=$_;$_=~ s/\\.mov/\\-1$&/;chomp $_;print \"$_ $k\";'"
do shell script "mv " & shone
set shone to do shell script "echo " & quoted form of ToFile & "|perl -n -e '$_=~ s/\\.mov/\\-2$&/;chomp $_;print $_;'"
do shell script "rm -rf " & shone
end if -- end if GOOD
end if
end if -- end if .MOV file
end if -- end if true DoRide
end repeat
-- CUTTING FILES TO 48 SECONDS
--tell application "Adobe Acrobat Pro" to quit

on numberRnd(maxNmb)
set rdPages to {}
set rCount to ""
repeat with i from 1 to 25
set rn to (random number (maxNmb)) as text
set toGood to true
repeat with vf from 1 to count of rdPages
set toVer to item vf of rdPages as string
if toVer is equal to rn then
set toGood to false
end if
end repeat
if toGood is true then
if rn > 15 then
set end of rdPages to rn
set rCount to rCount + 1
end if
end if
if rCount > 6 then
exit repeat
end if
end repeat
return rdPages
end numberRnd

--display dialog "Files: " & filesFound
on doOpenDocument(theDocument)
tell application "Adobe Acrobat Pro"
open (theDocument as alias)
delay 2
if not (exists first document) then
display dialog "There is no document opened" buttons {"Cancel"} default button 1
else
tell application "System Events"
tell process "Adobe Acrobat Pro"
keystroke "l" using {command down}
end tell
end tell
end if
end tell
end doOpenDocument


Now stand-alone Python file "select.py" used to select the portion of the script to record:
View Code#!/usr/bin/python
import sys
import time
from Quartz.CoreGraphics import *# imports all of the top-level symbols in the module

def mouseEvent(type, posx, posy):
theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
mouseEvent(kCGEventMouseMoved, posx,posy);
def mouseclickdn(posx,posy):
mouseEvent(kCGEventLeftMouseDown, posx,posy);
def mouseclickup(posx,posy):
mouseEvent(kCGEventLeftMouseUp, posx,posy);
def mousedrag(posx,posy):
mouseEvent(kCGEventLeftMouseDragged, posx,posy);

ourEvent = CGEventCreate(None);
currentpos=CGEventGetLocation(ourEvent);# Save current mouse position
mouseclickdn(3, 22 );

mousedrag(1677, 1030);
mouseclickup(1677, 1030);
time.sleep(2);
mouseclickdn(840, 530);
mouseclickup(840, 530);


Just as simple as that!There's no place like ~
RE: Scripting iMovie compiling identical moviesPosted: Wednesday, October 26, 2011 [17:35:50] - 2
rootPosted by:rootMember Since:
June 16 2010
Posts: 356
I forgot - one more program required on your Mac to trim the source movies - "QTCoffee" from http://www.3am.pair.com/QTCoffee.html
It's a donation-ware - so do not forget to pay for it. Very easy to work with and they did a great job of simplifying QuickTime movies manipulation.
QTCoffee works great under Mac OS X Lion.There's no place like ~
coding / applescriptPrev .. Next
 
Post Reply
Home - Coding: AppleScript C Perl Shell Xcode Other
Our Telegram Group