Automating encoding of videos (and creation of reference movie) for iOS - ios

My iPad app gives users the ability to watch a few hundred videos. Currently the iPad app points them to an MP4 file. However, I would like to point them to a QuickTime Reference Movie instead. (This allows the app to send a lower-bitrate version of the video if the user is connected via 3G vs. wifi.)
Right now, I create the various versions, plus the reference file, in QuickTime by going to File -> Export to Web. However, this process (1) only lets me do one file at a time, and (2) generates lots of useless stuff like HTML and JavaScript.
How can I automate the process? Does anyone know of existing scripts / tools to do this work? I'm sure other devs have had to do it before.

I wrote (and cobbled together from other stuff on the web) this AppleScript to do the job:
--property exportFolder : (path to documents folder as Unicode text) & "Your Folder:"
property QTExportPath : (path to movies folder from user domain)
property completedFolderName : "completion" -- this folder must already exist
property referenceFiles : {}
property encodeCount : 0
on run
choose folder with prompt "Choose folder with video files:" -- with multiple selections allowed
open (result)
end run
on open droppedItems
tell application "Finder"
set allFiles to every file of entire contents of droppedItems as alias list
end tell
-- Error checking
repeat with testFile in allFiles
set myChars to characters of basename(testFile)
if count_matches(myChars, ".") > 1 then
display dialog "ERROR: The file " & basename(testFile) & " has too many periods."
return
end if
end repeat
-- Made it past error checking, let's get started
tell application "QuickTime Player"
activate
close every window
end tell
repeat with thisItem in allFiles
tell application "QuickTime Player"
close every window
open thisItem as alias
end tell
activate application "QuickTime Player"
tell application "System Events"
tell process "QuickTime Player"
keystroke "E" using command down
repeat until exists sheet 1 of window 1
delay 0.2
end repeat
click button "Export" of sheet 1 of window 1
end tell
end tell
tell application "Finder"
set fileExt to name extension of thisItem
end tell
set fileBaseName to basename(thisItem)
set lengthWithoutExtension to (count of fileBaseName) - (count of fileExt) - 1
set fileBaseNameWithoutExtension to text 1 thru lengthWithoutExtension of fileBaseName
set end of referenceFiles to fileBaseNameWithoutExtension
repeat until startedExporting(fileBaseNameWithoutExtension)
-- wait for this file to start exporting before beginning another
end repeat
end repeat
repeat until doneExporting(referenceFiles)
-- wait for exporting to finish
end repeat
cleanUpGarbage(referenceFiles)
tell application "QuickTime Player"
activate
close every window
quit
end tell
display alert "finished exporting"
end open
on basename(thePath) -- Returns basename of alias
set thePOSIXPath to the POSIX path of thePath
if thePOSIXPath ends with "/" then
set nameIndex to -2
else
set nameIndex to -1
end if
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set thePOSIXPath to text item nameIndex of thePOSIXPath
set AppleScript's text item delimiters to ASTID
return thePOSIXPath
end basename
on startedExporting(fileBaseNameWithoutExtension) -- Checks if QuickTime began exporting a file
set moviesPath to QTExportPath as text
set fileToTest to ((the POSIX path of moviesPath) & fileBaseNameWithoutExtension & "/Resources/" & fileBaseNameWithoutExtension & ".mov")
if FileExists(fileToTest) then
-- do nothing
else
return false
end if
end startedExporting
on doneExporting(referenceFiles) -- Checks if QuickTime is done exporting everything
set moviesPath to QTExportPath as text
repeat with thisItem in referenceFiles
set fileToTest to ((the POSIX path of moviesPath) & thisItem & "/Resources/" & thisItem & ".mov")
if FileExists(fileToTest) then
-- do nothing
else
return false
end if
end repeat
end doneExporting
on FileExists(theFile) -- (String) as Boolean
tell application "System Events"
if exists file theFile then
return true
else
if exists folder theFile then
return true
else
return false
end if
end if
end tell
end FileExists
on cleanUpGarbage(referenceFiles)
set moviesPath to QTExportPath as text
set donePath to (QTExportPath as text) & completedFolderName as text
set POSIXMovies to the POSIX path of moviesPath
set POSIXDone to the POSIX path of donePath
repeat with thisItem in referenceFiles
set directoryToClean to (POSIXMovies & thisItem & "/")
set m4vcommand to "find '" & directoryToClean & "' -type f -iname '*m4v' -exec cp -n {} '" & POSIXDone & "' \\;"
set movcommand to "find '" & directoryToClean & "' -type f -iname '*mov' -exec cp -n {} '" & POSIXDone & "' \\;"
do shell script m4vcommand
do shell script movcommand
set thefolder to POSIX file directoryToClean
tell application "Finder"
delete thefolder
end tell
end repeat
end cleanUpGarbage
on count_matches(this_list, this_item)
set the match_counter to 0
repeat with i from 1 to the count of this_list
if item i of this_list is this_item then ¬
set the match_counter to the match_counter + 1
end repeat
return the match_counter
end count_matches
It assumes:
the folder you're starting from has ONLY movie files, AND you want to convert ALL of them.
you have already set up whatever export settings you want in QuickTime by doing one manual export to web
It will export them one at a time. If you want to convert multiple simultaneously, modify the startedExporting method to only check for a folder instead of the .mov file (the .mov file is the last one to generate.)

Related

How to run an Applescript that will click and open all links on a web page?

I'm trying to write a script that will automatically open a webpage http://www.legislation.gov.uk/new/uksi and then click on all the links in the table "All New Legislation".
So far I've managed to get it to open the page but no luck with clicking.
Here's my script so far:
activate application "Safari"
open location "http://www.legislation.gov.uk/new/uksi"
to clickID()
do JavaScript "document.getElementById(id=per).click();" in document 1
end tell
The following example AppleScript code will open the targetURL in a new Safari window, wait for the page to finish loading, retrieve all URLs on the target page, search them for URLs pointing the various Statutory Instruments published today, and then open each one in a new tab of the same window the targetURL was opened.
set targetURL to "http://www.legislation.gov.uk/new/uksi"
set theseURLs to {}
set grepSearchPattern to ".*\\.uk/uksi/.*\\|.*\\.uk/ssi/.*\\|.*\\.uk/wsi/.*\\|.*\\.uk/nisi/.*"
set jsStatements to "var a = document.links; var x = ''; var i; for (i = 0; i < a.length; i++) { x = x + a[i].href + '|'; };"
tell application "Safari"
make new document with properties {URL:targetURL}
activate
end tell
tell application "System Events"
repeat until exists ¬
(buttons of UI elements of groups of toolbar 1 of window 1 of ¬
process "Safari" whose name = "Reload this page")
delay 1
end repeat
end tell
tell application "Safari"
set allURLs to (do JavaScript jsStatements in document 1)
end tell
try
set theseURLs to paragraphs of (do shell script "tr '|' '\\12' <<< " & ¬
allURLs's quoted form & " | grep " & grepSearchPattern's quoted form)
end try
if (length of theseURLs) is greater than 0 then
tell application "Safari" to tell front window
repeat with thisURL in theseURLs
set current tab to (make new tab with properties {URL:thisURL})
end repeat
set current tab to first tab
end tell
else
display dialog " Nothing published on this date." buttons {"OK"} ¬
default button 1 with title "All New Legislation" with icon note
end if
Hint: Mouse over and horizontal/vertical scroll to see full code.
Notes:
The do JavaScript1 command create a pipe delimited string of all URLs on the page of the targetURL.
The do shell script command takes the pipe delimited string of all URLs and replaces the pipe characters with newline characters, using tr, so grep can return the URLs that match the grepSearchPattern.
The grepSearchPattern variable currently only searches for Statutory Instruments, as I assume that is all that will show under All New Legislation on the page the targetURL opens to, because of /new/uksi in the targetURL, and what I've observed at that URL since you posted the question. If you also want links for other types of legislation, the grepSearchPattern variable can be adjusted to accommodate.
1 Using the do JavaScript command requires Allow JavaScript from Apple Events to be checked on the Safari > Develop menu, which is hidden by default and can be shown by checking [√] Show Develop menu in menu bar in: Safari > Preferences… > AdvancedIf you are not allowed to enable that setting, the URLs can be collected for processing in another manner, however it uses the lynx third party utility.
Opening the links without the use of the do JavaScript command:
The following example AppleScript code will use lynx to retrieve the URLs from the targetURL, search them for URLs pointing the various Statutory Instruments published today, and if some have been published will open the targetURL in a new Safari window, wait for the page to finish loading, and then open each one in a new tab of the same window the targetURL was opened.
set targetURL to "http://www.legislation.gov.uk/new/uksi"
set theseURLs to {}
set lynxCommand to "/usr/local/bin/lynx --dump -listonly -nonumbers -hiddenlinks=ignore"
set grepSearchPattern to ".*\\.uk/uksi/.*\\|.*\\.uk/ssi/.*\\|.*\\.uk/wsi/.*\\|.*\\.uk/nisi/.*"
try
set theseURLs to paragraphs of ¬
(do shell script lynxCommand & space & targetURL's quoted form & ¬
" | grep " & grepSearchPattern's quoted form)
end try
if (length of theseURLs) is greater than 0 then
tell application "Safari"
make new document with properties {URL:targetURL}
activate
end tell
tell application "System Events"
repeat until exists ¬
(buttons of UI elements of groups of toolbar 1 of window 1 of ¬
process "Safari" whose name = "Reload this page")
delay 1
end repeat
end tell
tell application "Safari" to tell front window
repeat with thisURL in theseURLs
set current tab to (make new tab with properties {URL:thisURL})
end repeat
set current tab to first tab
end tell
else
display dialog " Nothing published on this date." buttons {"OK"} ¬
default button 1 with title "All New Legislation" with icon note
end if
Hint: Mouse over and horizontal/vertical scroll to see full code.
Notes:
In the lynxCommand variable, change /usr/local/bin/lynx to the appropriate /path/to/lynx. lynx can be installed using Homebrew
The grepSearchPattern variable currently only searches for Statutory Instruments, as I assume that is all that will show under All New Legislation on the page the targetURL opens to, because of /new/uksi in the targetURL, and what I've observed at that URL since you posted the question. If you also want links for other types of legislation, the grepSearchPattern variable can be adjusted to accommodate.
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.

How to make TextEdit links clickable

This script is supposed to open a series of web pages in a new browser window, and then open TextEdit with predetermined text and links.
Safari does what it is supposed to.
Text edit opens and pastes the text I want, but the links are not clickable.
I know I could just right click and choose Substitutions> Add Links> myself, but I am trying to automate the entire process.
I appreciate your time and efforts on my behalf! Thank you!
OpenWebPages()
OpenTextEditPage()
to OpenTextEditPage()
-- Create a variable for text
set docText to ""
tell application "TextEdit"
activate
make new document
-- Define the text to be pasted into TextEdit
set docText to docText & "Some text to show in TextEdit." & linefeed & "
My favorite site about coding is http://stackoverflow.com/
My favorite site for paper modeling is http://www.ss42.com/toys.html
My favorite site for inventing is http://www.instructables.com/howto/bubble+machine/
" & linefeed & "Click the links above to improve your mind!" as string
-- Past the above text and links into TextEdit
set the text of the front document to docText & "" as string
tell application "System Events"
tell process "TextEdit"
-- highlight all text
keystroke "a" using command down
-- Think of a clever way to right click and choose Substitutions> Add Links>
-- Or think of another clever way to turn all URLs into links please.
end tell
end tell
end tell
end OpenTextEditPage
to OpenWebPages()
-- Start new Safari window
tell application "Safari"
-- activate Safari and open the StackOverflow AppleScript page
make new document with properties {URL:"http://stackoverflow.com/search?q=applescript"}
-- Yoda is watching you
open location "http://www.ss42.com/pt/Yoda/YodaGallery/yoda-gallery.html"
-- Indoor boomerang
open location "http://www.ss42.com/pt/paperang/paperang.html"
-- Are you a Human ?
open location "http://stackoverflow.com/nocaptcha?s=f5c92674-b080-4cea-9ff2-4fdf1d6d19de"
end tell
end OpenWebPages
According to this question I built this little handler. It takes a path to your rtf-file like makeURLsHyper((path to desktop folder as string) & "TestDoc.rtf") and worked quite fine in my little tests. But it doesn't care about text formatting at this point.
on makeURLsHyper(pathOfRtfFile)
-- converting the given path to a posix path (quoted for later use)
set myRtfPosixPath to quoted form of (POSIX path of pathOfRtfFile)
-- RTF Hyperlink start
set rtfLinkStart to "{\\\\field{\\\\*\\\\fldinst HYPERLINK \""
-- RTF Hyperlink middle
set rtfMiddlePart to "\"}{\\\\fldrslt "
-- RTF Hyperlink end
set rtfLinkEnd to "}}"
-- use sed to convert http-strings to rtf hyperlinks
set newFileContent to (do shell script "sed -i bak -e 's#\\(http[^[:space:]]*\\)#" & rtfLinkStart & "\\1" & rtfMiddlePart & "\\1" & rtfLinkEnd & "#g' " & myRtfPosixPath)
end makeURLsHyper
Have a nice day, Michael / Hamburg

Save current document as .html with same name and path

I'm working on a script for FoldingText which will convert a FoldingText outline (basically a Markdown Text file) into a Remark presentation (an HTML script which makes slideshows from Markdown files). The script works, but I'd like to make the following improvement:
Instead of asking for the name and location to save the HTML file, I'd like to grab the name of the current document and save it as an HTML file (with the same name) in the current folder. The script should fail nicely if there is already a document with that name (offering to either write-over the document or save as a new document with a different name).
The script I'm using for writing to the file was from these forums. The first part is:
on write_to_file(this_data, target_file, append_data) -- (string, file path as string, boolean)
try
set the target_file to the target_file as text
set the open_target_file to ¬
open for access file target_file with write permission
if append_data is false then ¬
set eof of the open_target_file to 0
write this_data to the open_target_file starting at eof as «class utf8»
close access the open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
And the second part is:
set theFile to choose file name with prompt "Set file name and location:"
my write_to_file(finalText, theFile, true)
Thanks.
FoldingText should have some way of retrieveng the path of the document. I've not found any free dowonload fo the application, so I've not benn able to check by myself, but you should be able to find it if you view the dictionary of the application.
My guess is that there's a property like 'path of', or 'file of' for the FoldingText document:
You will probably end up with something like this:
set theDocPath to file of front document of application "FoldingText"
tell application "Finder"
set theContainer to container of theFile
end tell
set newPath to (theContainer & "export.html) as string
repeat while (file newPath exists)
set newPath to choose file name with prompt "Set file name and location:"
end repeat
my write_to_file(finalText, newPath, true)

how can I view tar archiving process in Terminal using this applescript?

I got this script working as an app in Finder but I would like to get an alert when the archive is done. Any idea how I can do this?
tell application "Finder"
set theItems to selection
repeat with _a from 1 to (count of theItems)
set theItem to (item _a of theItems) as alias
set itemPath to quoted form of POSIX path of theItem
set fileName to name of theItem
set theFolder to POSIX path of (container of theItem as alias)
set zipFile to quoted form of (theFolder & fileName & ".tgz")
do shell script "tar czvf " & zipFile & " " & itemPath
end repeat
end tell
many thanks
Erik

Applescript path to not working in Mountain Lion

This script has been working on 10.7 and older, but in 10.8, it seems it's broken. The line:
set theFilePath to ((path to application support from user domain) as rich text) & "AppFolderName:" & UniqueName as string
set theFileReference to open for access theFilePath with write permission
Worked fine on previous versions, but Apple apparently is preventing it from working properly on Mountain Lion. Is there any other way of getting access to that folder via Apple script in Mountain Lion?
Edit: I've included the entire code of the script that will, within a Mail rule export the entire message to a text file that my program can import. The text file is sent to ~/Library/Application Support/MyProgram/MailImport/
Make sure the directory already exists on your machine, as it does here on mine, and the Apple Script doesn't do any checking for it.
This script does not work when path to application support is in the code, but changing it to path to desktop work fine, meaning there is an issue writing to the application support folder, but the code works.
To test, you can create a new rule in Mail, and have Every Message run the script. You have to put the script in ~/Library/Application Scripts/com.apple.mail/
It will then appear as an option in the rules window. You can right-click a message and select Apply Rules to test the script on an individual message.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
set sub to subject of eachMessage
set mid to message id of eachMessage
set sen to sender of eachMessage
set recp to ""
repeat with thisRecpt in recipients of eachMessage
set recp to recp & address of thisRecpt & ","
end repeat
set {year:y, month:m, day:d, hours:hh, minutes:mm} to (date sent of eachMessage)
set dat to (y * 10000 + m * 100 + d) as string
set tim to (hh * 100 + mm) as string
set con to content of eachMessage
set TotalString to "<!STDMessageSubject>" & sub & "<!STDMessageSubject>" & "<!STDMessageID>" & mid & "<!STDMessageID>" & "<!STDMessageSender>" & sen & "<!STDMessageSender>" & "<!STDMessageRecipient>" & recp & "<!STDMessageRecipient>" & "<!STDMessageDate>" & dat & "<!STDMessageDate>" & "<!STDMessageTime>" & tim & "<!STDMessageTime>" & "<!STDMessageContent>" & con & "<!STDMessageContent>"
set UniqueName to do shell script "uuidgen"
set theFilePath to ((path to application support from user domain) as rich text) & "MyApplication:MailImport:" & UniqueName as string
set theFileReference to open for access theFilePath with write permission
write TotalString to theFileReference
close access theFileReference
end repeat
end tell
end perform mail action with messages
end using terms from
There's no such thing in applescript as "rich text". It should only be as "text". In addition theFilePath is a string, so in the next line you need to reference it like this... open for access file theFilePath. Notice the word "file". You need that word there to turn the string into a file reference which is what that command requires.
EDIT: Now that I see your entire code I would write it like this. Your problem still may be a sandboxing issue but at the very least you should eliminate any sources of possible coding errors in your script. This will give you the best chance of having a successful script. If it still doesn't work then it probably is a sandboxing issue.
The basic coding issues that I see are that you are telling Mail to perform all of the commands. Mail doesn't know commands such as "path to application support", "do shell script", or how to write to a file. They're applescript commands so you shouldn't tell Mail to perform them. They are not in Mail's applescript dictionary and thus they may be getting confused when Mail tries to perform them. That's certainly the reason that "text" keeps changing to "rich text" as you mention.
So give this a try. If you still have the problem then at least you know that you've done all you can to eliminate sources of errors in your code.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
repeat with eachMessage in theMessages
set sub to subject of eachMessage
set mid to message id of eachMessage
set sen to sender of eachMessage
set recp to ""
repeat with thisRecpt in recipients of eachMessage
set recp to recp & address of thisRecpt & ","
end repeat
set {year:y, month:m, day:d, hours:hh, minutes:mm} to (date sent of eachMessage)
set dat to (y * 10000 + m * 100 + d) as string
set tim to (hh * 100 + mm) as string
set con to content of eachMessage
set TotalString to "<!STDMessageSubject>" & sub & "<!STDMessageSubject>" & "<!STDMessageID>" & mid & "<!STDMessageID>" & "<!STDMessageSender>" & sen & "<!STDMessageSender>" & "<!STDMessageRecipient>" & recp & "<!STDMessageRecipient>" & "<!STDMessageDate>" & dat & "<!STDMessageDate>" & "<!STDMessageTime>" & tim & "<!STDMessageTime>" & "<!STDMessageContent>" & con & "<!STDMessageContent>"
my writeToFile(TotalString)
end repeat
end tell
end perform mail action with messages
end using terms from
on writeToFile(TotalString)
set UniqueName to do shell script "uuidgen"
set theFilePath to ((path to application support from user domain) as text) & "MyApplication:MailImport:" & UniqueName
set theFileReference to open for access file theFilePath with write permission
write TotalString to theFileReference
close access theFileReference
end writeToFile
EDIT2: try this handler in place of the one in the above code. This may be one way to make the writeToFile handler work because the writing part would happen in a separate process from the applescript. It's worth a try!
on writeToFile(TotalString)
set UniqueName to do shell script "uuidgen"
set theFilePath to ((path to application support from user domain) as text) & "MyApplication:MailImport:" & UniqueName
set theResult to do shell script "echo " & quoted form of TotalString & " > " & quoted form of POSIX path of theFilePath
end writeToFile
EDIT3: if edit2 doesn't work then look here. It seems others have had problems with Mail writing to certain locations and solved it by adding a key to Mail to give it permission.
So it turns out to be a Sandboxing issue. Apple Mail in 10.8 uses a Sandboxed Application Support folder location in general regardless of how hard you try to just get ~/Library/Application Support/, so from an AppleScript within Mail on 10.8
path to application support from user domain
Returns the path
~/Library/Containers/com.apple.mail/Data/Library/Application Support/
From there the MyApplication:MailImport: folders can be created and accessed. Since our actual program that's trying to read the output isn't sandboxed we can just read and access the data from that location for now, as it seems to be working fine.

Resources