Running shell commands in ITerm2 without tab closing after completion - shell

Probably a minor syntax issue that I'm getting wrong, but can't find the solution in the ITerm2 documentation. I'd like to create an applescript that opens an ITerm window with three tabs, each running various shell commands (ls, cd, echo, etc.) with the tab the remaining open after those commands have run. The opening tabs part is working fine, but it appears that as soon as the commands run, the tab closes (if I don't provide any commands, the tab will remain open.) For my script here:
tell application "iTerm2"
create window with default profile
tell current window
create tab with default profile command "echo abc"
create tab with default profile
end tell
end tell
Instead of "echo abc" what should I put there so the echo command will run in the tab, but leave me with a cursor for me to type in more commands instead of the tab immediately closing thereafter?

Instead of using the create tab ... command, use a separate write text command. For example, this is a script I use to open a terminal to a specific directory:
tell application "iTerm"
create window with default profile
tell current session of current window
write text "cd " & directory & "; clear"
end tell
end tell

Using the "write text" suggested by whereswalden I settled on the following, works well:
tell application "iTerm2"
create window with default profile
tell current window
tell current session
write text "echo abc"
end tell
create tab with default profile
tell current session
write text "ls -la"
end tell
create tab with default profile
tell current session
write text "cd mydir"
end tell
end tell
end tell

Related

Applescript does not run user command

I need the applescript to run tabset test command to change the name of the current tab of iTerm.
-- Launch iTerm and log into multiple servers using SSH
tell application "iTerm"
activate
create window with default profile
set newWindow to (create window with default profile)
set Servers to paragraphs of (do shell script "/bin/cat $HOME/serverlist")
repeat with nextLine in Servers
if length of nextLine is greater than 0 then
tell current window
create tab with default profile
tell current session of newWindow
do shell script "export PATH='/usr/local/bin:$PATH'; tabset test "
end tell
end tell
end if
end repeat
tell first tab of current window
close
end tell
tell second window
close
end tell
end tell
The problem is tabset test does not work, and there is no any error prompted.
The tabset command could be installed via npm install -g iterm2-tab-set
Ted Wrigley is right. I should use iTerm's write command to type the text into the window.

Alfred Workflow to open a file in vim in the terminal

How can we write an alfred workflow to open a file from finder to a vim editor?
This is my first attempt to create a workflow in Alfred.
I am still learning and wanted to start with a simple workflow.
Here, I first created Hotkey and Keyword.
Open the Alfred3
Go to Workflows
Click + sign at the bottom left sidebar
Choose Blak Workflow
Name: Vim Launcher
Description: Opens a file in the terminal inside vim editor
1. Right-click > Trigger > press cmd-alt-v
2. Right-click > Inputs > Keyword > open in vim and title also open in vim
3. Right-click > Actions > Run NSAppleScipt
The copy and paste following contents inside Run NSAppleScipt
on alfred_script(q)
tell application "Terminal"
activate
do script "vim '" & q & "'"
end tell
end alfred_script
This workflow works but opens two terminals instead of one.
How can we fix this make so that it opens only one window?
The workflow summary is given below.
You want a Terminal Command action.
Just connect this object to your triggers and use vim "{query}" on it.
EDIT:
I'm editing the answer to reply a comment asking a follow-up question:
The workflow now create new termnial for each file, can we make it open in the same terminal if a terminal is already running?
In this case the Run NSAppleScript object could be used with the difference that we have to specify a location where do script should run, since it always open a new window if no location is specified:
on alfred_script(q)
tell application "Terminal"
if not (exists window 1) then reopen
activate
do script "vim '" & q & "'" in window 1
end tell
end alfred_script

Script that launches commands on two Terminals

I need to do a .command file script/batch. Launching it (double-click) it has to to those things:
Open a terminal window (A)
Launching a command that open the folder where the file is (maybe this cd "dirname "$0"")
Launch a command
Open a terminal window (B)
Launching same command at point 2
Launch a command
Given that you explicitly want to create terminal windows, consider creating an application using AppleScript:
Open Script Editor (up to 10.9, AppleScript Editor)
Paste the code below.
Save as an application (via the pop-up list in the Save As dialog) to the desired folder.
# Determine the folder in which this app is located.
set thisFolder to do shell script "dirname " & quoted form of POSIX path of (path to me)
# Sample commands to execute in the new windows.
set cmds to {"date", "echo $$"}
tell application "Terminal"
# Create 2 new windows, change to the
# this app's folder, and execute the respective command.
repeat with i from 1 to 2
do script "cd " & quoted form of thisFolder & "; " & item i of cmds
end repeat
# Activate Terminal.app
activate
end tell
The reason that I recommend using an application over a *.command file is that the latter would itself open in a Terminal window first, before creating the desired windows, which is visually disruptive (and, depending on your Terminal.app preferences, may leave the extra window open).
Alternatively, you could turn that into a virtue and use the *.command file's own window as your 1st terminal window, and only create one additional one.

Open multiple tabs in iTerm2 with specific directories

I would like to be able execute an AppleScript command (from a file) that will open up new tabs for specific directories.
What would be the best way to do this?
Right now I have a node.js script that I loop through each dir and pass the dir to this AppleScript file:
on run arg
set p to arg's first item
set g to "cd " & p & "; clear; pwd"
tell application "iTerm"
make new terminal
tell the current terminal
activate current session
launch session "Default Session"
tell the last session to write text g
end tell
end tell
end run
However, that is not doing what I like (it opens the right amount of tabs, but the last one gets everything written to it).
BONUS: if you can show me how to make the original tab active after opening all the tabs.
tell application "iTerm"
if exists current terminal then
set t to current terminal
else
set t to make new terminal
end if
tell (launch session "Default Session") of t to write text "cd /etc;clear;pwd"
tell (launch session "Default Session") of t to write text "cd /var;clear;pwd"
activate
end tell

Close Terminal window from within shell script (Unix)?

Is there a way to close a Terminal window from within a shell script? I have a .command file that should just get out of the way once it's done.
Using exit 0 will cleanly terminate the script.
Whether Terminal window stays open is user-configurable. The default is to always stay open. To change this:
Terminal.app > Preferences > Profiles > Shell
- "When the shell exists:"
> Close if the shell exited cleanly
- "Ask before closing:"
(•) Never
-- OR --
(•) Only if there are....
When "Close if shell exited cleanly" is used, the script will close the window if the exit result is 0, which is the default if nothing went wrong.
Since you don't want to delete all Terminal windows, first change the name of your window from "Terminal" to something else:
echo -n -e "\033]0;My Window Name\007"
Then at the end of the script, use:
osascript -e 'tell application "Terminal" to close (every window whose name contains "My Window Name")' &
You can use apple script to quit the terminal app. Add the following to your script -
osascript -e 'tell application "Terminal" to quit'
This will give you a popup confirming to close the app. You can disable this in Terminal preferences.
Alternatively, you can also use killall command to quit the app. The following would work just as well.
killall Terminal
Note:
Just as a side note, you can freely add the above commands to your script and it would work as you want. However, there are few caveats. First being you will limit the ability of your script to work on different boxes. Secondly, it would be safer to use nohup so that any commands that are currently running won't quit due to quitting of the Terminal app.
This works for me:
#!/bin/sh
{your script here}
osascript -e 'tell application "Terminal" to close (every window whose name contains ".command")' &
exit
This will work for closing just your windows opened with a .command file but leave things already running in other terminal windows. I know that I almost always have either sass or grunt watch something so I don't want to quit terminal totally.
closeWindow() {
/usr/bin/osascript << _OSACLOSE_
tell application "Terminal"
close (every window whose name contains "YourScriptName")
end tell
delay 0.3
tell application "System Events" to click UI element "Close" of sheet 1 of window 1 of application process "Terminal"
_OSACLOSE_
}
This will close the Terminal window for your script and keep any other Terminal windows open as long as their window titles don't match. For it to work Terminal will have to be added to the list of applications permitted to use the Accessibility framework. You can also cycle through Terminal windows with a repeat command and close every window x that contains a UI element "Close" on sheet 1.
I find the best solution for this is to use Automator to create a true OSX application which will work the same way regardless of how your system is configured. You can have the Automator run your shell script, or you can embed the shell script itself in Automator.
Here is how you do it:
Run Automator (in Applications).
Choose "New Document" and when it
asks "Choose a type for your document" choose "Application"
In the
left panel, select "Utilities" then "Run Shell Script".
Type in your
script commands in the workflow item in the right panel. You can either call another
shell script, or just put your commands in their directly.
Save the
Application, which will be a full-fledged Mac App. You can even
cut-and-paste icons from other apps to give your script some
personality.
#!/bin/bash -x
{your script here}
. exit 0
kill -9 $PPID
you can also create a shortcut for your script:
cp yourscript.sh ~/bin/yourshortcutnamewhateveryouwant
then type
yourshortcutnamewhateveryouwant
will run whatever is writen into script at any directory.

Resources