Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Mike Baxter
4,442 PointsAppleScript for Tutorial Video Length
Huge disclaimer: you are responsible for whatever AppleScripts you run on your computer. Take the time to understand it before you use it.
Basically, I wanted to figure out how long it would take to watch the videos of any given project on Team Treehouse. (You can't really calculate code challenges or quizzes.) This is a perfect example of the power and flexibility of an AppleScript, so I decided to implement it.
With the help of Inspect Element on the web browser, I was able to figure out a pattern to detect the difference between completed and non-completed video tutorials. From that, I made an AppleScript that triggers jQuery on any Project page that pulls minutes and seconds of all remaining tutorial videos and displays the cumulative time in an alert. Since this may be helpful for others, I'd like to share that jQuery code. I'm not going to share the whole AppleScript purely out of cautionโI don't think it's wise to run some random AppleScript on your computer unless you understand it.
The AppleScript wrapper is just 5 lines of code, which you can find through a Google search. Basically, you need to tell your web browser to tell the first tab (or document) to do some JavaScript.
The essential jQuery code (which takes advantage of the fact that the pages already use jQuery) is the following:
var time = 0;
$('.achievement-steps li a').each(function () {
if (!$(this).hasClass('completed')) {
var text = $(this).find('p').text();
var min = parseInt(text);
text = text.replace(min + ':','');
var sec = parseInt(text);
sec /= 60; time += min + sec;
}
});
alert('Total time = ' + Math.round(time) + ' minutes.');
Of course, this all has to be within quotation marks in what you tell JavaScript to do. You don't have to make it one line of code, but it only seems to work if you perform a single "do JavaScript" command.
Happy coding!