AppleScript support for SplitPanes
来自:http://code.google.com/p/iterm2/issues/detail?id=559
Hi, I would like to be able to script the creation of SplitPanes which I find very useful. Ideally, would like to be able to name each pane, launch command in each pane upon creation and decide if the pane is split vertically or horizontally Congratulations for what's already been achieved with iTerm2. @samo9789
I've been using the following applescript to setup panes. Perhaps it will be useful to others until better support is available... osascript <<EOF tell application "iTerm" activate set myterm to (make new terminal) tell myterm launch session "Panes" set number of columns to 244 set number of rows to 73 tell application "System Events" to keystroke "d" using command down tell application "System Events" to keystroke "d" using command down tell application "System Events" to keystroke "D" using command down tell application "System Events" to keystroke "D" using command down tell application "System Events" to key code 123 using {command down, option down} tell application "System Events" to keystroke "D" using command down tell application "System Events" to keystroke "D" using command down tell application "System Events" to key code 123 using {command down, option down} end tell end tell EOF
Thanks for sharing this tip. I added it (crediting you) to my blog post about scripting iTerm2: http://www.worldgoneweb.com/2011/iterm2-advanced-features/#comment-20560
+1 on this one. I'm trying to script opening up 4 panes (2x2), and having each one SSH into a different machine. However, I can't reference any of the new sessions while running the script from within iTerm2.
(No comment was entered for this change.)
Some time ago, I wrote a ruby app that splits stuff for you and connects over ssh to different machines. Might be interesting: https://github.com/wouterdebie/i2cssh
It seems that the sessions implicitly created by simulation the keystrokes are appended to the end of the sessions list: tell application "iTerm" activate tell (make new terminal) set number of columns to 50 set number of rows to 50 tell (launch session "initial_session") to write text "echo 'SESSION 1'" tell application "System Events" to keystroke "D" using command down tell last item of sessions to write text "echo 'SESSION 2'" tell application "System Events" to keystroke "d" using command down tell last item of sessions to write text "echo 'SESSION 3'" end tell end tell
You can create 4 panes in a single tab like this by using the shortcut option-command-arrowup (key code 126): tell i term application "System Events" to key code 126 using {command down, option down} tell i term application "System Events" to keystroke "d" using command down tell last item of sessions to write text "echo 'SESSION 4'" This works, however "the last item of sessions" doesn't follow. The result is that SESSION 4 is shown in the pane of SESSION 3. Any ideas?
Ok, I already figured out. Apparently, when you switch panes with option-command-arrows and create a new pane, the number changes. The new session after session 3 becomes session 2. So to solve the issue for session 4, you can't use 'the last item' but specify item 2. The code then becomes: tell application "iTerm" activate tell (make new terminal) tell (launch session "initial_session") to write text "echo 'SESSION 1'" tell i term application "System Events" to keystroke "D" using command down tell last item of sessions to write text "echo 'SESSION 2'" tell i term application "System Events" to keystroke "d" using command down tell last item of sessions to write text "echo 'SESSION 3'" tell i term application "System Events" to key code 126 using {command down, option down} tell i term application "System Events" to keystroke "d" using command down tell i term application "System Events" to key code 126 using {command down, option down} tell item 2 of sessions to write text "echo 'SESSION 4'" end tell end tell
Hi, I begin in writing appleScript. I think I wrote a better script to store the session and then be able to launch command in the pane you want to tell application "iTerm" activate tell (current terminal) copy id of current session to session1 tell i term application "System Events" to keystroke "d" using command down copy id of last item of sessions to session2 tell i term application "System Events" to keystroke "D" using command down copy id of last item of sessions to session3 repeat with theItem in sessions if id of theItem is session1 then tell theItem to write text "echo " & (session1) end if end repeat repeat with theItem in sessions if id of theItem is session2 then tell theItem to write text "echo " & (session2) end if end repeat repeat with theItem in sessions if id of theItem is session3 then tell theItem to write text "echo " & (session3) end if end repeat end tell end tell I try to simplify it whith using `tell the first item of sessions whose id is session1` but it didn't work...
Hey, if anyone's interested here's an applescript I wrote that more or less inteligently splits a new iterm window and connects to an ad-hoc number of ssh sessions (sort of like cssh) https://gist.github.com/bmfurtado/5392276 enjoy.
https://github.com/luismartingil/scripts/blob/master/iterm_launcher02.applescript #!/usr/bin/osascript -- Applescript to launch iterm2 terminals/tabs with configurable: -- ~ List of commands <cmds> -- ~ Color <color> -- ~ Name <name> -- ~ Transparency <trans> -- ~ Zoom out <zoomout> -- ~ Split behavior horizontal(h) or vertical(v) <split> :: (h, v) -- -- Run from terminal with `osascript` or just ./<<script>> -- Dont unfocus with the mouse/keyboard while executing. the script. -- Recomended to go full screen (CMD + Enter) if <zoomout> attributes used. -- Change myTermWindow and myItem(s) as desired. -- -- -- Author: Luis Martin Gil http://www.luismartingil.com -- Year : 2013 tell application "iTerm" -- First tab set myItem1 to {} set myItem1 to myItem1 & {{color:"yellow", cmds:{"echo yellow", "ls -lrt"}, name:"name_yellow", trans:"0.1", zoomout:4, split:"h"}} set myItem1 to myItem1 & {{color:"blue", cmds:{"echo blue1", "ls -lrt"}, name:"name_blue1", trans:"0.1", zoomout:2, split:"v"}} set myItem1 to myItem1 & {{color:"blue", cmds:{"echo blue2", "ls -lrt"}, name:"name_blue2", trans:"0.1", zoomout:4, split:"v"}} set myItem1 to myItem1 & {{color:"blue", cmds:{"echo blue3", "ls -lrt"}, name:"name_blue3", trans:"0.1", zoomout:6}} -- Second tab set myItem2 to {} set myItem2 to myItem2 & {{color:"red", cmds:{"echo red1", "ls -lrt"}, name:"name_red1", trans:"0.1", zoomout:8, split:"h"}} set myItem2 to myItem2 & {{color:"red", cmds:{"echo red2", "ls -lrt"}, name:"name_red2", trans:"0.1", zoomout:4}} -- Third tab set myItem3 to {} set myItem3 to myItem3 & {{color:"green", cmds:{"echo green", "ls -lrt"}, name:"name_green", trans:"0.1", zoomout:2, split:"v"}} set myItem3 to myItem3 & {{color:"purple", cmds:{"echo purple", "ls -lrt"}, name:"name_purple", trans:"0.1", zoomout:4}} set myTermWindow to {myItem1, myItem2, myItem3} set myterm to (make new terminal) tell myterm repeat with n from 1 to count of myTermWindow launch session n repeat with i from 1 to count of (item n of myTermWindow) -- Lets set the properties of the actual tab tell the last session to set name to name of (item i of (item n of myTermWindow)) tell the last session to set background color to color of (item i of (item n of myTermWindow)) tell the last session to set transparency to trans of (item i of (item n of myTermWindow)) -- Some commands might require more columns to be readable repeat zoomout of (item i of (item n of myTermWindow)) times tell i term application "System Events" to keystroke "-" using command down end repeat -- Lets execute the commands for the tab repeat with cmd in cmds of (item i of (item n of myTermWindow)) tell the last session to write text cmd end repeat -- Split the pane in a "D" (vertical) or "d" (horizontal) way if i is less than (count of (item n of myTermWindow)) then if "h" is split of (item i of (item n of myTermWindow)) then set split_str to "D" else if "v" is split of (item i of (item n of myTermWindow)) then set split_str to "d" else error return end if tell i term application "System Events" to keystroke split_str using command down end if end repeat end repeat end tell end tell
Updating last post's link ( https://github.com/luismartingil/per.scripts/blob/master/iterm_launcher02.applescript )
来自:http://www.alfredforum.com/topic/2766-how-to-re-trigger-hotkey-in-applescript/
Posted 01 July 2013 - 08:21 AM
Hi there,
I've using workflow to supply some specified purpose in some applications via hotkey, for example:
Hotkey (cmd+g) -> run osascript:
If the frontmost app is not Evernote then
error -127
else
get the source url of the selected note
tell safari to open the url
end if
Now my question is, because alfred will overwrite the hotkeys, in some other applications which also use the cmd+g, will not trigger the corresponded events. Hence, I need some scripts to replace the "error -127" to re-trigger "cmd+g" again.
I've used
tell application "System Events" to key code 5 using {command down}
or
tell application "System Events" to keystroke "g" using command down
But they are not working, and my front most application will become very slow :\ It seems System Event will execute for almost half minutes, which is unreasonable in my opinion.
Am I miss something?
Thank you!
Posted 01 July 2013 - 01:01 PM
kengao, on 01 Jul 2013 - 4:21 PM, said:
Hi there,
I've using workflow to supply some specified purpose in some applications via hotkey, for example:
Hotkey (cmd+g) -> run osascript:
If the frontmost app is not Evernote then
error -127
else
get the source url of the selected note
tell safari to open the url
end if
Now my question is, because alfred will overwrite the hotkeys, in some other applications which also use the cmd+g, will not trigger the corresponded events. Hence, I need some scripts to replace the "error -127" to re-trigger "cmd+g" again.
I've used
tell application "System Events" to key code 5 using {command down}
or
tell application "System Events" to keystroke "g" using command down
But they are not working, and my front most application will become very slow :\ It seems System Event will execute for almost half minutes, which is unreasonable in my opinion.
Am I miss something?
Thank you!
I believe you have to do...
tell application "System Events" tell process "Evernote" to keystroke "g" using command down end tell
Twitter: jdfwarrior | Blog: dferg.us
Posted 01 July 2013 - 03:07 PM
Thank you David Ferguson. I know what causes my problem now, after tracing some logs.
My misunderstood is the keystroke is actually success, however, every key strokes -- even tell process to keystroke -- are still go back to Alfred. And it causes an infinite loop with many key strokes, which cause my entire OS slowing down
Before an application-specified howkey of alfred is supported, I will use the menubar to call the desired function for some specified applications. It's still not a universal and great solution because various applications will not put the items with same hotkey in the same menubar position. But it's still better than solwing down the system
Thank you.
Posted 01 July 2013 - 08:36 PM
kengao, on 01 Jul 2013 - 4:21 PM, said:
Hi there,
I've using workflow to supply some specified purpose in some applications via hotkey, for example:
Hotkey (cmd+g) -> run osascript:
If the frontmost app is not Evernote then
error -127
else
get the source url of the selected note
tell safari to open the url
end if
Now my question is, because alfred will overwrite the hotkeys, in some other applications which also use the cmd+g, will not trigger the corresponded events. Hence, I need some scripts to replace the "error -127" to re-trigger "cmd+g" again.
I've used
tell application "System Events" to key code 5 using {command down}
or
tell application "System Events" to keystroke "g" using command down
But they are not working, and my front most application will become very slow :\ It seems System Event will execute for almost half minutes, which is unreasonable in my opinion.
Am I miss something?
Thank you!
You might be interested in this feature request. It describes 'scoped' workflows, that are only activated when certain apps are frontmost.
Cheers
Edited by Tyler Eich, 01 July 2013 - 08:37 PM.