dougscripts.com

System Events, Key Code and Keystroke

Key codes are the numeric codes representing the keys on your keyboard. Keystrokes are the actual Unicode key representations. You can use these key code numbers or keystroke strings to emulate key presses via AppleScript with "System Events", provided you also have the "Enable access for assistive devices" option checked in the "Accessibility" (formerly "Universal Access") System Preference.

Using key code and keystroke you can have AppleScript perform some shortcut actions which, on occasion, may be more advantageous than scripting the actual action. Indeed, as far as iTunes is concerned, some of these shortcut actions aren't possible with AppleScript without using key code or keystroke. For instance, opening a Get Info Window or the View Options window. Here's how to get the most from the key code and keystroke commands, AppleScript, and iTunes.

macOS 10.15 Catalina and later: The example snippets below were written several years ago for iTunes. But most of them, if not all, will work with the Music app simply by changing "iTunes" to "Music". Some may even work with the TV app by targetting "TV".

Setting Up System Preferences

In order for the "System Events" application to use the key code or keystroke commands, you have to enable the macOS accessibility frameworks in System Preferences.

Mavericks and later. Access must now be set on a per-app basis. See this Apple support document for details.

Pre-OS X 10.9/Mavericks operating systems: Click on the "Accessibility" pane (formerly "Universal Access") in "System Preferences". At the bottom left 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.

AppleScript Implementation

"System Events" will send a key code or a keystroke to the frontmost application. So, you have to make sure that iTunes is frontmost—activate it, in AppleScript parlance. 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" (the script makes sure iTunes is not stopped first) 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, the using 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 (stuff in {} brackets) 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" sdef via AppleScript Editor's Window > Library.)

Go Nuts

Here's a script using key codes and "normal" AppleScripting to select the Music library and then put focus on the Search field:

tell application "iTunes"
	activate
	-- select the Music library
	reveal (some playlist whose special kind is Music)
end tell
tell application "System Events"
	-- bring focus to Search box - Command-Option-F
	key code 3 using {command down, option down}
end tell


A similar version that searches using a string in the clipboard:

tell application "iTunes"
	activate
	reveal (some playlist whose special kind is Music)
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.

You also might want to activate these scripts using a keyboard shortcut. For example, I've mapped the first "Music library & search" script above to Command-Option-S.

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 recommend using the free Key Codes utility developed by my friends at Many Tricks. It will display the key code, unicode value, and modifier keys state for any key combination you press on the keyboard.

Common key code Actions

These are posted as examples. DO NOT run this as a script otherwise ALL the commands will be executed.

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



Site contents © 2001 - 2024 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Privacy.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
The one who says "it cannot be done" should not be interrupting the one who is doing it.