1

I have the following Applescript in a workflow assigned to a service hotkey that opens a terminal in a given Finder window:

tell application "Finder"
    set myWin to window 1
    set thePath to (quoted form of POSIX path of (target of myWin as alias))
    tell application "Terminal"
        activate
        tell window 1
            do script "cd " & thePath
        end tell
    end tell
end tell

I would like to set an environment variable before the terminal is invoked so that when .bash_profile is sourced, an if statement checking for the existence of said environment variable passes.

I've seen methods to access environment variables, such as this, but nothing that sets environment variables for the following shell session.

I have tried to change the terminal line to do script "my_env_var=some_value; cd " & thePath, but I need it set before the session starts. I also tried to set the variable in a do script before the one that opens the shell I want to use:

do script "my_env_var=some_value"
do script "cd " & thePath

But this just opens two shells and does the same as before.

Is there a way to accomplish setting this environment variable, or perhaps determining if the terminal was invoked by this service rather than another method?

  • Is there some reason why you just don't edit the .bash_profile file directly and be done with it? – user3439894 Aug 21 '19 at 17:27
  • @user3439894 I have byobu set to open on default when a new terminal is opened, this is based on a variable that is set when .bash_profile is sourced. I want to skip that line to avoid opening the shell with byobu when I use the service to open the terminal in the given finder location. I wouldn't mind byobu being used, but all instances of byobu use the same buffer. Also, the cd gets gobbled up before byobu is activated. – Darrel Holt Aug 21 '19 at 17:33

You must log in to answer this question.

Browse other questions tagged .