Page 1 of 1

Help converting AppleScript for LaunchBar

Posted: Wed Jun 25, 2014 3:28 pm
by Jono
I have an AppleScript that gets the current selected application in Finder and shows it's app ID, and want to convert it to use as an action in LaunchBar

Code: Select all

tell application "Finder" to set theApp to item 1 of (get selection)

set appID to id of theApp

set the clipboard to appID
display dialog appID & space giving up after 3


The idea is I'd bring up an app in LaunchBar, hit Tab then select the action. I've tried altering it based on LaunchBar's documentation but my AppleScript knowledge is limited so haven't been successful. I'm hoping it's something pretty simple.

Re: Help converting AppleScript for LaunchBar

Posted: Wed Jun 25, 2014 5:48 pm
by iRounak
I am sure there are better ways to do this but this will get what you want nevertheless:

Code: Select all

on open (thisItem)
   try
      set thisItem to thisItem as text
      set tid to AppleScript's text item delimiters
      set AppleScript's text item delimiters to ":"
      set thisItem to text item -2 of thisItem as text
      set appID to id of application thisItem
      --tell me to activate
      --display dialog appID
      tell application "LaunchBar" to set selection to appID
   on error e
      tell me to activate
      display dialog e
   end try
end open

If you want the dialog box, then you can uncomment the two lines and comment out the line succeeding those two lines.

Re: Help converting AppleScript for LaunchBar

Posted: Wed Jun 25, 2014 7:06 pm
by Jono
Brilliant, thanks a lot!

This will also help me if I want to use another AppleScript that would pass files or apps to LaunchBar.

Re: Help converting AppleScript for LaunchBar

Posted: Wed Jun 25, 2014 9:47 pm
by ptujec
If you use handle_string you don't need the delimiter stuff.

Code: Select all

on handle_string(thisItem)
   try
      set appID to id of application thisItem as text
      set the clipboard to appID as text
      tell application "LaunchBar" to set selection to appID as text
   on error e
      tell me to activate
      display dialog e
   end try
end handle_string


If you don't want to have the stuff on your clipboard you can comment that line out with "--"

Re: Help converting AppleScript for LaunchBar

Posted: Wed Jun 25, 2014 10:41 pm
by Jono
Thanks, but if I try that it throws up an error:

The script does not implement a handler named "open".

Re: Help converting AppleScript for LaunchBar

Posted: Wed Jun 25, 2014 11:10 pm
by ptujec
Thats weird. I just double checked and it works just fine for me. Select app in Launchbar - Tab - select script. Maybe some leftovers in the script when replacing the code?

Re: Help converting AppleScript for LaunchBar

Posted: Thu Jun 26, 2014 12:04 am
by Jono
I've just remade it with a different name and bundle ID, and tried it on 2 different Macs, and still get the same problem.
Strange :?

Re: Help converting AppleScript for LaunchBar

Posted: Thu Jun 26, 2014 12:42 am
by ptujec
So you made an action with the script inside? Or just the plain AS? I just tested the plain script.

Re: Help converting AppleScript for LaunchBar

Posted: Thu Jun 26, 2014 5:36 am
by iRounak
ptujec wrote:Thats weird. I just double checked and it works just fine for me. Select app in Launchbar - Tab - select script.

It works fine.

Re: Help converting AppleScript for LaunchBar

Posted: Thu Jun 26, 2014 7:00 am
by Jono
Yes, I should have said. I made an action with it.

If I run it as an action that's when I get the error. If I save it as a script and run it from there it works fine.

Re: Help converting AppleScript for LaunchBar

Posted: Thu Jun 26, 2014 8:58 am
by ptujec
Ok. Reproduced. The action seems to be a bit stricter with what it accepts.
This should work:

Code: Select all

-- Sources: http://macscripter.net/viewtopic.php?id=24569

on open (_file)
   try
      set appID to bundle identifier of (info for (_file))
      set the clipboard to appID
      tell application "LaunchBar" to set selection to appID as text
   on error e
      tell me to activate
      display dialog e
   end try
end open


I also put it in an action if you want to try mine.

You could also make so that the id gets pasted in your frontmost document. Depends on your usecase.

Code: Select all

tell application "LaunchBar" to perform action "Copy and Paste" with string appID as text

Re: Help converting AppleScript for LaunchBar

Posted: Thu Jun 26, 2014 11:04 am
by Jono
Ah, that's great. Thanks a lot! :)