Display delayed messages from LaunchBar

Sharing and discussing custom actions for LaunchBar
Post Reply
Zettt
Rank 2
Rank 2
Posts: 45
Joined: Sat Mar 28, 2009 1:04 pm
Location: Stuttgart
Contact:

Display delayed messages from LaunchBar

Post by Zettt » Mon May 04, 2009 11:24 am

Hi,

Here we have two scripts that take your input and display a Large Type Message or a Growl Message after a given time.
The original script can be found in this thread.

The naming is a little confusing so i start a new topic. Below are the two scripts. One is for Large Type the other one is for Growl.

Code: Select all

-- Display Alert 1.0 for LB
-- this script displays a large typed message after a given delay time
--
-- originally written by ludwigschubert http://forums.obdev.at/viewtopic.php?f=24&t=615
-- modified by Zettt (http://www.zettt.de) at 2009-05-04
--
-- Usage: Call LaunchBar, Hit Space, Input your message and provied your delay time at the end in "3s", "1m" or "2d" (without quotes)
-- Example: "This is an example message 2s" (without quotes)

-- take string from LaunchBar
on handle_string(message)
   set future_message to message as text
   
   tell application "LaunchBar"
      -- we don't need to talk with LaunchBar from here. But it's nice to have a LaunchBar icon in the following dialogs. :)
      
      -- how much delay do we need?
      -- set delay_time to my delay_dialog()
      set delay_word to last word of message
      
      -- Encode message for URL; strip delay time
      do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(unicode(sys.argv[1], \"utf8\"))' " & quoted form of (texts 1 thru ((length of message) - ((length of delay_word) + 1)) of message)
      set delay_time to result
      
      -- Determine seconds, minutes, hours or days
      set time_multiplier to 1
      if last character of delay_word = "m" then set time_multiplier to 60
      if last character of delay_word = "h" then set time_multiplier to 60 * 60
      if last character of delay_word = "d" then set time_multiplier to 60 * 60 * 24
      set delay_value to texts 1 thru ((length of delay_word) - 1) of delay_word
      
      -- delay the message
      delay delay_value * time_multiplier
      
      -- display it in large type
      display in large type future_message
   end tell
end handle_string

Code: Select all

-- written by Zettt (http://www.zettt.de) at 2009-04-28
-- modified version of http://forums.obdev.at/viewtopic.php?f=24&t=615 to work with Growl
-- this script display a growl message after a given delay time
--
-- Usage: Call LaunchBar, Hit Space, Input your message and provied your delay time at the end in "3s", "1m" or "2d" (without quotes)
-- Example: "This is an example message 2s" (without quotes)

-- take string from LaunchBar
on handle_string(message)
   set future_message to message as text
   
   -- how much delay do we need?
   set delay_word to last word of message
   
   tell application "LaunchBar"
      -- Encode message for URL; strip delay time
      do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(unicode(sys.argv[1], \"utf8\"))' " & quoted form of (texts 1 thru ((length of message) - ((length of delay_word) + 1)) of message)
      set delay_time to result
      
      -- Determine seconds, minutes, hours or days
      set time_multiplier to 1
      if last character of delay_word = "m" then set time_multiplier to 60
      if last character of delay_word = "h" then set time_multiplier to 60 * 60
      if last character of delay_word = "d" then set time_multiplier to 60 * 60 * 24
      set delay_value to texts 1 thru ((length of delay_word) - 1) of delay_word
      
   end tell
   -- delay the message
   delay delay_value * time_multiplier
   
   -- display growl message
   my growlRegister()
   my growlNotify("LaunchBar", future_message)
   
end handle_string

-- additional scripting for Growlnotification
using terms from application "GrowlHelperApp"
   on growlRegister()
      tell application "GrowlHelperApp"
         register as application "LaunchBar" all notifications {"Message from the past"} default notifications {"Message from the past"} icon of application "LaunchBar.app"
      end tell
   end growlRegister
   on growlNotify(grrTitle, grrDescription)
      tell application "GrowlHelperApp"
         notify with name "Message from the past" title grrTitle description grrDescription application name "LaunchBar"
      end tell
   end growlNotify
end using terms from


Please make sure that you create to plist files with the same name as the scripts with the following conent:

Code: Select all

{
   InputTypes =     (
       NSStringPboardType
   );
   RunInBackground = YES;
}


Future updates to the scripts will be posted here. Thanks :)

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

Re: Display delayed messages from LaunchBar

Post by ptujec » Mon May 04, 2009 1:16 pm

Thanks. Works great most of the time. There are problems with üäö .... though

norbert
Objective Development
Objective Development
Posts: 648
Joined: Thu Nov 09, 2006 6:30 pm

Re: Display delayed messages from LaunchBar

Post by norbert » Thu May 14, 2009 1:08 am

Be careful with this approach. In general, you should better avoid running AppleScripts in background, since this can cause multithreading issues, especially if the background thread runs for a long time. The "RunInBackground" plist option is therefore not officially supported.

Furthermore LaunchBar does not allow to run the same AppleScript twice at the same time. So if you've already scheduled such a delayed message, you won't be able to schedule another one before the first one has completed.

However, we've planned to extend the "display in large type" scripting command with optional "after delay" and "with sound" parameters, so you will then be able to implement such an action in a more reliable manner.

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

Re: Display delayed messages from LaunchBar

Post by ptujec » Thu May 14, 2009 6:58 am

norbert wrote:However, we've planned to extend the "display in large type" scripting command with optional "after delay" and "with sound" parameters, so you will then be able to implement such an action in a more reliable manner.


Sounds good. Thanks for letting us know. Thanks for keeping LB stable also.
I experienced another issue with the current script. I can't control iTunes while the script is running.

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

Re: Display delayed messages from LaunchBar

Post by ptujec » Sat May 30, 2009 11:25 pm

norbert wrote:However, we've planned to extend the "display in large type" scripting command with optional "after delay" and "with sound" parameters, so you will then be able to implement such an action in a more reliable manner.


Did any one try this? RC2 has this option now, but I can't get it working.

norbert
Objective Development
Objective Development
Posts: 648
Joined: Thu Nov 09, 2006 6:30 pm

Re: Display delayed messages from LaunchBar

Post by norbert » Sun May 31, 2009 10:00 am

ptujec wrote:Did any one try this? RC2 has this option now, but I can't get it working.


Here are a few examples:

Code: Select all

display in large type "Hello world!" after delay 10
display in large type "Hello world!" after delay "10s"
display in large type "Hello world!" after delay "10sec"
display in large type "Hello world!" after delay "10 s"
display in large type "Hello world!" after delay "10 sec"

display in large type "Hello world!" after delay "2m"
display in large type "Hello world!" after delay "2min"
display in large type "Hello world!" after delay "2 min"
display in large type "Hello world!" after delay "2 minutes"

display in large type "Hello world!" after delay "1.5h"
display in large type "Hello world!" after delay "1.5 h"
display in large type "Hello world!" after delay "1.5 hours"

display in large type "Hello world!" with sound "Glass"
display in large type "Hello world!" with sound "Ping"

display in large type "Hello world!" after delay "5s" with sound "Submarine" using font named "Marker Felt"

ape
Rank 1
Rank 1
Posts: 32
Joined: Thu Jul 10, 2008 12:15 am
Location: Aloha, OR USA

Re: Display delayed messages from LaunchBar

Post by ape » Sun May 31, 2009 4:59 pm

Okay, I'll be the guinea pig here and probably look like an idiot...

If my script looks like this all works fine:


tell application "LaunchBar"
display in large type "My Message" after delay 2
end tell


If instead my script wants to take in text from the user, like so:

on handle_string(message)
tell application "LaunchBar"
display in large type message after delay 2
end tell
end handle_string

Nothing ever happens. What am I doing wrong?

norbert
Objective Development
Objective Development
Posts: 648
Joined: Thu Nov 09, 2006 6:30 pm

Re: Display delayed messages from LaunchBar

Post by norbert » Sun May 31, 2009 6:43 pm

ape wrote:If instead my script wants to take in text from the user, like so:

Code: Select all

on handle_string(message)
   tell application "LaunchBar"
      display in large type message after delay 2
   end tell
end handle_string

Nothing ever happens. What am I doing wrong?

I don't know. Your script works perfectly fine here. Anyone else who has problems with this scipt?

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

Re: Display delayed messages from LaunchBar

Post by ptujec » Sun May 31, 2009 7:07 pm

Now I figured out how to do it. Here is the script:
http://is.gd/KOET
Last edited by ptujec on Sun May 31, 2009 8:22 pm, edited 4 times in total.

ape
Rank 1
Rank 1
Posts: 32
Joined: Thu Jul 10, 2008 12:15 am
Location: Aloha, OR USA

Re: Display delayed messages from LaunchBar

Post by ape » Sun May 31, 2009 7:16 pm

I figured out the problem. I had originally been storing the script under ~/Library/Application Support/LaunchBar/Actions. Once I moved the script to ~/Library/Scripts/LaunchBar, it works as expected.

Given this tidbit, can someone explain to me the difference between the two locations and what would make a script work in one and not the other?

Thanks,

Steve

norbert
Objective Development
Objective Development
Posts: 648
Joined: Thu Nov 09, 2006 6:30 pm

Re: Display delayed messages from LaunchBar

Post by norbert » Sun May 31, 2009 7:44 pm

ape wrote:I figured out the problem. I had originally been storing the script under ~/Library/Application Support/LaunchBar/Actions. Once I moved the script to ~/Library/Scripts/LaunchBar, it works as expected.

Given this tidbit, can someone explain to me the difference between the two locations and what would make a script work in one and not the other?

It should work in both locations (and it actually does here). I have still no idea why it doesn't in your case. Is there any error message in the Console?

ape
Rank 1
Rank 1
Posts: 32
Joined: Thu Jul 10, 2008 12:15 am
Location: Aloha, OR USA

Re: Display delayed messages from LaunchBar

Post by ape » Sun May 31, 2009 7:48 pm

I know why this didn't work... When testing the script that started this thread, I'd created the accompanying .plist file. When I edited the script down to support the new 'after delay' feature, I forgot that the .plist file was there. Once I removed the .plist file the script works as expected.

Sorry for the trouble, but thanks for pointing me in the right direction. :D

Steve

Post Reply