how can i find out the current osx-terminal theme from within a bash script - macos

Inside a bash script, I need to know what the current OS X terminal theme is. How can this be done?
I check the output of the env command, but didn't find anything.

We can use an AppleScript to retrieve the name of the profile of the foremost Terminal window from within a bash shell:
echo 'tell application "Terminal" to return name of current settings of first window' | osascript
We can similarly set the profile:
echo 'tell application "Terminal" to set current settings of first window to settings set "Basic"' | osascript
Replace "Basic" with the name of the profile you wish to adopt.
These commands will apply to the current/foremost tab or window of Terminal.app
I've also written a script that will get/set the profile depending on whether a profile name is provided: https://github.com/starbase527/dotfiles/blob/master/local/bin/term-profile . Example usage:
# Gets profile name
> term-profile
Basic
# Sets profile to Basic
> term-profile Basic
>

On a per-shell basis, you can't. You can get the default setting:
defaults read com.apple.Terminal "Default Window Settings"
Or the new window setting:
defaults read com.apple.Terminal "Startup Window Settings"

Directly in zsh, you can get the current Terminal.app theme like this:
CURRENT_THEME=$(osascript -e 'tell application "Terminal" to return name of current settings of first window')
echo $CURRENT_THEME
From that, and with some AppleScript knowledge, you can get some of the details of the Terminal.app
Happy coding 🖖

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 ~

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

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

iTerm2 and tmux: open new tab in working directory of current tab

When I open a new tab (via ⌘T) on a remote shell using iTerm2 and tmux, I almost always want the new tab to have the same working directory as the current tab. The best I can do is make iTerm2 open up the new tab in the same directory in which I ran tmux -CC or tmux -CC attach. (This behavior can be configured by navigating to Preferences → Profiles → General → Working Directory.)
This directory will not necessarily be the working directory of the current tab. Is there any way to get the behavior I'm looking for? I searched online for a while but could not find any helpful information.
When using Iterm2, if you want a new tab to open in the same directory as the current tab via ⌘T, there is an option available in your profile under preferences.
From the iTerm2 main menu:
Iterm2 -> Preferences -> Profiles -> General -> Working Directory -> Reuse previous session's directory
If you're using ZSH you could try something like this;
function tab() {
local command="cd \\\"$PWD\\\"; clear; "
(( $# > 0 )) && command="${command}; $*"
}
If you're using bash I'm not sure what the equivalent would be. Also if you're using prezto or Oh-My-ZSH the tab function is already built in.
UPDATE
Having had a look at how prezto does it, this should be the full solution
local command="cd \\\"$PWD\\\""
(( $# > 0 )) && command="${command}; $*"
the_app=$(
osascript 2>/dev/null <<EOF
tell application "System Events"
name of first item of (every process whose frontmost is true)
end tell
EOF
)
[[ "$the_app" == 'iTerm' ]] && {
osascript 2>/dev/null <<EOF
tell application "iTerm"
set current_terminal to current terminal
tell current_terminal
launch session "Default Session"
set current_session to current session
tell current_session
write text "${command}"
end tell
end tell
end tell
EOF
}
It uses the CLI for AppleScript and seems to work fine for me.
With tmux, one solution is to set alias itab='open . -a iterm' in your .bash_alias.

Automate Everyday Tasks in Mac Applications with Aliases in the ~/.bash_profile

I know that you can create aliases in the ~/.bash_profile that automate bash commands like:
alias fly="ssh username#ip_address -p22"
But I was wondering if it was possible to automate tasks within applications on the Mac. So, for example, I know that you can make an alias to open the System Preferences:
alias sys="open /Applications/System\ Preferences.app/"
But how do you automate navigating within the System Preferences app? For example, I find myself opening the System Preferences, typing 'network', and then pressing enter frequently to navigate to the Network settings.
Is there a way to automate these series of steps with an alias? Or are aliases limited to bash commands?
Shell aliases are limited to shell commands. But many OS X applications can be controlled by AppleScript commands, and those can be issued with the shell command osascript. Quoting gets a little tricky, though, because your AppleScript commands often contain quotes, which then need to be wrapped in another layer of quotes in the shell command, which then need to be wrapped in another layer of quotes when you define the alias.
See macosxautomation.com for notes on using AppleScript (and this page for System Preferences specifically). To get System Preferences to show the Network pane, you could use this AppleScript:
tell application "System Preferences"
reveal pane id "com.apple.preference.network"
activate
end tell
...which can be issued with this osascript command (note that each line of the script corresponds to a -e argument, and the AppleScript double-quotes are wrapped in single-quotes for the shell):
osascript -e 'tell application "System Preferences"' -e 'reveal pane id "com.apple.preference.network"' -e 'activate' -e 'end tell'
And you could create an alias for this:
alias networkprefs="osascript -e 'tell application \"System Preferences\"' -e 'reveal pane id \"com.apple.preference.network\"' -e 'activate' -e 'end tell'"
Note that I had to escape the double-quotes that're intended for AppleScript consumption... rather confusing. I'd use a shell function instead (similar to an alias, but without the quoting weirdness):
networkprefs() {
osascript -e 'tell application "System Preferences"' -e 'reveal pane id "com.apple.preference.network"' -e 'activate' -e 'end tell'
}

open programs with applescript

2 part question:
I'm simply trying to run programs using applescript from the terminal, so I tried:
$ osascript tell application "iTunes" to activate
and get the error:
osascript: tell: No such file or directory
Giving the full path to the program did not work either. What am I missing? The second part of the question is what I eventually want to use applescript for. I would like to use it to open an application I built using py2app. Can applescript open any mac app or just certain ones that are already compatible.
Thanks
Try this. Notice you use "-e" when you are writing the command. Without "-e" you would give a path to an applescript to run. Also notice the string command must be in quotes.
osascript -e "tell application \"iTunes\" to activate"
And if you have a multi-line applescript you use "-e" before each line like this...
osascript -e "tell application \"iTunes\"" -e "activate" -e "end tell"
If you want to open an application just use the unix "open" command...
open "/path/to/application"
If you wanted to open an application using applescript and the "activate" command doesn't work (it should work for almost everything though) then tell the Finder to open it. Remember that applescript uses colon delimited paths...
osascript -e "tell application \"Finder\" to open file \"path:to:application\""
In a bash shell (like in Terminal), you can send multiple lines to osascript by using a "here document".
osascript -e "tell application \"iTunes\"" -e "activate" -e "end tell"
becomes
osascript <<EOF
tell application "iTunes"
activate
end tell
EOF
As an old-skool Unix hacker, I save these little snippets in my $HOME/bin directory and call them from the command line. Still learning the particulars, though.
Alan
an alternative to osascript:
open -a Calendar
close by:
pkill Calendar
Try:
do shell script "open /Applications/iTunes.app"
you need to put single quotes around the tell:
osascript -e 'tell app "iTunes" to activate'
otherwise you're defining a variable when you run -e
I'am new to script too.
I am confused to so I scan an essay named AppleScript Language Guide
and when I go through script commands items, I learn that if you want to activate an application in mac os with applescript editor you should type beneath code in your editor and then compile and run them! may this answer will help you, here's code:
// applescript editor code
----------
activate application "iTunes" line 1
----------
tell application "iTunes" to activate line 2

Resources