How to run bash_profile function from desktop alias .command file on OSX Lion?

0

1

Ok, so I have this great little function in my bash_profile which works great when I call it from Terminal (it opens a new firefox window instead of new tab):

    function firefoxNewWindow() {
    /usr/bin/env osascript <<-EOF
    tell application "System Events"
        if (name of processes) contains "Firefox" then
            tell application "Firefox" to activate
            keystroke "n" using command down
        else
            tell application "Firefox" to activate
        end if
    end tell
    EOF
    }

Now I create a text file and place it on my OSX Lion desktop with a .command extension (firefoxNewWindow.command), and put this text in it:

    firefoxNewWindow

I then make sure it is executable by doing this:

    chmod a+x desktop/firefoxNewWindow.command

Now when I double-click the command file on the desktop to run it I get this error:

    ~ >/Users/myname/Desktop/firefoxNewWindow.command ; exit;
    /Users/myname/Desktop/firefoxNewWindow.command: line 1: firefoxNewWindow: command not found
    logout

So why am I getting a Command Not Found when the function is in the bash_profile and it works from Terminal prompt? Since this is a function in bash_profile I am assuming that no path needs to be specified as it should run from any path. Any ideas on how to make this work?

Thanks

jsherk

Posted 2012-03-05T22:15:53.493

Reputation: 461

Answers

2

I don't know why functions aren't available here, even though ~/.bash_profile is sourced. You can test this by putting another command into your .bash_profile, and seeing it executed when you run the .command file.


If all you really want is to have a double-clickable item that launches this function, open up Automator.app from Applications/Utilities, and create a new Application.

Drag Run AppleScript from the left pane, and paste the AppleScript command.

Save it as a file, e.g. Open Firefox.app, wherever you want.

Of course, you can also use Run Shell Script instead of Run AppleScript if you need just plain shell script commands.

slhck

Posted 2012-03-05T22:15:53.493

Reputation: 182 472

>

  • If I create a text file with something simple like ls in it and then add .command extension and make it executable, then it will run fine when I double-click it.
  • What other command can I put in bash_profile? Since all the commands are already available at Terminal prompt, not sure I understand what I am supposed to do.
  • I will try automator idea and see how that works. Thanks
  • < – jsherk – 2012-03-05T23:17:36.980

    What I mean is, for example, put the command env into your ./bash_profile file. You can see the environment variables being listed when you call your custom .command file – just as a proof ./bash_profile is being sourced. I don't know why functions aren't available though. – slhck – 2012-03-06T08:04:54.753

    Ok I see... when I add env to bash_profile and run source and then run the .command file from desktop, it does indeed display/run the env command just fine but when it gets to the function it still says command not found. – jsherk – 2012-03-06T15:43:47.893

    Exactly. That's what I don't get. But if you just want to achieve the Firefox thing, the Automator action will just work fine. I use similar stuff all the time. – slhck – 2012-03-06T15:47:21.330

    I did the automator thing and that works great! To summarize: Start Automator, choose new Application and then drag the Run Applescript Action over to the right hand side and then copy and paste your applescript into it and save it as application. You now have an application that runs just as fast as the original applescript, and you can also create an alias on your desktop to it. – jsherk – 2012-03-08T05:43:57.230