OSX: How can I programatically tell if I'm running in Terminal or iTerm? - macos

I have a command line application running on OSX that would like to create several tabs in the current window using AppleScript.
How can I tell if my program is running in Terminal, iTerm, or another terminal program?

The $TERM_PROGRAM env var is set to iTerm.app or Apple_Terminal so you can determine which AppleScript cmds to run if you pass it as an arg to osascript (assuming your shelling to osascript)
Example usage:
osascript ThisScriptName.scpt $TERM_PROGRAM
Example script:
--osascript ThisScriptName.scpt $TERM_PROGRAM
on run {TermType}
if (TermType = "iTerm.app") then
-- iTerm.app :-P
tell application "iTerm"
tell current window
tell current session
set newSession to (split horizontally with default profile)
end tell
end tell
end tell
else if (TermType = "Apple_Terminal") then
-- Terminal.app
tell application "Terminal"
do script "open 'https://iterm2.com/downloads.html'" in window 1
end tell
else
-- Unknown terminal application
return "Really? Not using iTerm.app or Terminal.app"
end if
end run

osascript -e 'tell application "Finder" to get the name of every process whose visible is true'
This will a list of running applications, assuming your only running one of terminal, iTerm, ..., this will work
Do what you want with the list
Hope this helps

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 ~

Trying to automate tabs and shell commands in Terminal?

I have a custom vim setup running inside split (GNU) screen sessions running in several tabs inside Terminal. Naturally I want to automate all that. So Ive Googled a lot and most answers talk about using osascript -e to run a bunch of AppleScript commands. My situation is slightly different: first Im using TotalTerminal, a plugin for Terminal (dont think it matters but mention it just in case) and Im writing a hashbang script and not a bash script, i.e.
#!/usr/bin/osascript
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
tell application "Terminal" to activate
tell application "Terminal" to do script with command "cd ~/Desktop/Projects && screen -d -U -R -A"
which Im running from the command-line. The tab opening works but the script/command runs in a new window instead of inside a newly-created tab.
This is how I might recommend setting things up:
#!/usr/bin/osascript
tell application "System Events"
tell process "Terminal"
set frontmost to true
end tell
end tell
tell application "Terminal"
activate
tell application "System Events" to keystroke "t" using command down
do script "cd ~/Desktop/Projects && screen -d -U -R -A" in window frontmost
do script "clear; echo 'Hello, World!'" in tab 1 of window frontmost
end tell
Note: You also can select the tab you want the next command to go into by using tab x. If you switch back to the first tab you should notice the echo sent to it after creating the new tab.
The example above is a few more lines of code perhaps, although it should get all the processes correctly in order. I think the key ingredient is having Terminal set frontmost to true which gets the current Terminal window to start interacting with the rest of the script.
EDIT: The OP came back and needed to make a few changes and this was the end result:
#!/usr/bin/osascript
tell application "System Events"
tell process "Terminal"
set frontmost to true
end tell
end tell
tell application "Terminal"
activate
do script "mosh user#someserver" in window frontmost
tell application "System Events" to keystroke "t" using command down
do script "cd ~/Desktop/Projects && screen -d -U -R -A" in tab 2 of window frontmost
end tell

Use one terminal instance for many do shell scripts?

So, I need to make an AppleScript to ssh into my mint headless server. I don't need to transfer files, just run commands. The issue is, when I ssh with one do shell script, it is in a separate instance than the others. Also, bonus if I don't have to use keystrokes and keep the terminal in front.
Edit: It would help if you guys told me why you're downvoting this post.
Not sure if that is what you want but with Terminal.app you can do that:
set shellScript to "echo" & space & quoted form of "Test Message"
tell application "Terminal"
activate
try
if (exists tab 1 of window 1) then
do script with command shellScript in front window
else
do script with command shellScript
end if
on error
beep
end try
end tell
Above code executes shellScript in the front window or in a new window when no window is open. Hope it helps.
ADDITION
I think there is no AppleScript script command to hide an application.
With System Events we can do a keystroke, so let's do cmd-h which hides an application:
tell application "System Events"
tell application process "Terminal" to set frontmost to true
-- delay 0.25 -- in case it does not work
keystroke "h" using command down
end tell
I don't know how Terminal.app will behave with hiding and doing scripts but you will find out.
Or put the Terminal-Window in another Finder Space.

Applescript to open Terminal window without sourcing ~/.bash_profile

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.

After Effects (Mac) Execute a Script from Terminal

I'm trying to figure out how to execute an After Effects script from the command line.
Official documentation says:
Example (for Windows):
afterfx -r c:\script_path\example_script.jsx
And nothing for Mac. However I'm trying:
open -b com.adobe.AfterEffects --args -r /script_path/example_script.jsx
And nothing happens. The program get opened though, but it seems that the script is never called.
Anyone had figured out this?
This is actually documented here
You should now be able to use DoScriptFile (instead of DoScript) with Applescript. Something like:
tell application "Adobe After Effects CS6"
DoScriptFile path/to/file.jsx
end tell
you can also call a specific function using external parameters by mixing this method with the one mentioned by MentalFish
make an applescript [in this case called ASfile.scpt]:
on run argv
set SomeName to (POSIX file (item 1 of argv))
tell application "Adobe After Effects CS6"
DoScriptFile SomeName
DoScript item 2 of argv
end tell
end run
from python and assuming you have a 64bit machine you can call:
ae = subprocess.call('arch -x86_64 osascript /AppleScriptLocation/ASfile.scpt %s/SomeScript.jsx "SomeFunction(\'%s\')"' %( ScriptsFolder, ParameterFromPython),shell=True)
Apparently AE on Mac does not support command line execution of scripts: http://www.aenhancers.com/viewtopic.php?f=8&t=1903
This works, create an AppleScript that tells AE to run the script via the DoScript command:
set test to (POSIX file ("/Users/username/Desktop/test.jsx"))
tell application "Adobe After Effects CSYourVersionNumber"
DoScript test
end tell
... and run the script via the command line as such (to run it in 32-bit mode):
arch -i386 osascript test.applescript
If you want to pass in the parameter of what script to launch you can do it like this:
on run argv
set test to (POSIX file (item 1 of argv))
tell application "Adobe After Effects CSYourVersionNumber"
DoScript test
end tell
end run
Run the script and pass in the path to the jsx file:
arch -i386 osascript test.applescript /Users/username/Desktop/test.jsx
you could place it in the startup folder (I use windows, the folder in win is Adobe After Effects CS4/Support Files/Scripts/Startup). Scripts in that folder are executed upon AfterEffects booting up.
Might be helpful?
I would like to throw in here, some answers where great though possibly outdated.
This code:
on run argv
tell application "Adobe After Effects CC 2018"
DoScriptFile item 1 of argv
end tell
end run
For the applescript file works, the POSIX code does not.
Then running it in the terminal for Mojave works like this:
osascript AppleScriptAETest.scpt "/Users/.../Your/file/here.jsx"
This works best for me.
I got it working with this:
open -b com.adobe.AfterEffects --args /Users/[user]/Desktop/[folder]/file.aep
seems all I had to do was get rid of the 'r' flag.
Good luck!

Resources