OSX Lion – How to Run bash_profile Function from Desktop Alias .command File

command linemacososx lionosx-snow-leopardterminal

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

Best Answer

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.

Related Question