Home Link

Hello, my name is Brett Terpstra, and it’s nice to meet you. Elegant solutions to complex problems. Curious?

oft: Bash function for opening a specific filetype

oft: Bash function for opening a specific filetype Tagged: bashscripting
Jul 07, 2011 (213 days ago)

Here’s another simple Bash function that I’ve used so much recently I thought I should share. It’s called oft, which stands for Open File Type, and can be used as a standalone shell script or as a function in your .bash_profile. When run, it looks in the current directory for files with extensions that match (or partially match) the first argument and opens them.

My most obvious use case is Xcode projects, where I may have dozens (and dozens) of files, but there’s only one .xcodeproj file (folder). I don’t always know the name of the project in the folder, but if I run oft xco it will open it without my having to search. If there is more than one result, it gives you a numeric menu to select the file you want to open. You can cancel, select a single file or “Open ALL” from that menu. If you run oft with no arguments, it will read a (partial) extension from a prompt.

This is a script born of laziness (so many good ones are, though). You can accomplish the same with an ls *.ext, spot the file and open filename.ext. This is just faster and better for me when I’m working with less-than-optimal amounts of sleep.

# Open filetype
# Finds every file in folder whose extension starts with the first parameter passed
# if more than one file of given type is found, it offers a menu
oft () {
  if [[ $# == 0 ]]; then
    echo -n "Enter an extension or partial extension: "
    read extension
  fi
  if [[ $# > 1 ]]; then
    echo "Usage: oft [(partial) file extension]"
    return
  fi
  extension=$1
 
  ls *.*$extension* > /dev/null
  if [[ $? == 1 ]]; then
    echo "No files matching \"$extension\" found."
    return
  fi
  declare -a fileList=( *\.*$extension* )
 
  if [[ ${#fileList[*]} -gt 1 ]]; then
    IFS=$'\n'
    PS3='Open which file? '
    select OPT in "Cancel" ${fileList[*]} "Open ALL"; do
      if [ $OPT == "Open ALL" ]; then
        read -n1 -p "Open all matching files? (y/N): "
        [[ $REPLY = [Yy] ]] && $(/usr/bin/open ${fileList[*]})
      elif [ $OPT != "Cancel" ]; then
        $(/usr/bin/open "$OPT")
      fi
      unset IFS
      break
    done
  else
    $(/usr/bin/open "${fileList[0]}")
  fi
}

Related posts:

  • A Bash function for Markdown bloggers
  • fk: a useful bash function
  • fk: redux
  • A simple but handy Bash function: console
  • Fixing Spotlight indexing of Markdown content
This entry was posted on Thursday, July 7th, 2011 at 5:03 pm and is filed under Blog, Code.You can leave a response, or trackback from your own site. Tagged: bashscripting

3 Responses to “oft: Bash function for opening a specific filetype”

  1. spacer Lri says:
    July 8, 2011 at 2:39 am

    How could this be generalized so that you could use any path selectors, or something like ls and open? Or is there already some smarter way to do that?

    One alternative would be open *.xc*, but it obviously doesn’t display a menu if there’s multiple matches.

    Reply
    • spacer Brett says:
      July 8, 2011 at 4:15 am

      Essentially, this is just a friendly wrapper around open *.xc*. You could modify the ls line to use any pattern if you wanted to do something different.

      I would like to add an option parser to handle -a appname arguments so you could still specify which app to open the result in. I got frustrated with getopts before I got that worked out…

      Reply
    • spacer Lri says:
      July 12, 2011 at 10:16 am

      This is probably completely wrong, but lso: ls and open — Gist.

      Reply

Leave a Reply

Click here to cancel reply.

Comments may use standard Markdown formatting


Notify me of followup comments via e-mail. You can also subscribe without commenting.

Saying…

Follow me on Twitter

spacer spacer

Markdown? spacer

marked

MultiMarkdown preview.
Everywhere.

Learn More

Looking for me elsewhere? Here's my card.

spacer
Writing…RSS
  • Introducing Gather, a Cocoa Markdownifier
  • Connecting nvALT and Address Book
  • ScrivWatcher, one more time
  • From my Macworld Diary
  • ScrivWatcher droplet, an easier live Scrivener preview
  • Preview a full Scrivener document in Marked, live
  • Web excursions: January 20, 2012 — January 24, 2012
  • A Service for writing MultiMarkdown footnotes inline
  • iOS-inspired popup box CSS
  • System Service: Clip to Day One
Listening… (last.fm)
  • Sorting…
    • Blog
    • Bookmarks
    • Code
    • Featured
    • Misc
    • Music
    • Reviews
    • Write
Thanks for reading!

Entries (RSS) and Comments (RSS), or Subscribe by Email

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.