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!

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

General Discussion

Apple Script Tutorials?

Are there any apple script or similar program tutorials on this site or any other site for projects. Just to get to know the software better? Or maybe some ideas of cool things to do in Apple Script. I'm a beginner.

2 Answers

Mike Baxter
Mike Baxter
4,442 Points

I know Apple Scripts are supposed to be intuitive and all, but I found them to be confusing, and I didn't come across any decent resources last time I looked. I had to kind of patch different semi-decent resources together to get what I wanted. (And not all things are Apple Scriptable; Photoshop would be an example of something that would be nice to write a script for but isn't allowed.)

I made a few interesting ones: One of them iterates through a bunch of different window sizes I like to use for web browsing and code editing. (But as of Mavericks it requires giving the browser permissions that it probably shouldn't have, so I'm not using that one anymore.) I also made one to trigger jQuery events in Safari to help me rearrange a bunch of data for a project at work. (Chrome crashed as if its command buffer was filled or something, but Safari handled it gracefully.)

Sorry that's not more helpful; it wasn't super easy finding resources, and I doubt there's anything out there nearly as nice as Treehouse.

Mike Baxter
Mike Baxter
4,442 Points

Here's an Apple Script that calculates how many minutes of video are on a given project page. (It might break if they change their HTML element naming scheme.)

Note: It's the user's responsibility to understand the Apple Script. Don't run an Apple Script on your computer unless you know what it's going to do.

tell application "Safari"
    tell front document
        do JavaScript "var time = 0; $('.achievement-steps li p').each(function () { var text = $(this).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');"
    end tell
end tell

You'd have to modify it for whatever web browser you're using. Here I'm using Safari, which seems more reliable with Apple Scripts than Chrome.

It takes quite a bit more work to remove all completed videos, so I didn't go to the effort to do that.

The basic break down of the code goes like this:

  • Tell Safari's active tab to perform some JavaScript:
  • The JavaScript looks like this in a more readable format (but it has to be condensed for Apple Script, I think)
// Working variable for time in minutes
var time = 0;

// Grab every element that might have a video duration in it. (Sometimes it's a code challenge, but parseInt() turns it to 0 so we don't have to worry about that.)
$('.achievement-steps li p').each(function () {

   // Text in the <p> tag
   var text = $(this).text();

   // Integer, returns minutes only
   var min = parseInt(text);

   // Remove minutes and the : to return only seconds
   text = text.replace(min + ':','');
   var sec = parseInt(text);
   sec /= 60;

   // Update time
   time += min + sec; } );

   // Display value to user
   alert('Total time = ' + Math.round(time) + ' minutes');

I usually wouldn't comment code that heavily, but maybe it helps :)