Open multiple tabs in iTerm2 with specific directories - applescript

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

Related

reload Terminal preferences .plist after change

I used plutil to edit the plist. then I placed in under /User/<myUsername>/Library/Preferences/com.apple.Terminal.plist. I want it to update the settings without reboot the application. How do I do this? using defaults read com.apple.Terminal doesn't seem to make the changes take effect. I added a terminal profile to the plist and want the changes to take effect.
I tried making an import profile with AppleScript but long story short it's not 100% possible. if you store window 1 so it's the same var in-between delays right after open it errors more times.
on run argv
set importScript to first item of argv
set flag to application "Terminal" is not running
tell application "Terminal"
open importScript
delay 1.0E-5
activate
if flag then close back window
do script "exit" in window 1
delay 0.5
close window 1
end tell
end run
there is no need for editing plists just a simple AppleScript. the limitations of this are you must have _oti_<yourProfileId> in your terminal profile default header and change the title when running your terminal profile to prevent accidental closures when installing the same profile of an already running app
import.applescript
on run argv
set importScript to first item of argv
set closeScript to second item of argv
set profileId to third item of argv
do shell script "open -a Terminal " & importScript
do shell script "osascript " & closeScript & " _oti_" & profileId
end run
closeMe.applescript
on run argv
set c to first item in argv
tell application "Terminal"
set wList to every window
repeat with app_window in wList
set wname to name of app_window
if wname contains c then
do script "exit" in app_window
delay 0.312
close app_window
end if
end repeat
end tell
end run
and then you can call the profile using this AppleScript
on run argv
set flag to application "Terminal" is not running
set scpt to first item in argv
set n to second item in argv
set p to third item in argv
tell application "Terminal"
set newTab to do script scpt
set badFlag to back window is equal to window 1
if p is not equal to "" then set current settings of newTab to settings set p
set custom title of newTab to n
activate
if flag and (not badFlag) then
do script "exit" in back window
delay 0.1
close back window
end if
end tell
end run

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.

How to close terminal window launched using Apple Script in Mac app

I have a Mac application.
This app is executing following apple script using executeAndReturnError method of NSAppleScript class:
set tempDir to path to temporary items as string
set tempFilename to tempDir & "Test_Log.txt"
set tempFilePath to POSIX path of file tempFilename
set wasRunning to application "Terminal"'s running
tell application "Terminal"
if not wasRunning then
close front window saving no
end if
set mytab to do script "ls -l" & quoted form of tempFilePath
set title displays custom title of current settings of mytab to true
set custom title of current settings of mytab to "Test Log"
repeat until busy of mytab is false
delay 1
end repeat
if ((count of windows) = 1) then
quit
else
close (every window whose name contains "Test Log")
end if
end tell
When above apple script is executed by my app for the first time, terminal window remains open.
When above apple script gets executed subsequently, terminal window is closed (as expected).
Can anyone please assist me on how can I close the terminal window always.
Please note that, I want to close the terminal window that has been opened by my apple script mentioned above.
If there is already other terminal window opened (manually by user) before running above apple script, then this manually opened window should not be closed.

Running shell commands in ITerm2 without tab closing after completion

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

Get iTerm location Applescript

I would like to know how to get the current location of the current session in iTerm using Applescript.
I have tried all sorts of things. The dictionary is very difficult for me to understand.
Here is my latest attempt
tell application "iTerm"
tell the current session of current terminal
set theSelection to attachment file name
display dialog theSelection
end tell
end tell
And the error this produces is:
error "iTerm got an error: Can’t get file name of current session of current terminal." number -1728 from file name of current session of current terminal
I just want the location the iTerm session is currently in: /Users/SuperCoolGuy/Desktop
This is ugly...
tell application "iTerm"
if not (exists terminal 1) then reopen
set mySession to current terminal's current session
tell mySession
write text "pwd"
set sessionContents to it's text
end tell
end tell
set myPath to paragraph -2 of (do shell script "grep . <<< " & quoted form of sessionContents)
If somebody stumbles upon this question, there's another option now with Shell integration. With it you can access the path in AppleScript using the iTerm defined variable session.path:
activate application "iTerm"
tell application "iTerm"
tell current session of current window
set myTerm to (variable named "session.path")
write text "echo this is the current path: " & myTerm
end tell
end tell
That information isn't currently available through iTerm's AppleScript interface. The only exposed properties of the session object having to do with what's running in it (as opposed to its visual appearance) are contents and tty, and those aren't what you're looking for here.

Resources