Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.

share|improve this question
2  
Are you trying to use the path in another application? If so, Cmd-C on the file in LaunchBar will put the POSIX path to the file on the clipboard, no need for AppleScript. –  Ian C. Feb 27 '12 at 18:17
    
no, but that's a very useful little trick –  Tor Thommesen Feb 28 '12 at 0:16

2 Answers 2

up vote 2 down vote accepted

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
share|improve this answer
    
this works for me too, thank you! –  Tor Thommesen Feb 27 '12 at 10:36

I see you know about LaunchBar's "large type" feature -- did you know you can send selected text to it with the "Display in Large Type" action? I.e.

  1. Select your file in LaunchBar
  2. Tab, "dla", return
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.