Help converting AppleScript for LaunchBar

Sharing and discussing custom actions for LaunchBar
Post Reply
Jono
Rank 4
Rank 4
Posts: 161
Joined: Mon Feb 25, 2008 11:25 am
Location: UK
Contact:

Help converting AppleScript for LaunchBar

Post by Jono » Wed Jun 25, 2014 3:28 pm

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.

iRounak
Rank 4
Rank 4
Posts: 191
Joined: Fri Oct 23, 2009 7:11 pm
Contact:

Re: Help converting AppleScript for LaunchBar

Post by iRounak » Wed Jun 25, 2014 5:48 pm

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.

Jono
Rank 4
Rank 4
Posts: 161
Joined: Mon Feb 25, 2008 11:25 am
Location: UK
Contact:

Re: Help converting AppleScript for LaunchBar

Post by Jono » Wed Jun 25, 2014 7:06 pm

Brilliant, thanks a lot!

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

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: Help converting AppleScript for LaunchBar

Post by ptujec » Wed Jun 25, 2014 9:47 pm

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 "--"

Jono
Rank 4
Rank 4
Posts: 161
Joined: Mon Feb 25, 2008 11:25 am
Location: UK
Contact:

Re: Help converting AppleScript for LaunchBar

Post by Jono » Wed Jun 25, 2014 10:41 pm

Thanks, but if I try that it throws up an error:

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

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: Help converting AppleScript for LaunchBar

Post by ptujec » Wed Jun 25, 2014 11:10 pm

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?

Jono
Rank 4
Rank 4
Posts: 161
Joined: Mon Feb 25, 2008 11:25 am
Location: UK
Contact:

Re: Help converting AppleScript for LaunchBar

Post by Jono » Thu Jun 26, 2014 12:04 am

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 :?

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: Help converting AppleScript for LaunchBar

Post by ptujec » Thu Jun 26, 2014 12:42 am

So you made an action with the script inside? Or just the plain AS? I just tested the plain script.

iRounak
Rank 4
Rank 4
Posts: 191
Joined: Fri Oct 23, 2009 7:11 pm
Contact:

Re: Help converting AppleScript for LaunchBar

Post by iRounak » Thu Jun 26, 2014 5:36 am

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.

Jono
Rank 4
Rank 4
Posts: 161
Joined: Mon Feb 25, 2008 11:25 am
Location: UK
Contact:

Re: Help converting AppleScript for LaunchBar

Post by Jono » Thu Jun 26, 2014 7:00 am

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.

ptujec
Wizard
Wizard
Posts: 321
Joined: Fri Dec 19, 2008 11:36 am
Contact:

Re: Help converting AppleScript for LaunchBar

Post by ptujec » Thu Jun 26, 2014 8:58 am

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

Jono
Rank 4
Rank 4
Posts: 161
Joined: Mon Feb 25, 2008 11:25 am
Location: UK
Contact:

Re: Help converting AppleScript for LaunchBar

Post by Jono » Thu Jun 26, 2014 11:04 am

Ah, that's great. Thanks a lot! :)

Post Reply