LaunchBar script that returns the path of a folder "dropped" on it

I want to make a LaunchBar script that displays in a dialog the path of a file or folder that's chosen in the first pane and then "dropped" on a script. But I can't make it work.

I've got a couple of working scripts that almost do what I want:

Return contents of first pane (used with a shortcut):

tell application "LaunchBar"
   set x to selection as text
   return x
end tell

Display text written in the first pane and "dropped" on the script:

on handle_string(textFromLaunchbar)
   tell application "LaunchBar"
       display in large type textFromLaunchbar
   end tell
end handle_string

The latter uses the handle_string() handler but I can't quite make the open() handler work for files and folders:

from the LaunchBar manual:

http://www.obdev.at/resources/launchbar/help/index.php?chapter=SendingItems

Sending items to AppleScripts

To pass a selected file, URL or text to an AppleScript, press the Tab key to trigger the “Send to…” action, select the desired AppleScript and press Return. Files will be passed to the script’s open() handler, URLs will be passed to the handle_string() handler.


Solution 1:

The open() handler gets passed an alias (which is a kind of a pointer to a file or folder). You can then simply convert the alias to text, but it will show you the path with colons as separators (e.g. "Macintosh HD:Users:Username:Documents:Folder:").

Use POSIX path of to convert it to a UNIX-style path with "/" separators, and if you want to paste it into Terminal, especially if the path has spaces in it, you should use quoted form of to properly enclose it in quotes.

The following script works with both folders and files for me, when an item is passed to it via LaunchBar:

on open (thisItem)
    display dialog (thisItem as text) -- alias
    display dialog quoted form of POSIX path of thisItem -- POSIX path
end open