Jump to content

AppleScript Key Code Finder


Recommended Posts

About Workflow

As someone pretty new to the world of scripting, automations and Alfred, I found myself constantly referring to the AppleScript key code list for certain GUI automations (I'm sure many of these are easily done via other language, but I'm learning to walk first!) 

After a while of doing this, I decided to look for a key code workflow but really couldn't find one that had all the keys mapped. While I was building it, I also thought I'd shave off a couple of seconds of typing by outputting the text including any appropriate modifier keys (such as key code 12 using {command down, shift down}), created icons for each key, and now thought someone else could benefit from this fun little build.

 

Dependencies

None

 

Usage

Hotkey + Space followed by any letter on the keyboard. If you need modifiers, press any combination and hit enter. The workflow will paste the result in the application with focus and include any modifiers such as {command down, option down}.

 

I'm sure there are cleaner ways to make it work in Alfred that doesn't involve so many modifier paths, so I would love to learn how I might be able to do that (keeping in mind I'm still elementary school with AppleScript only).

 

CleanShot+2021-03-05+at+13.05.29.gif

 

Download

The workflow can be downloaded from GitHub here. The icons used in this workflow (an icon for each letter of the keyboard) can also be downloaded from the same source).

 

 

Edited by nagym
Updated with GitHub links.
Link to post

Welcome to the community, @nagym,

 

You don’t need to map every single key (that would be a huge undertaking, due to language-specific characters). You can use keystroke instead of key code for typeable characters (keystroke "q" using command down).

 

Nice idea with pasting the key code with the corresponding using! Makes a ton of sense.

 

1 hour ago, nagym said:

keeping in mind I'm still elementary school with AppleScript only

 

Regarding this, the best advice I can give you is “move to another language as soon as possible”. In this forum you’ll find plenty of posts where I and @deanishe discourage people from using AppleScript as their main language. Long-story-short, AppleScript is an underpowered language that is dying (Apple doesn’t care for it and fired the people in charge of improving it); it’s unpopular (documentation and good help are hard to find); and what you learn from it isn’t easily transferable (it deviates from other languages in key points).

 

We’d recommend instead Ruby or Python, which have vibrant communities with tons of learning resources and libraries. AppleScript is one of those “only use when absolutely necessary” languages.

 

That said, have fun learning. Just keep in mind that if you plan on keeping at it, AppleScript will soon hold you down instead of propping you up.

Link to post
29 minutes ago, vitor said:

the best advice I can give you is “move to another language as soon as possible”.

 

That said, for firing keypresses at applications, AppleScript is often the best choice.

 

2 hours ago, nagym said:

The workflow can be downloaded from iCloud here or an alternative location via JumpShare here.

 

Are those links permanent or "auto-deleted after X days" links?

 

We recommend hosting any workflows you share somewhere permanent (a GitHub repo or GitHub pages website is a great choice). People will still want to download your workflow years from now.

 

I love the icons.

 

EDIT: Is that iCloud drive link correct? It says you've shared a folder. It won't let me download it, at any rate. Only copy it to my own iCloud Drive.

Edited by deanishe
Link to post
6 hours ago, vitor said:

AppleScript is an underpowered language that is dying (Apple doesn’t care for it and fired the people in charge of improving it); it’s unpopular (documentation and good help are hard to find); and what you learn from it isn’t easily transferable (it deviates from other languages in key points).

 

Thanks @vitor, totally agree with you! I'm not at all in the engineering space (business development, actually) but since I started using Alfred, I was actually inspired to start learning by 'doing.' I wanted to make an addition here, a modification here, etc. and next thing I know here I am. And that's pretty cool considering what sparked it!

 

I will definitely learn more about the other languages, thanks for the suggestions. I found the GUI scripting something I really use, which is probably why I started down the AS path. One of my 'most impressive' (to me) workflows is for video conferencing. Basically, menus that launch the appropriate application, changes my input/outputs on the computer, starts a mini DAW along with Audio Unit plugins for my external microphone, starts my Elgato lights, turns on Hue lighting, turns on screen mirroring to the TV while texting people in the house that I"m on a conference call, all the while minimizing every application and putting DND on my calendar. 5 seconds to start all that vs. the manual work? Win. 

 

I think I liked the concept of AS for that application since it's across such a wide variety of macOS programs vs. Electron apps vs. GET and POST actions, AppleScript could accomplish them all via GUI. 

 

Is there a certain language that you suggest I should start with that has similar capabilities of being almost 'universal to every app' regardless of how that app is built? Or I could just be overcomplicating something simple. Probably, actually. 🙂

 

 

 

5 hours ago, deanishe said:

Are those links permanent or "auto-deleted after X days" links?

 

We recommend hosting any workflows you share somewhere permanent (a GitHub repo or GitHub pages website is a great choice). People will still want to download your workflow years from now.

 

I love the icons.

 

EDIT: Is that iCloud drive link correct? It says you've shared a folder. It won't let me download it, at any rate. Only copy it to my own iCloud Drive.

 

Hey @deanishe! Great call, I didn't even think about the timeline aspect. The iCloud was indeed a shared folder that just contained that workflow, so that if I updated an icon or two, it would stay 'updated' for any new downloads. I didn't even think about a GitHub repo, which is silly, considering I find all of the coolest 'speakeasy' macOS applications on there. I'll do that now and update the original!

 

Thanks RE: icons. You'd think there would be some place like icons8, or really anywhere that had all the keyboard letters as a pack, but nope. Actually, to save someone any potential future headaches, I'll post that above, too!

 

Link to post
4 hours ago, nagym said:

I will definitely learn more about the other languages, thanks for the suggestions. I found the GUI scripting something I really use, which is probably why I started down the AS path. One of my 'most impressive' (to me) workflows is for video conferencing. Basically, menus that launch the appropriate application, changes my input/outputs on the computer, starts a mini DAW along with Audio Unit plugins for my external microphone, starts my Elgato lights, turns on Hue lighting, turns on screen mirroring to the TV while texting people in the house that I"m on a conference call, all the while minimizing every application and putting DND on my calendar. 5 seconds to start all that vs. the manual work? Win.

 

Holy shit! That must be mostly simulated keypresses and clicks?

 

4 hours ago, nagym said:

AppleScript could accomplish them all via GUI

 

Yeah. AppleScript is great for talking to applications. The problem is, it’s horrible for doing anything with any data you get back from the app. JXA (JavaScript) is much better in that regard. It has AppleScript’s support for application scripting, but is also a reasonable language. And it supports JSON, which is a big deal for writing workflows.

 

4 hours ago, nagym said:

Is there a certain language that you suggest I should start with that has similar capabilities of being almost 'universal to every app' regardless of how that app is built?

 

Scripting applications only works well in Apple's own languages: AppleScript, JXA, Swift, Objective-C. There are also libraries for, say, Python, but it's generally best to use an Apple language if you want to use Apple's APIs (including application scripting).

 

Ruby and Python are both great choices for a first language. They’re both powerful, high-level, general-purpose languages that can be used to make almost anything. Python in particular has a huge amount of documentation aimed at beginners, as it’s really popular for data science (and scientists aren’t programmers).

 

Personally, I prefer Python because of its "there should be one obvious way to do it" philosophy. It helps me avoid writing code that I no longer understand a week later.

 

4 hours ago, nagym said:

@deanishe Updated original post with inaugural GitHub repo, and also posted the keyboard key icons there as well. 🙂

 

Nice. Normally, you should put the source code in the GitHub repo, not (just) the built .alfredworkflow file.

 

For example, here are Vítor's workflows. Here's one of my workflows, which needs to be compiled, so the .alfredworkflow files are distributed as GitHub releases.

 

Edited by deanishe
Link to post
40 minutes ago, deanishe said:

 

Holy shit! That must be mostly simulated keypresses and clicks?

 

 

For some, yeah. There are a combination of scripts, Automator-built-applications, list filters, couple of PUT commands for the lights, and a lot of call external triggers linking to other Alfred workflows. The trickiest part was setting it up so that all of this runs from a single key press on the keyboard that looks at the status of a bunch of apps (to determine if any video or audio components are active) and also the current time to determine which menu to bring up (start or close conference), what light options (daytime vs. nighttime), etc. 

 

 

40 minutes ago, deanishe said:

 

Nice. Normally, you should put the source code in the GitHub repo, not (just) the built .alfredworkflow file.

 

 

Booyah, thanks for the tip. I uploaded it, though mine is rather boring since it's all done in Alfred without any scripting work. One day I'll build something that looks half as impressive and clean as yours!

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...