Breaking applescript to make it better - please provide feedback

703 views
Skip to first unread message

George Nachman

unread,
Aug 26, 2014, 2:08:05 AM8/26/14
to iterm2-...@googlegroups.com
Since it may be a while before Swift is usable as a scripting language, I'm cleaning up how iTerm2 implements its Applescript support. The change will become effective in the nightly builds soon. It will break backward compatibility but I think it'll make life better for users and it definitely makes the code more maintainable for me, plus it fixes some insanity.

* The application name is changing from iTerm to iTerm2, which will immediately break all existing scripts (plus maybe other stuff too?)

* The names of properties are being changed to be consistent with current terminology (for example, "session" will refer to a single terminal session, while it used to refer to a tab).

* Split pane support will be improved and things will generally make more sense because the true hierarchy of window->tabs->sessions will be exposed.

The down side is that scripts will need to be updated. Here's an example script that does basically everything that will be possible; if you see something missing that you depend on please let me know.

tell iTerm2
        -- application-level commands
create window with default profile
create window with default profile command "ls -l -R /"
select first terminal window
create window with profile "Default"
create window with profile "Default" command "ls -l -R /"

        -- window-level commands
repeat with aWindow in terminal windows
tell aWindow
tell current session
split horizontally with default profile
end tell
end tell
end repeat
tell current window
create tab with default profile
create tab with profile "Projection"
end tell
tell current window
tell current session
set columns to 40
set rows to 40
end tell
end tell

        -- tab-level commands
tell current window
tell second tab
select
end tell
tell first tab
close
end tell
                tell current tab
repeat with aSession in sessions
tell aSession
write text "Hello"
end tell
end repeat
                   end tell
end tell

        -- session-level commands
tell current session of first terminal window
write text "cat > /dev/null"
write contents of file "/etc/passwd"
write text (tty)
write text (contents)
write text (unique ID)

split vertically with default profile
split vertically with profile "Default"
split vertically with same profile
split horizontally with default profile
split horizontally with profile "Default"
split horizontally with same profile

set foreground color to {65535, 0, 0, 0} set background color to {65535, 0, 0, 0} set bold color to {65535, 0, 0, 0} set cursor color to {65535, 0, 0, 0} set cursor text color to {65535, 0, 0, 0} set selected text color to {65535, 0, 0, 0} set selection color to {65535, 0, 0, 0} set ANSI black color to {65535, 0, 0, 0} set ANSI red color to {65535, 0, 0, 0} set ANSI green color to {65535, 0, 0, 0} set ANSI yellow color to {65535, 0, 0, 0} set ANSI blue color to {65535, 0, 0, 0} set ANSI magenta color to {65535, 0, 0, 0} set ANSI cyan color to {65535, 0, 0, 0} set ANSI white color to {65535, 0, 0, 0} set ANSI bright black color to {65535, 0, 0, 0} set ANSI bright red color to {65535, 0, 0, 0} set ANSI bright green color to {65535, 0, 0, 0} set ANSI bright yellow color to {65535, 0, 0, 0} set ANSI bright blue color to {65535, 0, 0, 0} set ANSI bright magenta color to {65535, 0, 0, 0} set ANSI bright cyan color to {65535, 0, 0, 0} set ANSI bright white color to {65535, 0, 0, 0} set background image to "/usr/share/httpd/icons/small/rainbow.png" set name to "New Name" set transparency to 0.5
end tell
end tell

Michael Garrett

unread,
Aug 26, 2014, 1:35:43 PM8/26/14
to iterm2-...@googlegroups.com
I switched from iTerm to iTerm2 last week and started heavily using the tmux integrations.  I then noticed that I was getting this for all new tmux sessions:

[mgarrett@admin2 ~] tmux -CC Can't enter tmux mode: another tmux is already attached


A few google searches later and I found this page:


Which told me to call “open -na iTerm” from a terminal window to spawn a whole new iTerm session instead of another instance of the first iTerm session.  (and that would allow me to open a 2nd or Nth tmux connection)  That worked, but,,,, I open almost all of my sessions with hotkeys that rely upon AppleScript to open iTerm.  So, I could really benefit from a “-na” like chunk of AppleScript code.   I thought maybe there was a solution on this page:


But if there is one, I can’t find it.

Does anyone know how to spawn a completely new iTerm2 session from applescript?

Here is the script that I use right now to launch my new iTerm sessions, just in case that helps anyone to know what I am asking:

tell application "iTerm"
--activate


-- make a new terminal
set myterm to (make new terminal)


-- talk to the new terminal
tell myterm
-- make a new session
set mysession to (make new session at the end of sessions)


end tell


tell mysession


-- set some attributes
--set name to "bubblep"
--set foreground color to "red"
--set background color to "blue"
set transparency to "0.1"


-- execute a command
exec command "ssh -o StrictHostKeyChecking=no host.mydomain.com"


end tell -- we are done talking to the session


end tell



George Nachman

unread,
Aug 26, 2014, 1:45:28 PM8/26/14
to iterm2-...@googlegroups.com
A simpler answer is to use the nightly build, which supports multiple tmuxes at one time.

Inline image 1


--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mike Garrett

unread,
Aug 26, 2014, 2:57:25 PM8/26/14
to iterm2-...@googlegroups.com
Thank you. Works great!

On Aug 26, 2014, at 1:45 PM, "'George Nachman' via iterm2-discuss" <iterm2-...@googlegroups.com> wrote:

A simpler answer is to use the nightly build, which supports multiple tmuxes at one time.

<image.png>

George Nachman

unread,
Sep 11, 2014, 12:48:41 AM9/11/14
to iterm2-...@googlegroups.com
The nightly build has just been released with this change.

Tom Feist

unread,
Sep 11, 2014, 2:18:10 AM9/11/14
to iterm2-...@googlegroups.com
Hey,

Oops, I meant to reply earlier and then forgot. Some things I've been thinking about
since updating to the more recent branches:

I don't know if it's practical, but a way to make the hotkey window actually appear when you
'tell iterm2 to activate' would be great. Right now (Build 2.9.20140904-nightly) it switches
to it (the menu bar changes from the previous app), but doesn't focus or raise the window.


Having an 'is $x tab/session currently focused (in the "if I type, it will go there" sense,
independently of whatever OSX seems to think of as focus)/visible' would also be extremely handy.

While I'm wishing for ponies, something that would allow the hotkey window to raise/focus without
raising other non-hotkey windows in that Space/on other monitors would be super-amazing, but I think
I remember a ticket about the futility of that dream.

I don't know if it's down to my ignorance (and the fact that I try to use pyObjC/ScriptingBridge
rather than actual applescript as much as possible), but there don't seem to be any reverse accessors
on the window/tab/session hierarchy, so you can't 'get parent window of parent tab of session X' which
is occasionally useful. There's also no 'current session of current tab', although there is on the window.

For the 'unique id' thing, would it be possible to expose the same value as the "$ITERM_SESSION_ID"
environment variable? It would make it easier to coordinate between things.

A separate accessor for the hotkey window (I believe there can be only one at a time, right?) from the
top level would be useful, if not essential. Maybe some way of specifying or querying tab-relative
split positions as well. "leftmost upper session of current tab" or suchlike. An unambiguous naming/ordering
sounds challenging though. Maybe more like the current menu options for 'get session right of {session down of this}'

Those would be the key extras for me (mostly the raise/lower hotkey-win and is-visible/focused), but I'm
sure there's lots of other things. Scripting expands to fill time available, and I imagine
supporting scripting doubly so :)

Cheers,

Tom

On 26 Aug 2014, at 07:07, George Nachman <gnac...@llamas.org> wrote:

> Since it may be a while before Swift is usable as a scripting language, I'm cleaning up how iTerm2 implements its Applescript support. The change will become effective in the nightly builds soon. It will break backward compatibility but I think it'll make life better for users and it definitely makes the code more maintainable for me, plus it fixes some insanity.
>
> * The application name is changing from iTerm to iTerm2, which will immediately break all existing scripts (plus maybe other stuff too?)
>
> * The names of properties are being changed to be consistent with current terminology (for example, "session" will refer to a single terminal session, while it used to refer to a tab).
>
> * Split pane support will be improved and things will generally make more sense because the true hierarchy of window->tabs->sessions will be exposed.
>
> The down side is that scripts will need to be updated. Here's an example script that does basically everything that will be possible; if you see something missing that you depend on please let me know.
> <snip>

George Nachman

unread,
Sep 11, 2014, 1:58:29 PM9/11/14
to iterm2-...@googlegroups.com
Thanks for the suggestions, Tom. I think those are all good ideas. Would you mind filing separate bugs for each? My bug queue has exploded lately so it may be a while before I get around to it.

Tom Feist

unread,
Sep 11, 2014, 4:21:02 PM9/11/14
to iterm2-...@googlegroups.com

Ok, I've filed the interesting stuff (with a bit more thought into some of it) as #3188-#3193 (and a couple of bonus bug reports!)

--Tom

On 11 Sep 2014, at 18:58, 'George Nachman' via iterm2-discuss <iterm2-...@googlegroups.com> wrote:

> Thanks for the suggestions, Tom. I think those are all good ideas. Would you mind filing separate bugs for each? My bug queue has exploded lately so it may be a while before I get around to it.
>
> On Wed, Sep 10, 2014 at 11:18 PM, Tom Feist <sha...@gmail.com> wrote:
> Hey,
>
> Oops, I meant to reply earlier and then forgot. Some things I've been thinking about
> since updating to the more recent branches:
...


Michael Garrett

unread,
Sep 26, 2014, 2:53:16 PM9/26/14
to iterm2-...@googlegroups.com
George or anyone that can help,

I’m having a problem with the applescript change.  I launch my iTerm sessions almost exclusively from applescripts and have for years.  The problem I see is, in short, I can’t get George’s sample script (provided below in this email thread) to even run.  I get a Syntax Error: "Expected end of line, etc. but found class name."  On the 3rd line of the file.  It highlights this word on line 3:  create window with default profile
  Sorry if this is a simple or silly problem.  I’m not new to applescript, but I’m also definitely not an expert in it either.  The example looked like it should work, and when it didn’t, I couldn’t figure out why.  Help if you can.

I should mention that I upgraded to the current nightly build before sending this email, but that the symptom hasn’t changed since the day this went live.  (I just didn’t have time to look at it until today)

-Mike

Tom Feist

unread,
Sep 26, 2014, 3:06:05 PM9/26/14
to iterm2-...@googlegroups.com
Mike,

What's the complete script that you're running?
The following works ok (with current nightly) for me (directly from Applescript Editor):

--
tell application "iTerm2"
create window with default profile
end tell
--

As does:
$ osascript -e 'tell application "iTerm2" to create window with default profile'

I seem to recall at some point in the past that sometimes after installing new releases of iterm, applescripts and the 'open' command would launch the older version if I hadn't
nuked it completely. You might check if you have any other versions lying around, or if in Applescript Editor -> menu -> Open Dictionary dialog you see more than 1
iTerm(2) listed.

Or it's something else entirely :) Applescript is weird like that.

Cheers,

Tom

Michael Garrett

unread,
Sep 26, 2014, 3:16:50 PM9/26/14
to iterm2-...@googlegroups.com
I copied and pasted the example from George’s (Mon, Aug 25, 2014 at 11:07 PM) email below starting with “tell iTerm2” and ending with the last “end tell”.

Was that just pseudo code? If so, I am very embarrassed.

Your 3 line script works for me which I think rules all other “other versions” things out.

-Mike

Michael Garrett

unread,
Sep 26, 2014, 3:20:54 PM9/26/14
to iterm2-...@googlegroups.com
Maybe I spoke too soon and “other versions” aren’t ruled out.  

When I pasted your 3 line script in:

tell application "iTerm2"
create window with default profile
end tell

It worked.  But, I didn’t notice that it changed the iTerm2 back to iTerm.  Does that mean that I have an old version hanging around that is messing with me?

-Mike

Michael Garrett

unread,
Sep 26, 2014, 3:27:12 PM9/26/14
to iterm2-...@googlegroups.com
To answer your earlier question, When I open Dictionary, I only see one iTerm listed.  It is version 2.9.20140925-3

Tom Feist

unread,
Sep 26, 2014, 3:54:52 PM9/26/14
to iterm2-...@googlegroups.com
I think that might be related, although honestly I don't know enough about the guts of how applescript/events actually decide who/what/where to dispatch.
I believe it has something to do with LaunchServices, and there are things out there for querying/resetting that database (lsregister), but I'd be wary of suggesting
you mess with them without a better understanding of their role (and, that you have good/recent backups!)

`lsappinfo' seems to have some interesting info about what LS thinks is running, and might give you some insight.
If you haven't already, restarting your machine might conceivably help if there's some cache built in memory or at boot. Otherwise, I'm out of ideas I'm afraid.

--Tom

Michael Garrett

unread,
Sep 26, 2014, 4:14:39 PM9/26/14
to iterm2-...@googlegroups.com
I rebooted just now to see if that made things better. “iTerm2” still automatically switches back to “iTerm” after the reboot. I think that indicator tells me that it isn’t working properly, or that I am not seeing George’s changes.

If anyone else has an idea, please send it my way.

Thanks Tom for the help.

-Mike

Daniel Comnea

unread,
Sep 28, 2014, 4:18:54 AM9/28/14
to iterm2-...@googlegroups.com
Mike,

I have the same problems when i open up Dictionaries i see only iTerm.app.

Now a while back i've raised an issue https://code.google.com/p/iterm2/issues/detail?id=3187 and George replied with

Unfortunately I can't change the dock icon without breaking auto-update because /Applications/iTerm.app would need to be renamed. 

And that you can confirm with the output of ps -ef|grep Term

/Applications/iTerm.app/Contents/MacOS/iTerm2

I'm still working through to get the Shuttle ssh app working again following the changes

HTH,
Dani

Michael Garrett

unread,
Sep 29, 2014, 11:46:20 AM9/29/14
to iterm2-...@googlegroups.com
I want to close the loop just in case someone looks back on this email thread for help.  My applescripts were simple.  They simply launch a new iTerm and ssh to a specific machine.  That’s it.  Here is what one of them looked like 2 weeks ago (prior to the recent change)

tell application "iTerm"
--activate

-- make a new terminal
set myterm to (make new «class Ptrm»)

-- talk to the new terminal
tell myterm
-- make a new session
set mysession to (make new «class Pssn» at the end of every «class Pssn»)

end tell

tell mysession

-- set some attributes
--set name to "bubblep"
--set foreground color to "red"
--set background color to "blue"
set «class Tran» to "0.1"

-- execute a command
«event ITRMExec» given «class Cmnd»:"ssh -o StrictHostKeyChecking=no machine.mydomain.com"

end tell -- we are done talking to the session

end tell

After updating to a recent nightly, those scripts stopped working.  I thought the answer was to simply change [tell application “iTerm” ]  to [tell iTerm2] like I saw in George’s example code (see the Aug 25th email below for his example code).  But that didn’t compile.   I now think that code was mostly pseudo code, and wasn’t capable of running.  The missing word “application” in line 1 is an indicator of that I think.

Tom Feist gave me a 3 line script to try, and it worked.  His script started with [tell application “iTerm2”].  But, notably, my applescript editor auto changed the iTerm2 back to iTerm.  That made me think that some part of the applescript dictionary was still from the older iTerm installation.  I’m still not certain that is or isn’t true.  But, I can get the functionality that I used to have by replacing my old 10 line script with this more simple one:  (note the “iTerm”, not “iTerm2”)

tell application "iTerm"
create window with default profile command "ssh -o StrictHostKeyChecking=no machine.mydomain.com"
end tell

I’m not sure if a future update will replace that dictionary and break me again, but I can easily fix my 3 line applescripts if it does happen.

Thanks for all of the work and support.  I love iTerm(2)!

-Mike



On Sep 26, 2014, at 4:14 PM, Michael Garrett <mg.no...@gmail.com> wrote:

I rebooted just now to see if that made things better.  “iTerm2” still automatically switches back to “iTerm” after the reboot.  I think that indicator tells me that it isn’t working properly, or that I am not seeing George’s changes.  

If anyone else has an idea, please send it my way.

Thanks Tom for the help.  

-Mike

On Sep 26, 2014, at 3:54 PM, Tom Feist <sha...@gmail.com> wrote:

I think that might be related, although honestly I don't know enough about the guts of how applescript/events actually decide who/what/where to dispatch.
I believe it has something to do with LaunchServices, and there are things out there for querying/resetting that database (lsregister), but I'd be wary of suggesting
you mess with them without a better understanding of their role (and, that you have good/recent backups!)

`lsappinfo' seems to have some interesting info about what LS thinks is running, and might give you some insight. 
If you haven't already, restarting your machine might conceivably help if there's some cache built in memory or at boot. Otherwise, I'm out of ideas I'm afraid.

--Tom


On 26 Sep 2014, at 20:20, Michael Garrett <mg.no...@gmail.com> wrote:

Maybe I spoke too soon and "other versions" aren't ruled out.  

When I pasted your 3 line script in:

tell application "iTerm2"
create window with default profile
end tell

It worked.  But, I didn't notice that it changed the iTerm2 back to iTerm.  Does that mean that I have an old version hanging around that is messing with me?

-Mike

On Sep 26, 2014, at 3:16 PM, Michael Garrett <mg.no...@gmail.com> wrote:

I copied and pasted the example from George's (Mon, Aug 25, 2014 at 11:07 PM) email below starting with "tell iTerm2" and ending with the last "end tell".

Was that just pseudo code?  If so, I am very embarrassed.  

Your 3 line script works for me which I think rules all other "other versions" things out.  

-Mike


-- 
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

George Nachman

unread,
Oct 5, 2014, 6:36:44 PM10/5/14
to iterm2-...@googlegroups.com
Sorry for getting back to this thread so late, but the issue is that the name you give to tell application depends on how your app is named on disk. I had originally planned to rename the app from iTerm.app to iTerm2.app on disk, but that breaks the autoupdater :(. I'll update the sample code. Sorry for the inconvenience!
Message has been deleted

Daniel Comnea

unread,
Oct 29, 2014, 4:00:50 PM10/29/14
to iterm2-...@googlegroups.com
George,

When are you planning to add the new Applescript syntax in the nightly builds to the official release?

Asking because valuable app like SSH Shuttle http://fitztrev.github.io/shuttle/ relies on it and i'm heavily using it but i can push a change as the owner won't accept it, will break other users who still on the official iTerm release.

Cheers,
Dani

George Nachman

unread,
Nov 8, 2014, 4:04:35 PM11/8/14
to iterm2-...@googlegroups.com
Hi Dani,
Unfortunately both versions will need to exist together for a while. The 2.9 branch (currently the nightly build) is not ready for beta status quite yet. Beta periods tend to be long as well.

I think your best bet is to do something like this:

get version of application "iTerm"

If the version number is 2.9 or greater, use the new syntax.

On Wed, Oct 29, 2014 at 12:58 PM, Daniel Comnea <daniel...@gmail.com> wrote:
George,

When are you planning to add the new Applescript syntax in the nightly builds to the official release?

Asking because valuable app like SSH Shuttle http://fitztrev.github.io/shuttle/ relies on it and i'm heavily using it but i can push a change as the owner won't accept it, will break other users who still on the official iTerm release.

Cheers,
Dani


On Sunday, 5 October 2014 23:36:44 UTC+1, George Nachman wrote:
Reply all
Reply to author
Forward
0 new messages