Applescript to open Terminal window without sourcing ~/.bash_profile - macos

I am trying to use Platypus to create an app launcher for an interactive command-line program on OSX 10.8. I want to be able to double-click on my application, and have a Terminal window open, running my program. The problem is that my Applescript, (borrowed from Octave, and adapted for Julia) launches a Terminal window and attempts to spit some commands into it, however I have a rather hefty ~/.bash_profile that interferes with this. Is there a way to get my Applescript to open a non-login shell, or not source ~/.bash_profile, etc?
Here's the script that Platypus runs:
# This is the startup procedure written as AppleScript to open a
# Terminal.app (if the Terminal.app is not already running) and start
# the Julia program.
# 20071007 removed: open -a /Applications/Utilities/Terminal.app
osascript 2>&1>/dev/null <<EOF
tell application "System Events" to set ProcessList to get name of every process
tell application "Terminal"
activate
if (ProcessList contains "Terminal") or ((count of every window) is less than 1) then
tell application "System Events" to tell process "Terminal" to keystroke "n" using command down
end if
do script ("exec bash -c \"PATH=${ROOT}/julia/bin:${PATH} OPENBLAS_NUM_THREADS=1 FONTCONFIG_PATH=${ROOT}/julia/etc/fonts GIT_EXEC_PATH=${ROOT}/julia/libexec/git-core GIT_TEMPLATE_DIR=${ROOT}/julia/share/git-core exec '${ROOT}/julia/bin/julia'\"") in front window
end tell
EOF
# Quit the Julia.application immediately after startup (ie. quitting
# it in the taskbar) because once it is started it cannot be restarted
# a second time. If Julia.app stays (eg. because of a crash) opened
# then restarting is not possible.
osascript 2>&1>/dev/null <<EOF
tell application "julia"
quit
end tell
EOF

In general, you don't need a Terminal window to execute command line stuff. You would only use the Terminal if there was information you need to manually type in by hand. So you can probably just run the command using "do shell script" instead of "do script" in a Terminal window. Note that doing it this way won't use your bash profile file. So try this command all by itself in the applescript...
do shell script ("exec bash -c \"PATH=${ROOT}/julia/bin:${PATH} OPENBLAS_NUM_THREADS=1 FONTCONFIG_PATH=${ROOT}/julia/etc/fonts GIT_EXEC_PATH=${ROOT}/julia/libexec/git-core GIT_TEMPLATE_DIR=${ROOT}/julia/share/git-core exec '${ROOT}/julia/bin/julia'\"")
Then you can add your other applescript commands as needed, just don't use the Terminal and thus your bash profile won't be used.

Related

How can I open a new z shell in a tab and run a command?

I'm on macOS which is currently using the zshell.
I would like to run a command in one shell which opens another shell preferably in another tab in the same window and then runs a given command. For example:
> openTab
would open another tab and run a basic command like ls.
Is this possible to do?
It appears that the open command will open new window, but I want it to be opened as a new tab in the current window. See here
Osascript appears a bit messy, is there way to do this natively with zshell?
You can use AppleScript, via osascript to do exactly that:
function runInNewTab() {
osascript >/dev/null <<EOF
tell application "System Events"
tell process "Terminal" to keystroke "t" using command down
end tell
tell application "Terminal" to do script "$*" in front window
EOF
}
Run it like this:
runInNewTab ls -l ~

Launch Emacsclient with GUI (from Dock) on Mac OS X

How can I launch Emacsclient with GUI from the Dock (or maybe also from the terminal) on Mac OS X?
The EmacsWiki describes how to create an "Emacs from Dock" app using Automator. It worked for me but I don't want to launch Emacs but Emacsclient. So, I tried replacing /Applications/Emacs.app/Contents/MacOS/Emacs with both /Applications/Emacs.app/Contents/MacOS/bin/emacsclient and /Applications/Emacs.app/Contents/MacOS/bin/emacsclient -c but both didn't work.
From the terminal
You can find the appropriate path to emacsclient using type in your shell (assuming emacsclient -c works from said shell):
$ type emacsclient
emacsclient is /usr/local/bin/emacsclient
Then we can add the appropriate emacsclient flags (see $ man emacsclient for details) to open the GUI:
/usr/local/bin/emacsclient -n -c -a ""
From macOS GUI
To launch emacsclient from eg the Dock or Spotlight, it's easy to use Automator. Automator is built in to macOS.
Choose to make an "Application", then choose "Run Shell Script", and add a modified version of the above call to emacsclient:
/usr/local/bin/emacsclient -n -c -a "" -- "$#"
Then change "Pass input": use "as arguments" instead of "to stdin".
The added "$#" is where any optional arguments passed to this shell script will be placed. Here, this allows you to pass a filename to open with emacsclient. Automator automates passing this filename in when, eg, you click to open a file with the application we've just made. This also allows you to set the application to be the default application for certain file types.
From anywhere, flexibly
Another way to run the above shell command is with skhd (link). skhd is far more involved to learn, but ultimately makes it much easier to set up a large number of shell commands with rapid access.
For example, you could make "Ctrl-o" from anywhere in macOS enter a mode you name open_app, from which you could press "e" to open emacsclient, "d" to open emacs --debug-init, "t" to run emacs --adv-timers, "f" to open Firefox, "F" to open a second Firefox profile, etc.
One idea would be to create an applescript that does whatever the original poster desires, and wrap it up in an application using something like platypus or automator. See https://superuser.com/questions/685111/basic-setup-of-emacs-server-under-osx for additional ideas such as using the --daemon command-line argument instead of placing (server-start) within the user-configuration file.
Here is an example applescript:
# (server-start) must be inside `init.el` or `.emacs` file.
#
# This script can also be used in the terimal: osascript path-to-script arguments
# Terminal Example:
# osascript /absolute/path/to/applescript/file "-e '(progn (dired \"/Applications\") (message \"Hello-World\!\"))'"
on run argv
set arg to item 1 of argv
set emacs to application "Emacs"
set appIsRunning to emacs is running
if appIsRunning then
say "Emacs is already running."
do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient " & arg
else
tell application "/Applications/Emacs.app/Contents/MacOS/Emacs" to activate
say "Please wait five seconds for Emacs to load."
delay 5
do shell script "/Applications/Emacs.app/Contents/MacOS/bin/emacsclient " & arg
end if
end run

AppleScript: Put text in Terminal ONLY (no execution)

I am using AppleScript to launch a certain python script. This python script uses arguments. I know how to use do script to launch commands but this time I just want to have python myscript.py ready. After that I would type my own argument and hit RETURN.
I don't know how to put the text in Terminal. Arguments will be different everytime I want to use the script, otherwise I would just use do script.
This will type your command into the current active terminal window:
tell application "Terminal" to activate
tell application "System Events" to keystroke "python myscript.py "
If you need to select which terminal window, then there will be a bit more work to do.

Mac: gnome-terminal equivalent for shell script

I am trying to run a shell script in a MAC terminal. I want to open a new terminal window that will execute the script separate from the program that is running. In Fedora, there is a gnome-terminal command which lets me execute a script in another terminal shell.
Does anyone know an equivalent on MAX OSX and how to use it?
For example say I have a script crazy.sh and I want to call this from a program that is executing but in a separate terminal from the one which is currently executing the program.
I like DigitalTrauma's answer but I found for my use, this worked better
open -a Terminal.app crazy.sh
Thanks for the answers.
One way to do it is to use an xterm instead of a terminal window:
xterm -e crazy.sh
If you want the xterm to stay open after the script completes, use the -hold option to xterm.
But if you really need to do this in a terminal, you can do it with applescript:
tell application "Terminal"
activate
tell application "System Events" to keystroke "n" using command down
repeat while contents of selected tab of window 1 starts with linefeed
delay 0.1
end repeat
do script "crazy.sh" in window 1 -- make sure the path to your script is right
end tell
(Credit to the answer here https://superuser.com/questions/466619/open-new-terminal-tab-and-execute-script)

Applescript: Keeping track of multiple terminal windows and writing into each of them

I've got a setup procedure for a project that involves me using multiple terminal windows. The start up procedure is sort of messy and involves me tabbing between the terminal windows (different tools running) and sequentially inputting commands into each terminal.
Applescript is useful for getting the first run of commands out and opening all my terminals with the:
do script "echo blablablabla"
These are decent because they open new terminal windows each time I "do script". This is actually more beneficial to me than tabbing (although, I couldn't quite figure out tabbing between terminal tabs).
However, I'd like to keep track of these windows since I need to go back to specific ones and input more commands. Any ideas?
More specifically:
Is there a way I can add an alias for each window to track it and tab back to it in the Applescript? If so, how do I implement it?
do script has an in specifier:
tell application "Terminal"
set t to do script "echo a"
do script "echo b"
do script "echo c" in t
set index of window 2 to 1
end tell
Or using tabs:
tell application "Terminal"
activate
set t to do script "echo a"
tell application "System Events" to keystroke "t" using command down
do script "echo b" in window 1
set selected tab of window 1 to t
end tell

Resources