Archive for April, 2008

Apr 16 2008

TextMate: Execute Inline Ruby

Published by under TextMate

When working in TextMate, if you have a one or more lines of Ruby code that you would like to have the interpreter run, without the need for passing the entire file to the interpreter, this tip may be just what the doctor ordered.

For example, when debugging, often times it would be really handy to run a simple script at a specified location in your code to look at a value, call a method on an Object… The key point here is that you can have as little as one line of Ruby processed by the interpreter and the return value will be inserted inline.

This easiest way to get the gist of this is to watch the screencast below:

If you want to insert the results of one or more lines of script, directly into your code, using # => markers is the way to go.




The music in the video is J.J. Cale and the song: Call Me the Breeze.

A longer clip of J.J. Cale jamming: [audio:/2008/jj.mp3]

6 responses so far

Apr 15 2008

Launch Applications from Terminal

Published by under System

When working inside a terminal window, there’s a quick shortcut you can use to open a file (read, launch an application on a specific file or set of files). The beauty of this trick is that the command will work as if you clicked on an application icon(s) in Finder. For example, if you request to open a file with an extension of xml, the application associated with that extension will be started and the specified file will be passed to the application.

The command I am referring to is open; read on to see a few examples:

From a terminal window, to all the files with a “.rb” extension (in the current directory):

> open *.rb

To launch a browser at a specified URL, try this:

> open http://macDeveloperTips.com

And to open Finder in the current directory:

> open .

You can get more information about using the open command by viewing the manual page:

> man open

One response so far

Apr 14 2008

Finder and glob

Published by under AppleScript

I’ve found a really nice, well documented, AppleScript application that makes for easy work to use Unix path pattern expansion (glob) syntax along with Finder. This is the utility you want around when you need to select a specific set of files within Finder, and command-clicking just isn’t going to cut it.

For instance, when you need to select files with a unique patterns such as: files that contain the string “test” (anywhere within the filename), or only those files where the name contains “test” and the extension is “rb”.

The application I am referring to is lselect, and here are a few examples:

All files with “test” in the name and the extension “rb” will be selected:

Easy enough, however, lselect also allows you to add to the current list of selected files, as shown below where I ask for all files with a “C” extension (notice the button ‘Add Matches’):

The resulting list of files is shown below:

The source code for lselect is shown below (it looks rather long, however, much of the listing is comment blocks):

Script Editor Click here to paste the code below into the Script Editor

(*
  lselect 1.1 by Jim DeVona

http://anoved.net/lselect.html

  1.0: 1 November 2006
  1.1: 18 December 2006 (somewhat improved Column view behavior)

  Select files in the current Finder folder using shell glob syntax.
  When invoked, the user will be prompted to supply a glob pattern.
  The "ls" command line utility is used to determine which files match
  the pattern, and then they are selected.

  Suggested installation location:
    ~/Library/Scripts/Applications/Finder/lselect.scpt

  The script can be invoked with the standard Mac OS X Script Menu,
  but I've found FastScripts (http://www.red-sweater.com/fastscripts/)
  to be a preferable alternative, primarily because of the ease with
  which reliable keyboard bindings can be assigned. I use Command-G.

  Issues:
    - If the last match is a directory and the current view type is
      Column, other matches will not end up selected (they appear
      to lose selection when the Finder come to rest on and reveals the
      contents of the last directory). I don't know how to prevent this.
    - Spaces must be escaped to match properly: "iTunes\ Music"
    - Performance is poor with hundreds of matches; see notes below

  Wishes:
    - Select sub-matches within current selection
    - Select matches on desktop instead of in "Desktop" window
    - Support patterns like "../" (subfolder patterns work, but not parent)
*)

tell application "Finder"

  (*
    Determine the present working directory as alias and POSIX path.
    If the insertion location is not a folder, use its parent. This is the
    case when a file is selected in Column view (otherwise, file selections
    do not seem to be treated as the insertion location).
  *)
  set pwdAlias to insertion location as alias
  if not (exists folder pwdAlias) then
    set pwdAlias to (container of pwdAlias) as alias
  end if
  set pwd to POSIX path of pwdAlias

  (*
    Ask the user what to select. Dialog time out is equivalent to cancellation.
    The default "Select Matches" option clears the current Finder selection,
    whereas "Add Matches" leaves it intact. Clearing the selection is not done
    in Column view if the displayed folder is the only thing selected.
  *)
  set dr to display dialog "Glob pattern:" default answer ¬
    "" buttons {"Cancel", "Add Matches", "Select Matches"} ¬
    default button 3 cancel button 1 with title pwd giving up after 60
  if button returned of dr is equal to "" then
    return
  else if button returned of dr is equal to "Select Matches" then
    try
      -- do not clear selection if the only thing selected is the focal folder
      if selection as alias is not equal to pwdAlias then select {}
    on error
      -- more than one thing already selected
      select {}
    end try
  end if

  (*
    Initialize list of selected files. Generally identical to selection returned
    by Finder, except the present working directory should not be included,
    which is initially selected in some Column view circumstances (see above).
    This is a little clumsy; selection state is vaguely defined in Column view.
  *)
  set selectables to selection
  try
    if selection as alias is equal to pwdAlias then set selectables to {}
  end try

  (*
    Get the glob pattern given by the user.
    We treat a blank pattern as cancellation (use * to select everything).
    Alternatively, omit this conditional to select the containing folder;
    this ought to be the default behavior once "../" issues are ironed out.
  *)
  set query to text returned of dr
  if query = "" then return

  (*
    Ask ls for a listing of files that match the given pattern.
    From the ls man page:
      -d Directories are listed as plain files (not searched recursively).
    If nothing matches the query, ls will return an error; just stop.
  *)
  try
    tell me to set matches to do shell script ("/bin/ls -d " & quoted form of pwd & query)
  on error
    return
  end try

  (*
    Parse each line of the response from ls as the path to a match.
    The visibility test is twofold: the "info for" test throws an error on
    Icon^M (the full name doesn't survive all translations and transmissions).
    This try-info-for-visibilty test is the main bottleneck;
    for faster handling of many matches (100s), replace this
    repeat body with "set end of selectables to matchpath as POSIX file"
  *)
  repeat with matchpath in paragraphs of matches
    set posixmatch to matchpath as POSIX file
    try
      set fileinfo to info for posixmatch without size
      if visible of fileinfo then set end of selectables to posixmatch
    end try
  end repeat

  (*
    Conclude by selecting the results.
    The "try" protects against cases we don't [yet] handle,
    such as certain "../" path traversals and anything else that may come up.
    If the last item of selectables is a directory and we're using
    Column view, other items may not end up selected.
  *)
  try
    select every item of selectables
  on error errMsg number errNum
    display alert "Could not make selection (" & errNum & "):" message errMsg as critical
    return
  end try
end tell

Here is brief list of glob syntax you can try with lselect:

? Match any one character.
* Match any sequence of zero or more characters.
[abc] Match any one character in abc. A range of characters may be specified as a-z, or 0-9.
[^abc] Match any one character not in abc.
{a,b,c} Matches any of the strings a, b, or c, which may consist of more than one character each.

lselect shows a good use of AppleScript, is well documented and it’s handy…all good things.

2 responses so far

Apr 11 2008

Screencasts with ScreenFlow – Part 3

Published by under Tools/Utilities

I’ve covered some of the basics for working with ScreenFlow in the two previous posts. At this point I want to show you a non-conventional way that I use screencasts on a regular basis. What I am referring to is creating videos that are not meant to live on YouTube or another publicly accessible server, rather, videos that have a very limited lifespan, as in hours.

I often find that a quick screencast is the most efficient means to explain something, from how to install/configuration an application to pointing out a problem spot in a block of code.

For example, in the video that follows I talk through the steps I recently explained to another blogger for inserting code into a php file on a WordPress blog to display a Digg icon. The video is short and to the point.

The same idea holds in the next video, no bells or whistles, just a simple description on how to choose a color using the Color Picker application and use the hex value of the selected color to configure style sheet information in an application.

If you are already familiar with creating screencasts, or would like to give it a go, you owe it to yourself to download a free trial of ScreenFlow.

3 responses so far

Apr 08 2008

Screencasts with ScreenFlow – Part 2

Published by under Tools/Utilities

I apologize this video is no longer available. I had a system crash on my web host and was unable to recover the video. To make matters more frustrating I no longer have a copy of ScreenFlow to recreate the video.

Apologies for any inconvenience.

John

In a previous post on using ScreenFlow, I demonstrated some of the basics of this unique application for creating screencasts. What follows is another video demonstrating how to record video separately from audio. Unless you’ve spent time creating a screencast, and have experienced the challenges that come with the same, you might not fully appreciate the value of this feature.

Let me put it this way, I often find it challenging to synchronize all the moving parts, from typing code, clicking the mouse to verbally describing what I’m trying to demonstrate, and if there’s a means to simplify the process, I’m there.

There are tools available for recording video and audio separately, and mixing the two into a final product. However, ScreenFlow offers these capabilities in one tool.

Have a look at the video below to see how I create an audio recording inside of ScreenFlow and merge this with a video recording:

In my short time in working with ScreenFlow, I’ve been impressed with both its ease of use (read, short learning curve) and the powerful and well thought out (and implemented) features to take away some of the pain of creating screencasts.

4 responses so far

Apr 07 2008

Screencasts with ScreenFlow – Part 1

Published by under Tools/Utilities

This post is one in a series demonstrating a very intriguing application for creating screencasts, ScreenFlow. If you are currently creating screencasts on a Mac, or have considered doing so, you definitely need to give this a look.

In this post I will create three short screencasts, all created using ScreenFlow. The first will be a simple demonstration of a coding trick which is handy when working with column blocks in TextMate. The second will show how to make various modifications to the first video from within ScreenFlow, and the final will show the end result.

When watching the first video, understand that we haven’t done any editing as of yet, so the code/text is rather hard to read. This is even more pronounced in the second video where I demonstrate ScreenFlow to capture a video showing various editing features of ScreenFlow (it’s not as confusing as it sounds). Stick with me on this and I’ll make it right (click the image below to watch the first video)..

Now that we’ve captured the demonstration of the TextMate trick, we need to dive into some of the editing features of ScreenFlow to make the text more readable (click the image below to watch the video):

And with the changes complete, we can now see the final result (you know the drill):

Join me in the next post where I’ll demonstrate some additional features of ScreenFlow including how to record video separately from audio. If you’ve ever created a screencast, you’ll truly appreciate this feature!

Comments Off

Apr 04 2008

Mac Developer Tips on TV!

Published by under General

…NetBeans TV, that is. The short screencast I created on using NetBeans, Ruby and rb-appscript to control scriptable applications on a Mac can now be seen on NetBeans.tv! If you aren’t familiar with NetBeans.tv, it is a popular extension of the NetBeans.org site, focused on connecting the people, projects and technologies surrounding NetBeans.

Rob Demmer from the NetBeans team contacted me about posting the video on NetBeans.tv. I’m all for spreading the word in the developer community and if the video I created can play even a small part in helping to reach a few more developers or otherwise introduce a new technology to this audience, count me in.

As far as NeBeans.tv, there are several sections to the site: Interviews, Community, News, Screencasts and On the Road. The last section is quite interesting as it is a video diary of sorts that chronicles the days and nights of two guys traveling around the world meeting NetBeans developers (which sounds like a great job, if you can get it). It’s an interesting website, definitely worth a look.

If you would like to watch the video, as it appears on the NetBeans.tv site, click the image below:

Comments Off

« Prev - Next »