Can LB inject the stdout of a shell script like a snippet

Sharing and discussing custom actions for LaunchBar
Post Reply
comety
Posts: 18
Joined: Tue Aug 19, 2014 10:47 pm

Can LB inject the stdout of a shell script like a snippet

Post by comety » Thu Aug 21, 2014 5:15 pm

I have read the page on unix executables more times than I want to admin. Right now, in an app called Keybord Maestro, I have shell script that is bound to the key command "..date", any time I type that it is replaced with 08/21/14 — 07:57:44 AM. I use it all the time to time stamp a file I am working on.

I want to use LB for this instead, so I made a simple shell script. Right now it lives in ~/Library/Application\ Support/LaunchBar/Actions/ and I am calling it `datey`

That is a goof place to start, does LB care about syntax, or is there some magic that goes on where it is just piping date from the command in and out. For example, I can do this:
echo `date` or the much more POSIX war echo $("date")

Here are the contents of `datey`

Code: Select all

#!/bin/sh
/bin/echo $(/bin/date "+%D - %r")


It shows up in the index, but when I call it nothing happens. I have looked everywhere for log, and can't figure out where LB logs to, does anyone know, or know how to create a repeatable error so I can look for the log that lists that error?

I have the permissions coronet: -rwxr-xr-x 1 me staff 43 Aug 20 17:48 datey\

Running it from the command line works fine:
$./datey
08/21/14 - 08:04:49 AM

That leads me to a question. Commands that need rot privs, how do we deal with that in LB aside from making a mess of Applescript and adding the "with administrator privileges".

So now I am stumped, I want to know if this script is even running when LB asks it to. I add in a touch ~/Desktop/LB-ran-the-script.txt and added that as the last line of the script. Sure enough, when I ran the script from LB, a file called "LB-ran-the-script.txt" appeared on my desktop.

This tells me permissions are ok, the index is picking it up, the shell script is ok, and error free.

Is there a return code I need to enable, a preference?> Maybe I need to exit 1 or LB things the script is not finished?

Suggestions? Working shell script but i don't know how to tell the output to go where the cursor is.

comety
Posts: 18
Joined: Tue Aug 19, 2014 10:47 pm

Re: Can LB inject the stdout of a shell script like a snippe

Post by comety » Sun Aug 24, 2014 7:59 am

Aynone? An admin perhaps?

nbjahan
Rank 1
Rank 1
Posts: 34
Joined: Sat Jun 28, 2014 1:06 pm

Re: Can LB inject the stdout of a shell script like a snippe

Post by nbjahan » Sun Aug 24, 2014 8:13 am

@comety in case of simple scripts LB will just run them. To get the output you have to use something like this:

Code: Select all

#!/bin/sh
open "x-launchbar:select?string=$(/bin/date "+%D - %r")"

comety
Posts: 18
Joined: Tue Aug 19, 2014 10:47 pm

Re: Can LB inject the stdout of a shell script like a snippe

Post by comety » Sun Aug 24, 2014 8:21 am

I will give that a try within the next 15 minutes, can you break down what it is you are doing? Is this standard operating procedure or a hack? Are there docs on this method, I have never seen this before.

Actually, I just tried it, and the LB min window makes the replacement, hitting return again will show the type on screen larger. called it with command-space, the typing datey, then pressing return, option return, nothing, unable to get that date into the current document I am working on here my cursor is. Snippets work this way, but in this case, a snipped is not quite what I want to use as it won't work everywhere I want it to.

Thanks for the suggestion.

nbjahan
Rank 1
Rank 1
Posts: 34
Joined: Sat Jun 28, 2014 1:06 pm

Re: Can LB inject the stdout of a shell script like a snippe

Post by nbjahan » Sun Aug 24, 2014 8:35 am

Sorry I didn't understand the question. You want to paste the script output in the frontmost application right? Here is how I would do it:

Code: Select all

#!/bin/sh
osascript -e "tell application \"LaunchBar\" to perform action \"Paste in Frontmost Application\" with string \"$(/bin/date "+%D - %r")\"" &


by the way x-launchbar scheme is described here

nbjahan
Rank 1
Rank 1
Posts: 34
Joined: Sat Jun 28, 2014 1:06 pm

Re: Can LB inject the stdout of a shell script like a snippe

Post by nbjahan » Sun Aug 24, 2014 8:55 am

also:

Code: Select all

#!/bin/sh
osascript -e "tell application \"LaunchBar\" to paste in frontmost application \"$(/bin/date "+%D - %r")\"" &

comety
Posts: 18
Joined: Tue Aug 19, 2014 10:47 pm

Re: Can LB inject the stdout of a shell script like a snippe

Post by comety » Sun Aug 24, 2014 9:29 am

For what it's worn, this doesn't work either, I am off to try the applescript that forces a paste. Is this really how this is supposed to be, I seem to recall this just working in the past.

comety
Posts: 18
Joined: Tue Aug 19, 2014 10:47 pm

Re: Can LB inject the stdout of a shell script like a snippe

Post by comety » Sun Aug 24, 2014 9:33 am

nbjahan wrote:also:

Code: Select all

#!/bin/sh
osascript -e "tell application \"LaunchBar\" to paste in frontmost application \"$(/bin/date "+%D - %r")\"" &


Would you mind firing up script editor and testing that one out, it is a escaping issue, and I am stumped on what is going on. Right now, Script editor, just trying to run/compile it, and it tells me that """" can't be done.

Thank you.

*And to thing, this was all in an effort to take the ~100 LB actions I have made and move them to github so others can use them. This just happened to be the first one I picked and it's been 2 weeks now and I still haven't been able to post anything to github, as I'm being stubborn and won't let it rest until this simple one works.

nbjahan
Rank 1
Rank 1
Posts: 34
Joined: Sat Jun 28, 2014 1:06 pm

Re: Can LB inject the stdout of a shell script like a snippe

Post by nbjahan » Sun Aug 24, 2014 9:43 am

comety, I'm here till we resolve this.
The shell script is working for me. just run it in the shell

Code: Select all

osascript -e "tell application \"LaunchBar\" to paste in frontmost application \"$(/bin/date "+%D - %r")\"" &

but if you want to write an applescript for this you should check this out:

Code: Select all

/Applications/LaunchBar.app/Contents/Resources/Actions/Paste in Frontmost Application.lbaction/Contents/Scripts/default.scpt

Code: Select all

-- Copyright (c) 2013 Objective Development
-- http://www.obdev.at/
-- Version 1

on handle_string(_string)
   tell application "LaunchBar" to paste in frontmost application _string
end handle_string


and LaunchBar logs to /var/log/system.log check the Console.app for possible errors.

nbjahan
Rank 1
Rank 1
Posts: 34
Joined: Sat Jun 28, 2014 1:06 pm

Re: Can LB inject the stdout of a shell script like a snippe

Post by nbjahan » Sun Aug 24, 2014 10:05 am

datey.scpt

Code: Select all

set out to do shell script "/bin/date '+%D - %r'"
tell application "LaunchBar" to paste in frontmost application out

comety
Posts: 18
Joined: Tue Aug 19, 2014 10:47 pm

Re: Can LB inject the stdout of a shell script like a snippe

Post by comety » Sun Aug 24, 2014 10:33 am

nbjahan wrote:comety, I'm here till we resolve this.
The shell script is working for me. just run it in the shell

Code: Select all

osascript -e "tell application \"LaunchBar\" to paste in frontmost application \"$(/bin/date "+%D - %r")\"" &

but if you want to write an applescript for this you should check this out:

Code: Select all

/Applications/LaunchBar.app/Contents/Resources/Actions/Paste in Frontmost Application.lbaction/Contents/Scripts/default.scpt

Code: Select all

-- Copyright (c) 2013 Objective Development
-- http://www.obdev.at/
-- Version 1

on handle_string(_string)
   tell application "LaunchBar" to paste in frontmost application _string
end handle_string


and LaunchBar logs to /var/log/system.log check the Console.app for possible errors.


nbjahan, thanks for trudging through the grey water with me :)
I am running tail -f /var/log/system.log and run my script, nothing was logged. Just for sanity, I have in my script the last line, of what is now a three line script, first line, shebang, second line, a call to the `date` command with some formatting strings or -flags, and the third line, a `touch /Users/me/Desktop/LaunchBar-worked-sort-of.txt`

My script is called `datey`, running it from the command line, no issue, running it from LB, and it seems to work, as that `touch` file makes it's way to the Desktop, I should put a sleep command in that after 30 seconds it removes that file, all I need is the indicator that it ran, and I can use & to toss the script into the bg to keep it from just hanging there.

Anyway, it confirms that the script is being located, is in the index, is being called, is being ran, but who knows where the stdout is going to. I know I have other scripts that do this same thing, I just can't remember them or where they are at the moment. But I used to use it all the time, when I want to put my address into a file, I would type `addy` and poof, it was on the page. This was nothing more than a bunch of echo statements and newlines.

Just pasting in your first osascript command into an open Terminal, which I am running bash, but trying to be more POSIX complaint and my shebang will call out to sh instead, though in this case, I have reverted to how I used to do things, how I know they used to work. So, paste this into a shell:

Code: Select all

osascript -e "tell application \"LaunchBar\" to paste in frontmost application \"$(/bin/date "+%D - %r")\"" &


Intesrting…After hitting return, I get output, but I get a small stall too, here is the output:

Code: Select all

me@foonty ~ $osascript -e "tell application \"LaunchBar\" to paste in frontmost application \"$(/bin/date "+%D - %r")\"" &
[3] 1271
[2]   Done                    osascript -e "tell application \"LaunchBar\" to paste in frontmost application \"$(/bin/date "+%D - %r")\""
me@foonty ~ $08/24/14 - 01:06:23 AM


As you can see, eventually, we got the output we are looking for, yay! Not sure what the process ID's are all about, and if I keep running it, they seem to keep piling up. Are they not terminating? `jobs` lists nothing. `top`, and `ps` don't appear to show me anything though with `top` I never learned how to locate things in it unless they are using a lot of CPU, then tossing in a -u flag will nicely push it to the top of the window. In this case, I suspect there will not be enough CPU usage to gain that advantage.

Dropping the background & from your test, and it immediately returns the date formatted string.

What now? Stuff that in an AppleScript and call it a day? I hate to say it, but I'm not happy with that, it seems a kludge, and I would just you keyboard maestro to make it so there is a trigger for any time It sees the keys of "d a t e y" typed, it will run my shell script, and take the output to the cursor position, or anywhere I tell it really, and put it where it belongs. I was just trying to use LaunchBar as it seemed a nice fit, though this is getting ridiculous.

I really don't want to run this as an AppleScript, it seems an unnecessary step that should not need to be done. If the developer tells me that shell scripts can't be run directly from LaunchBar, and you must use a intermediate step to make it happen, ok, I will live with that, though that is something that I would imagine could be fixed in a dot update easily.

I may be getting a little lost in these forums, where are we at now? Running your same script in an AppleScript and then trying to call it from LB, or are we going to skip that entirely?

I just pushed the code into a file in my local actions folder, touched the parent folder to let LB know there had been a change and to re-scan the index, though I guess since I was in there I could have just updated the index, but that is the old habit I have been in. :) It worked! albeit very slow. I have a command/action now called `matey` and if I run it in a shell, as in cd path/to/where/he/is then issue ./matey ( it is already chmod +x or u+x ) it will fire back the date pretty much right away. I say "pretty much" because it is not as fast as it could be, I can feel about a half a second delay, I imagine waiting for the `osascript` part to initialize.

It actually took so long on the second time, I stopped waiting, came over to this window to type my reply to you in the forum, and magically about half way through my typing, the date was injected. I just ran it again, the LB window which I keep in the upper left, just hangs there, for minutes on end, no way to back out of it, you are stuck, waiting for it to finish. I want to run option-return, though even if that works, I am getting into territory where there is too much mental overhead to doing something that is supposed to save me time, not complicate what I am up to.

What should be try next?

nbjahan
Rank 1
Rank 1
Posts: 34
Joined: Sat Jun 28, 2014 1:06 pm

Re: Can LB inject the stdout of a shell script like a snippe

Post by nbjahan » Sun Aug 24, 2014 10:54 am

the & should be there for this action (ignore the bash output with job ids. It just happens when you run the script from terminal.)
So just put the shell script as I posted above in your action folder and you should be good to go.

comety
Posts: 18
Joined: Tue Aug 19, 2014 10:47 pm

Re: Can LB inject the stdout of a shell script like a snippe

Post by comety » Mon Aug 25, 2014 3:32 am

I have put that snippet in my actions folder, here is a screenshot of the action in my text editor:
https://dl.dropboxusercontent.com/u/340 ... 182049.png

I save that into my actions folder, and it is picked up in the index as the file is named, in this case `matey`

But as per my previous mention, it is dog slow for some reason, and I would like official reason as to why I have to use osascript, when to the best of my memory, any shell script would return it's output just fine. Am I missing an exit 0 or exit 1 or some other standard to the format to make it work right?

If I call it right now, it is 08/24/14 — 06:25:57 PM, at this point, I am ginning up, the LB window is dropped down, the command `matey` in this case is selected and waiting, stalling actually, it just now finished at 08/24/14 - 06:26:01 PM, so almost 2 minutes. What doesn't work for me. Not when I can use Keyboard Maestro and get a date pushed in, using the exact same shell script, as fast as I can type the shortcut, actually, so fast I have to add in a delay in Keyboard Maestro of a few milliseconds or the command will not have enough time to backspace through the keys I typed as a trigger.

Curious, the basic command, echo "Hello World";, can you put that in a script, give it the proper shebang, I guess #!/bin/sh followed by echo "Hello World"; and possibly an exit 0/1, add it to your actions directory, and get that to work so you can command-space, press "hw" for "hello world", and get it to input that echo into a document you are working on?

Thank for all your help.

manfred
Objective Development
Objective Development
Posts: 561
Joined: Sat Jul 31, 2010 9:47 am
Location: Vienna
Contact:

Re: Can LB inject the stdout of a shell script like a snippe

Post by manfred » Mon Aug 25, 2014 12:18 pm

First of all, maybe there is something I overlooked but LaunchBar snippets seem to the way more elegant solution for that.
This snippet should create exactly the output you are looking for:

Code: Select all

<date MM/dd/yy> - <time hh:mm:ss a>


About the shell script: I would strongly suggest to place the script in an LaunchBar Action bundle. The Info.plist of the action allows different options, like LBDebugLogEnabled or LBReturnsResult. With DebugLogEnabled, LaunchBar will log stdout as well as stderr to your system.log file, thus it will appear in Console.app. If LBReturnsResult is true, LaunchBar will try to parse the stdout and you should see it in LaunchBar after executing the script.

Placed in an LaunchBar Action this script works as expected:

Code: Select all

#!/bin/sh

d=$(/bin/date "+%D - %r")
osascript -e "tell application \"LaunchBar\" to perform action \"Copy and Paste\" with string \"$d\""

Post Reply