惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
V
V2EX
aimingoo的专栏
aimingoo的专栏
爱范儿
爱范儿
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
T
The Blog of Author Tim Ferriss
月光博客
月光博客
云风的 BLOG
云风的 BLOG

博客园 - Cheney Shen

如果将 macOS Mojave 降级为 macOS Sierra 或者更低版本 pip源提示“not a trusted or secure host” 解决 Window系统下MongoDB安装及远程访问 Windows下安装appium桌面版和命令行版 Command 'java' not found during running appium sudo npm install -g cnpm --registry=https://registry.npm.taobao.org Pycharm 中错误ImportError: No module named appium CURL并发测试POST和DELETE请求 curl如何发起DELETE/PUT请求?(备忘) Ubuntu 11.10下安装配置Zend Studio 9.0破解版详细步骤 iphone开源汇总(转) iphone开源汇总(转) python中的cursor[备忘] 在main函数执行之前和执行之后执行的方法 Intel苹果电脑Mac+Win+Linux多重系统启动(+公用分区)终极解决方案(备忘) IGT笔试题,正整数N等于M个不同的正整数之和的问题 如何通过一个结构体成员变量的地址找到该结构体的首地址?[备忘] 为特定应用程序关闭恢复窗口功能 [Mac OS X Lion][转] Mac下Perl脚本如何运行
System Events, Key Code and Keystroke from Doug's AppleSc...
Cheney Shen · 2011-09-09 · via 博客园 - Cheney Shen

Setting Up System Preferences

In order for the "System Events" application to use the key code or keystroke commands, you have to enable Mac OS X's accessibility frameworks. To do this, click on the "Universal Access" pane in "System Preferences". At the bottom of the pane is a checkbox setting called "Enable access for assistive devices". Click on the checkbox so the setting is enabled. Close out of System Preferences.

using key codes

AppleScript Implementation

"System Events" will send a key code or a keystroke to the front application. So, you have to make sure that iTunes is frontmost. Without going into a long winded explanation, here's an example script that should be self-explanatory. It will select the currently playing iTunes track using the keystroke command:

tell application "iTunes"
	activate
	if player state is not stopped then
		tell application "System Events" to keystroke "l" using command down
	end if
end tell


I have emulated a key press for "Command-L", provided iTunes is not stopped, by providing the lowercase "l", the key you would press for the actual menu command. The using parameter tells the keystroke command to include a key press of the Command key. Here's the same action using the key code command:

tell application "iTunes"
	activate
	if player state is not stopped then
		tell application "System Events" to key code 37 using command down
	end if
end tell


In the example above the number 37 refers to the key "l"—lowercase. As with the keystroke command, theusing parameter tells the key code command to include a key press of the Command key.

You can include multiple helper keys by setting the using parameter to a list. The following scripts each open the iTunes Store in the iTunes browser (Command-Shift-H), first with a keystroke command and then with the key code command. Note the list following using:

tell application "System Events"
	tell application "iTunes" to activate
	keystroke "h" using {command down, shift down}
end tell


tell application "System Events"
	tell application "iTunes" to activate
	key code 4 using {command down, shift down}
end tell


The number 4 refers to the key "h". (For more info on how to construct a keystroke or key code command, check out the "System Events" dictionary.)

Go Nuts

Here's a script using key codes and "normal" AppleScripting that searches for a string in the clipboard:

tell application "iTunes"
	activate
	set view of front window to playlist 1
end tell
tell application "System Events"
	-- bring focus to Search box - Command-Option-F
	key code 3 using {command down, option down}
	-- paste the clipboard - Command-V
	key code 9 using command down
end tell


Select all tracks in the selected playlist and make a new "untitled" playlist from them:

tell application "iTunes"
	activate
end tell
tell application "System Events"
	-- select all tracks - Command-A
	key code 0 using command down
	--new playlist from selection- Command-Shift-N
	key code 45 using {command down, shift down}
end tell


Get it?

Of course, the above can be easily scripted the "normal" way. I'm not a big fan of using this sort of GUI scripting but sometimes it will be the only way to automate some iTunes tasks.

How to Get Key Codes

Using the keystroke command is fairly straight forward: just use the key's character as a string. To get the key code number for a key press, I used the simple and free application Full Key Codes, written by Denis Bajram, which displays the decimal, ASCII, and hexidecimal numbers for a keypress on the keyboard.

Common key code Actions

-- select all tracks of front playlist - Command-A
tell application "System Events"
	tell application "iTunes" to activate
	key code 0 using command down
end tell

-- toggle browse mode - Command-B
tell application "System Events"
	tell application "iTunes" to activate
	key code 11 using command down
end tell

-- copy selection (or whatever) - Command-C
tell application "System Events"
	tell application "iTunes" to activate
	key code 8 using command down
end tell

-- toggle Right Sidebar - Shift-Command-G
tell application "System Events"
	tell application "iTunes" to activate
	key code 5 using {command down, shift down}
end tell

-- Get Info for selected track(s) - Command-I
tell application "System Events"
	tell application "iTunes" to activate
	key code 34 using command down
end tell

-- open View options window - Command-J
tell application "System Events"
	tell application "iTunes" to activate
	key code 38 using command down
end tell

-- select current track - Command-L
tell application "System Events"
	tell application "iTunes" to activate
	key code 37 using command down
end tell

-- Show File of selected - Shift-Command-R
tell application "System Events"
	tell application "iTunes" to activate
	key code 15 using {command down, shift down}
end tell

-- toggle Zoom of Main Browser - Control-Command-Z
tell application "System Events"
	tell application "iTunes" to activate
	key code 6 using {command down, control down}
end tell

-- Close a window with "Cancel" - Escape key
tell application "System Events"
	tell application "iTunes" to activate
	key code 53
end tell


(* 
THE FOLLOWING ARE DELETE ACTIONS!
USE CAREFULLY!!!
*)
-- Delete the selected playlist from your Source list without confirming that you want to delete it - Command-Delete
tell application "System Events"
	tell application "iTunes" to activate
	key code 51 using command down
end tell

-- Delete the selected playlist and all the songs it contains from your library 
-- OR
-- Delete the selected song from your library and all playlists
-- Option-Delete
tell application "System Events"
	tell application "iTunes" to activate
	key code 51 using option down
end tell

-- Select the search field - Command-Option-F
tell application "System Events"
	tell application "iTunes" to activate
	key code 3 using {command down, option down}
	-- and optionally, paste the clipboard - Command-V
	
	-- key code 9 using command down
	
end tell


-- playlist and folder

-- new playlist - Command-N
tell application "System Events"
	tell application "iTunes" to activate
	key code 45 using {command down, shift down}
end tell

-- new playlist from selection - Command-Shift-N
tell application "System Events"
	tell application "iTunes" to activate
	key code 45 using {command down, shift down}
end tell

-- new Smart Playlist - Command-Option-N
tell application "System Events"
	tell application "iTunes" to activate
	key code 45 using {command down, option down}
end tell

-- new Playlist Folder - Command-Option-Shift-N
tell application "System Events"
	tell application "iTunes" to activate
	key code 45 using {command down, option down, shift down}
end tell

-- toggle Show/Hide Main Browser window - Command-1
tell application "System Events"
	tell application "iTunes" to activate
	key code 18 using command down
end tell

-- toggle Show/Hide Equalizer window - Command-2
tell application "System Events"
	tell application "iTunes" to activate
	key code 19 using command down
end tell