Archive for Keyboard Maestro

Change Screen Resolution with AppleScript

Recently, I read an article at 9to5Mac about automatically changing your screen resolution based on the program you are using. In my practice, I find that the optimal resolution for my Retina MacBook Pro is different for different programs (due to older eyes and various programs that do not allow larger text). FileMaker Pro, Parallels, Microsoft Word, Firefox, Chrome, PDFpenPro,…. each look the best – to me – at different resolution.

The 9to5Mac article discusses SwitchResX which will (among other things) automate the process of changing your screen resolution based on the active program. The program costs $15 and there is a free trial period.

Of course, this is not a lot for a useful app, but I wondered if switching resolutions could be done by AppleScript or another automation tool. Launching a program can trigger an action in Keyboard Maestro. However, there are no actions that relate to screen resolution. But Keyboard Maestro will allow an AppleScript to be triggered when a program becomes the active window. By combining Keyboard Maestro and AppleScript, I was able to automate changes to the screen resolution for different programs.

After a little internet research and some trial and error, I came up with the following script to change the screen resolution to 1920 x 1200. The System Preferences app is only partially scriptable for my intended purpose. For example, scripting is available to change to the various panes of System Preferences, but I could not specifically change settings. To work around this limitation, I used UI scripting available with the System Events app. To identify the specific UI elements (radio buttons, tab groups,,…) that I needed, I used UI Browser by PFiddlesoft.


Screen Shot 2016-05-10 at 11.21.05 AM

This script can be triggered in Keyboard Maestro with the following macro.

KM - Resize Screen 1920x1200

When the AppleScript is triggered, System Preferences is launched and the resolution is changed. There is a very brief screen flicker and System Preferences is closed. The whole process takes about 1.5 seconds.

The rMBP has five difference resolutions from 1024 x 640 to 1920 x 1200. in the above AppleScript, the “5” in the line “click radio button 5” refers to selecting the highest (of the 5) resolution. Changing the “5” to a different value will change the screen to the following resolutions:

“4” – 1680 x 1050

“3” – 1440 x 900

“2” – 1280 x 800

“1” – 1024 x 640

I have three different macros in Keyboard Maestro for the three highest resolutions. For me, Chrome, Parallels and iMovie  look the best at the highest resolution. Firefox, Outlook and FileMaker Pro look the best at “4” resolution. Lastly, Keyboard Maestro itself looks best at the middle resolution, 3.

Please let me know if you find this helpful.



Email Automation – Keyboard Maestro & AppleScript

During a recent episode of Mac Power Users, David Sparks and Katie Floyd discussed email handling. Specifically, they talked about how messages are moved from their Inbox and where they end up. David suggested that dragging messages from the Inbox to another folder is tedious and error prone. Agreed. His solution appears to be “tagging” the emails in such a way that they can found later after searching for the “tag.” My solution is to automate the tedious drag-and-drop procedure. For email automation, I use Keyboard Maestro and AppleScript to do the filing.

First, a little background. I use (primarily) Microsoft Outlook. However, the process described below is not specific to Outlook. As a family law attorney, I represent individuals. As a result I have many “folders” that contain client emails. For example, I have folders named “Smith, John”‘, “Jones, Mary” and so on. Each client has a folder in the Mail portion of Outlook as well as a contact card in the Contact section.

When I’m ready to file an email, I select the email and press command-e. This key combination triggers a Keyboard Maestro macro which does the following:

  1. Nothing, unless Outlook is the active window (“at the front”) – Command-E is not used by Outlook, but it is by other programs.
  2. If Outlook is the active window, an AppleScript is triggered.


The AppleScript does the following:

  1. Assigns the sender’s email address to a variable.
  2. Switches to the Contact view.
  3. Selects all Contacts and assigns them to a variable which is a list of all contacts (I have nearly 1000 contacts – but this process is instantaneous).
  4. A folder name (which I call “directoryName”) is assigned if the sender’s email address is one of a few addresses that have no contact. I do this for email I receive from Ruby Receptionists and Square. If no folder name is assigned in this way, then the sender’s email address is compared to each email address in each contact searching for a match. A single contact can contain more that one email address. For example, client John Smith may have separate work and home email accounts. Both of these email address are taken into account.
  5. If a match is found, another text variable is defined based other contact/client name in the format “Last Name, First name”. For example, if the sender’s email address is matched to contact John Smith, then variable is set to “Smith, John”.
  6. The selected email is moved to an email folder based on the value of the variable. So, in my example, the email would be moved to the folder “Smith, John”.
  7. If the email is successfully moved, an approving sound is played. I use glass.aiff. If no match is found, (or if no corresponding email folder exists) a disapproving sound is played. For that, I use submarine.aiff. (Both of these sounds are system sounds and can be found – along with others – in the System:Library:Sounds folder). You might think that searching through 1000 contacts and moving the email would take at least a few seconds or more. Wrong. This whole process takes about 1 second – much faster (and reliable) than drag-and-drop.


Screen Shot 2016-01-09 at 11.59.26 AM

I use a similar AppleScript to process the “Sent” folder. That script moves messages based on the recipient rather than the sender. If the recipient does not have an associated contact, then the message is left in the “Sent” folder. There is no hotkey for this process. I use Keyboard Maestro to trigger the AppleScript in the middle of the night on a Mac Mini that is always running.

Of course, this solution isn’t perfect. One of the problems is what happens with email from other lawyers. The lawyers are contacts, but not clients so they do not have an email folder. My solution to this problem is manually editing the AppleScript to assign a specific client name (i.e. “directoryName”) when the sender of the email is an opposing attorney. This edited portion of the AppleScript can be removed when the case ends or assigned to a different client if the opposing attorney is involved in a different case.

Another problem, that I’m still working on, is what happens if I have two cases against the same opposing attorney at the same time? Also, in Florida, lawyers get email from the Clerk when documents are filed. Can the filing of those emails be automated? I’m sure the answer is “yes,” but for now it’s drag-and-drop.

EDIT 2/4/2016: For some reason, the part of the script that selects all contacts was working slowly. I used the line: “keystroke “a” using command down” to select all contacts. To speed the script up, I replaced that line with:

tell menu item “Select All” of menu 1 of menu bar item “Edit” of menu bar 1
     click it
end tell