AppleScript 判断按键事件的脚本文章收集

AppleScript support for SplitPanes

来自:http://code.google.com/p/iterm2/issues/detail?id=559

Reported by  samantha... @gmail.com ,  Jan 29, 2011
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
May 14, 2011
#1 troy.bol...@gmail.com
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
Jun 1, 2011
#2 samantha...@gmail.com
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
Nov 27, 2011
#3 tomas.gu...@gmail.com
+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.
Dec 5, 2011
Project Member #4 gnach...@gmail.com
(No comment was entered for this change.)
Blockedon: 152 
Mar 20, 2012
#5 wou...@spotify.com
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
May 8, 2012
#6 otto.poe...@gmail.com
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
Jun 14, 2012
#7 fr.ro...@gmail.com
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?
Jun 14, 2012
#8 fr.ro...@gmail.com
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
Mar 21, 2013
#9 dfourn...@azendoo.com
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...
Apr 18, 2013
#10 bmfurt...@geekoo.eu
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.
Jul 21, 2013
#11 martingi...@gmail.com
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


How to re-trigger hotkey in applescript?

来自:http://www.alfredforum.com/topic/2766-how-to-re-trigger-hotkey-in-applescript/

kengao

Newbie

17202538_D7dC.jpg Member Pip 5 posts

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!


#2 17202540_C96B.pngDavid Ferguson

Alfred Guru

17202540_dcaI.jpg Staff 1,562 posts
  • LocationHuntsville, AL

Posted 01 July 2013 - 01:01 PM

kengao, on 01 Jul 2013 - 4:21 PM, said:17202541_3xAc.png

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

#3 17202540_C96B.pngkengao

Newbie

17202538_D7dC.jpg Member Pip 5 posts

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 :P

 

Thank you.


#4 17202540_C96B.pngTyler Eich

Advanced Member

17202545_Hbud.png Member Pip Pip Pip 410 posts
  • LocationIndiana, USA

Posted 01 July 2013 - 08:36 PM

kengao, on 01 Jul 2013 - 4:21 PM, said:17202541_3xAc.png

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.


转载于:https://my.oschina.net/freeblues/blog/185371

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值