Archive for the 'System' Category

Apr 25 2008

MacFUSE

Published by under System

MacFUSE is an open source project that simplifies the process of adding a file system to Mac OS X. If you’ve ever had an inclination to create a file system, or thought that a file system like “interface” would be an intuitive approach to solving a problem, yet, upon delving into what it takes to create a kernel extension, you opted to rethink your idea (or at least the approach) here’s where MacFUSE comes in, effectively offering an API to implement a file system.

FUSE (file system in user space) was born and bred as a Linux tool. MacFUSE is a port of FUSE to the Mac by Amit Singh. From the highest level, there are two key aspects to FUSE. First and foremost (without it you have nothing) the dynamically loadable kernel extension. This is where all the hard work (aka magic) takes place. The second aspect is the API, not unlike any other API you’ve worked with, the FUSE library offers an interface to communicate with the kernel extension. Using calls such as read, open and write, working with your own file system on FUSE becomes a rather trivial undertaking.

Following are a few examples of application using MacFUSE:

  • SpotlightFS: an application that dynamically generates smart folders from Spotlight queries
  • SSH FileSystem: file system client based on the SSH

To learn more about MacFUSE, you can watch this video of Amit reviewing his MacWorld 2007 presentation.

One response 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

Mar 28 2008

Copy Finder Path to Clipboard – Tip 2

Published by under AppleScript,System,Tools/Utilities

Yesterday I wrote a short AppleScript application that copied the current path of the Finder window to the clipboard. A rather unassuming, yet handy application. One downside to this application is that if you want the path of a folder selected in Finder, you would have to double-click on the folder to change the Finder path to that folder location, and then you could engage the AppleSscript application to copy the path.

For example, in the figure below clicking the Script Editor icon that we created in yesterday’s post (to the right of the terminal window with green arrow) will copy the path /Applications/ to the clipboard. However, what if you intention was to copy the path of the highlighted folder, that is, /Applications/Utilities/Java/ ?

I’ve reworked the original example to add support for dragging a folder onto the icon and also moved the code for displaying a dialog box when an error occurs into a subroutine. Take a look at updated application below:

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

-- When clicking on the icon
try
  tell application "Finder"
    set currentPath to (POSIX path of (target of front window as alias))
    set the clipboard to currentPath
  end tell
on error
  my finderErrorMsg()
end try

-- When dropping a folder onto the icon
on open {droppedFolder}
  try
    tell application "Finder"
      set the currentPath to (POSIX path of droppedFolder as text)
      set the clipboard to currentPath
    end tell
  on error
    my finderErrorMsg
  end try
end open

-- Error msg when unable to copy a path to the clipboard
on finderErrorMsg()
  display dialog "Unable to copy a path to the clipboard.
                      Make sure Finder is referencing a directory/folder
                      within the file system."
                      buttons {"Ok"} with icon caution with title "Error"
end finderErrorMsg

With the addition of the on open reference (line 12) you can now drag/drop a folder in Finder onto the application icon and its path will be copied to the clipboard. Before you can use the script, you will need to save the AppleScript code as an application and drag the script onto the toolbar (remove any previous version of the program by right clicking and selecting ‘Remove Item’).

As a quick debugging tip, you can add a dialog box as shown below to display the path that was copied into the clipboard:

tell application "Finder"
  set currentPath to (POSIX path of (target of front window as text))
  set the clipboard to currentPath
  display dialog currentPath buttons {"ok"}
end tell
...

Now you can view the clipboard contents (through the dialog box) which makes the debugging process a little easier should you decide to tweak this application.

Another good example of the power of scripting on a Mac.

7 responses so far

Mar 27 2008

Copy Finder Path to Clipboard – Tip 1

Published by under AppleScript,System,Tools/Utilities

I’ve written a short script that I’ve found more useful that I ever anticipated. It is nothing more than an AppleScript application that copies the current path of Finder to the clipboard. Sounds rather unassuming, however, I think you’ll be surprised how handy it is.

The AppleScript code is less than 10 lines that instruct Finder to get the path of the front most Finder window and copy the path to the clipboard.

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

try
  tell application "Finder"
    set currentPath to (POSIX path of (target of front window as alias))
    set the clipboard to currentPath
  end tell
on error
  display dialog "Unable to copy a path to the clipboard.
                      Make sure Finder is referencing a directory/folder
                      within the file system."
                      buttons {"Ok"} with icon caution with title "Error"
end try

From within Script Editor, save the code as an application and then drag/drop the application to the toolbar in Finder. The screenshot below shows the Script Editor icon in Finder (to the right of the terminal window with the green arrow), where it is now one click away.

There is a trivial amount of error handling in this short script. If you try and run this script while Finder is referencing the system (as compared to a directory/folder on the system) you’ll get the following error message.

In the next tip of this series we’ll drill down one level further and I’ll show how to copy the path of a selected folder within the current Finder window, to the clipboard.

10 responses so far

Mar 25 2008

Open Terminal Here…

Published by under System,Tools/Utilities

I can’t count how many times I’ve found myself in Finder and wanted to jump to a terminal at the current path location. The Open Terminal application is one of those little gems that will make you wonder how you ever got along without it. If you spend any time at all moving between Finder and a terminal, read on…

Installing the application is as simple as extracting the zip file and dragging the OpenTerminal.app file into a location where you store other scripts, tools, utilities, etc. I have a folder within my home directory where I dump all this kind of stuff, so down the road when I bump into this folder I’ll have a clue where the applications came from.

With the app installed, open Finder and drag the application to the toolbar. You’ll see the icon appear as shown here:

The first time you run this application you will be presented will an impressive list of options. The defaults work fine, however, take a few minutes to read through each option. Depending on how you work, chances are you can configure this app to keep pace with you.

Clicking on the Shell config option (on the top left) will present even more configurations choices:

If you are not using the default shell on Mac OS X (bash) I recommend choosing the ‘pushd’ option as this is another really handy tool to have when bouncing around the file system (pushd/popd are built-in commands on bash).

From here forward, whenever you are in Finder and want to jump to a terminal, simply click on the Open Terminal icon, and you’ll be whisked off to a terminal window. Slick.

The latest version will only work on Leopard, however, you can download an older version for Tiger as well. The only downside to this application is that the source code is not included. I’ll keep my eyes peeled for a similar version that includes the source. If you find one first, please post a comment.

OpenTerminal also includes a contextual menu plugin for the Finder that adds a "Open Terminal" command, that is as close as right click…

3 responses so far

Mar 19 2008

Mass Rename File Extensions

Published by under System,Tools/Utilities

I recently had to rename somewhere in the neighborhood of 100+ files as I was moving code from one platform to another. Obviously, I was looking for a quick solution, and what follows is the shell (Bash) script that I used to do the job:

Continue reading...

4 responses so far

Mar 11 2008

Copy/Paste from Safari

Published by under Safari,System

One thing I was accustomed to when working on Windows, was copying a section of a webpage and pasting the same into a Word document. Word did a pretty good job of keeping the formatting intact, including images, fonts and the like. This trick is handy when you want to print only a portion of a webpage.

You can get there from here on a Mac, albeit with a different approach as to the internal implementation.

Continue reading...

Comments Off

« Prev - Next »