Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
- 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 Mar 5 '12 at 23:17
    
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 Mar 6 '12 at 8:04
    
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 Mar 6 '12 at 15:43
    
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 Mar 6 '12 at 15:47
    
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 Mar 8 '12 at 5:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.