Page 1 of 2

cannot run a unix executable

Posted: Thu Feb 05, 2009 10:29 pm
by bigcloits
As per the LB docs, I have a proper shebang line and execute permission is set. Script runs great in Terminal. But when I launch it from LB, the file just opens in BBEdit. How can I convince LB that this script is to be executed?

Posted: Fri Feb 06, 2009 12:53 am
by Dennis
You know, I could have sworn I saw the same problem in some testing a few weeks ago but just assumed I was doing something wrong. Maybe we're on to something.

Do you have execution privileges set for the script? You must if it's running fine in Terminal. So that's probably not it.

Ultimately, I just called my shell script with AppleScript's "do shell script" command. Still, somebody should probably report this.

-Dennis

Posted: Fri Feb 06, 2009 12:56 am
by bigcloits
Dennis wrote:Do you have execution privileges set for the script? You must if it's running fine in Terminal. So that's probably not it.


Exactly. Execution privileges definitely set.

Posted: Fri Feb 06, 2009 8:42 am
by kastorff
Right-click on the script and see what application the Finder sees as the default. If Terminal is indicated as the default app, try zapping the file creator metadata with something like Path Finder.

Posted: Fri Feb 06, 2009 4:55 pm
by bigcloits
kastorff wrote:Right-click on the script and see what application the Finder sees as the default.


Thanks for the suggestion. "BBEdit text document" is the kind, and BBEdit is the default app.

Posted: Sat Feb 07, 2009 12:19 am
by kastorff
bigcloits wrote:
kastorff wrote:Right-click on the script and see what application the Finder sees as the default.


Thanks for the suggestion. "BBEdit text document" is the kind, and BBEdit is the default app.
We'd definitely want Terminal to be the default app for a UNIX executable for this to work like you need it to. Try making a copy of the script and see if the Finder will recognize the metadata properly. I've had issues before with things just "sticking".

Posted: Sat Feb 07, 2009 6:42 pm
by bigcloits
kastorff wrote:We'd definitely want Terminal to be the default app for a UNIX executable for this to work like you need it to. Try making a copy of the script and see if the Finder will recognize the metadata properly. I've had issues before with things just "sticking".


I'm not sure what you mean that we want the Terminal to be the default app. If you write a script in BBEdit (or any text editor), how and why would the script be anything but a file of type "BBEdit", and why would it matter? Scripts are text files, not "Terminal" files, and they do not and should not require a file-type association with the Terminal to run.

I write scripts in BBEdit all the time, and the only requirement to run them is that they have their execution permission set. The problem is here seems to simply be that LB isn't recognizing the script as an executable -- instead it's just opening the source file.

Posted: Sat Feb 07, 2009 9:24 pm
by kastorff
bigcloits wrote:
kastorff wrote:We'd definitely want Terminal to be the default app for a UNIX executable for this to work like you need it to. Try making a copy of the script and see if the Finder will recognize the metadata properly. I've had issues before with things just "sticking".


I'm not sure what you mean that we want the Terminal to be the default app. If you write a script in BBEdit (or any text editor), how and why would the script be anything but a file of type "BBEdit", and why would it matter? Scripts are text files, not "Terminal" files, and they do not and should not require a file-type association with the Terminal to run.

I write scripts in BBEdit all the time, and the only requirement to run them is that they have their execution permission set. The problem is here seems to simply be that LB isn't recognizing the script as an executable -- instead it's just opening the source file.

Of course they don't require that the creator be set to run properly in the Terminal as long as the execute property is set. But your problem is that LaunchBar is opening the file with the default application, which as you've said, is BBEdit. If the default app for your script was Terminal, it would run like you want it to. I'm not saying there's not some issue with LaunchBar, but it's behavior is clear, and the solution is for your script's default app to be set to Terminal. I'm just trying to help. If you don't want my help, that's okay.

Posted: Sat Feb 07, 2009 10:49 pm
by Dennis
Yeah, setting type and creator seems like an odd requirement to me as well. Yesterday, I tried setting a simple Ruby script to open with Terminal (using the Finder's Get Info window). Running it from LaunchBar or the Finder results in Terminal opening and running the script. Is that the expected behavior? I'd prefer it to just run without opening Terminal.

I remember reading that you can run shell command in the Finder by adding a .command filename extension. I've never tried it in LaunchBar. Does it help? Unfortunately, I don't have the time to test it myself right now.

-Dennis

Posted: Sun Feb 08, 2009 7:28 am
by kastorff
Dennis wrote:Running it from LaunchBar or the Finder results in Terminal opening and running the script. Is that the expected behavior?

IME, yes. According to the LaunchBar docs, we should be able to run a UNIX executable "invisibly", or in the background, by using Option-Return to launch the selected script in LaunchBar.

Posted: Sun Feb 08, 2009 8:37 pm
by bigcloits
kastorff wrote:If the default app for your script was Terminal, it would run like you want it to. I'm not saying there's not some issue with LaunchBar, but it's behavior is clear, and the solution is for your script's default app to be set to Terminal.


Interesting point. But I actually don't want Terminal to open and run the script in the foreground, just as Dennis mentioned. I definitely need the script to run in the background, or all of this is a moot point for me. Background execution is pretty much my goal. :-)

Dennis wrote:I remember reading that you can run shell command in the Finder by adding a .command filename extension. I've never tried it in LaunchBar. Does it help? Unfortunately, I don't have the time to test it myself right now.


Yes, the .command extension allows a script to be executed from the Finder, but I think this is more or less equivalent with setting "open with" to Terminal (i.e. the default application for .command files is the Terminal), and doesn't seem to have any effect on LB's recognition of the script as an executable.

I speculate that the key here is that LB has to recognize the script as an executable so that it can execute it instead of opening it, including the option of executing it in the background. That recognition just doesn't seem to be there, regardless of the extension or "open with" association. Any objections to that summary?

Posted: Sun Feb 08, 2009 9:06 pm
by Dennis
bigcloits wrote:I speculate that the key here is that LB has to recognize the script as an executable so that it can execute it instead of opening it, including the option of executing it in the background. That recognition just doesn't seem to be there, regardless of the extension or "open with" association. Any objections to that summary?


That sounds like a pretty accurate assessment to me, at least according to my understanding of what is (or is not) written in the user manual. There's no mention of assigning the file to be opened with Terminal or appending the .command extension. In fact, the implication seems to be that a proper shebang line and setting the execution bit should be sufficient:

Preparing Shell Scripts

LaunchBar must be able to distinguish a Shell Script from an ordinary plain text file. First, make sure that your script contains a proper shebang line:

Code: Select all

#!/usr/bin/ruby
puts 'Hello world'


Further, the script must have execute permissions set to make LaunchBar recognize it as a Unix Executable:

Code: Select all

chmod +x MyHelloWorldScript




kastorff, your suggestion is a decent workaround and your help is appreciated! But I think it'd be much better if LaunchBar was able to recognize the script for what it is and just execute it, as can be done with AppleScripts and Automator Workflows.

-Dennis

Posted: Mon Feb 09, 2009 12:54 am
by kastorff
I absolutely agree that it would be nice in this case if LaunchBar would use the executable bit to determine if it should honor the creator code. What I'm not sure about is what repercussions this might have for "normal" behavior. Say I want to open a text file in BBEdit for editing. Right now, I just select the file, and open it with LaunchBar. LaunchBar opens the text file for editing. Now if that text file has the executable bit set, how does LaunchBar determine if I want to run it or edit it? It simply might be one of those user interface issues where more common behavior drives the application's default way of handling things.

Back when I encountered this issue originally, I did some research (LaunchBar 4 at the time). Since LaunchBar can run UNIX commands in the /bin directory, I examined the file info on the ls command, and compared it to the working shell scripts I'd written in BBEdit. LaunchBar would only run the scripts if the Type and Creator codes were cleared (which, with the combination of the set executable bit, seems to tell the Finder the Terminal is the default app), and the executable bit is set. Otherwise, it tries to open the file as a document, using the Type as TEXT, and/or the creator as R*ch (BBEdit). I guess I brute forced a solution by matching the file settings of the UNIX commands.

I do have my completed scripts in a ~/bin directory in my PATH and I've added that directory as a Custom location in the Index. I wonder if that has any impact on how LaunchBar behaves.

Posted: Mon Feb 09, 2009 6:49 pm
by norbert
We cannot rely on a file's execute permissions to determine if it's executable or not. Quite often a regular document may have execute permissions set, although it isn't actually executable.

For example, when you copy a file from a DOS formatted USB memory stick you get a file with execute permissions.

Just try the following command:

Code: Select all

find ~ -type f -perm +111 -ls

It searches your home directory for all plain files that have execute permissions set. Most likely it will reveal quite a lot of "executables" that are actually ordinary documents.

To prevent the erroneous execution of such invalid executables we must rely on the type information provided by Mac OS X LaunchServices (i.e. the "Kind" information shown in Finder's Get Info window).

If we get the information "this is a JPEG image", we treat it as a JPEG image, even if it has execute permissions. Same for any kind of text documents.

Only if Finder shows a file as a "Unix Executable File" we can safely assume that it is indeed executable.

Posted: Mon Feb 09, 2009 9:37 pm
by Dennis
norbert wrote:Only if Finder shows a file as a "Unix Executable File" we can safely assume that it is indeed executable.


To be clear then, kastorff's suggestion is the correct approach, right? That is, set the "Open With" app to Terminal and make sure the file is executable.

For my test on a Ruby script, I had to remove the .rb extension as well to get the Finder to recognize it as Kind: Unix Executable.

-Dennis