Trying to script LaunchBar itself - space key?

Sharing and discussing custom actions for LaunchBar
Post Reply
Cottser
Rank 1
Rank 1
Posts: 21
Joined: Fri Jan 22, 2010 2:57 am

Trying to script LaunchBar itself - space key?

Post by Cottser » Sun Jan 24, 2010 4:43 am

I am trying to create an AppleScript to replicate my most-used trigger from Quicksilver.

It is a Google search shortcut. Activate it, and you have a text field ready to search Google. I'm planning to use Spark to run the script.

The first part was easy, getting "Google" selected in LaunchBar. Seemed to work fine.

I couldn't figure out how to get LaunchBar to focus the text field, though. I tried a manual keypress through System Events, but for some reason that activates the Edit Text action with "Google" entered in the field.

Here's my initial script:

Code: Select all

tell application "LaunchBar"
   activate
   set selection as text to "Google"
   
   tell application "System Events"
      keystroke space
   end tell
end tell


A second attempt that works OK, but relies way too much on UI scripting. Definitely the hit-it-with-a-hammer solution, and I'm not pleased with it.

Code: Select all

tell application "LaunchBar"
   activate
   
   --delay so we don't mix the keyboard input of
   --launching this script with the keystrokes
   --below. what a mess :)
   delay 0.2
   
   if has keyboard focus then      
      tell application "System Events"
         
         keystroke "g"
         keystroke "o"
         keystroke "o"
         keystroke "g"
         keystroke "l"
         keystroke "e"
         
         keystroke space
      end tell
   end if
   
end tell


I'm hoping there's something I can pass to LaunchBar to activate that text field so it's ready for text entry.

Any experts out there?

Cottser
Rank 1
Rank 1
Posts: 21
Joined: Fri Jan 22, 2010 2:57 am

Re: Trying to script LaunchBar itself - space key?

Post by Cottser » Mon Jan 25, 2010 8:01 am

After looking through the documentation (which is awesome, by the way), I found a much better way of selecting Google.
http://www.obdev.at/resources/launchbar/help/URLCommands.html

I'd rather avoid the System Events call altogether, but this is at least quicker and more consistent.

Code: Select all

tell application "LaunchBar"
   activate
   open location "x-launchbar:select?abbreviation=GOOGLE"
   
   delay 0.05
   
   tell application "System Events"
      keystroke space
   end tell
   
end tell

Cottser
Rank 1
Rank 1
Posts: 21
Joined: Fri Jan 22, 2010 2:57 am

Re: Trying to script LaunchBar itself - space key?

Post by Cottser » Sat Mar 27, 2010 4:57 pm

If anyone is curious, I ended up ditching the "trigger" because although it's less keystrokes, running the AppleScript is slow.

The search field comes up quicker for me if I hit Cmd-Space, G, Space. Bit of muscle memory retraining.

Post Reply