Automate tasks with AppleScript, Fake, and the keychain
You can make life a lot easier on your Mac if you learn how to automate things. The standard approach to do this on Macs is to use Automator (see How to use Automator: What Automator is and how it works from Macworld UK). If you include AppleScript, Fake, and the keychain in your toolchain, you can achieve even more. As an example I will develop an automated task to create a new email alias using a web portal. This example is used since it includes user input (email alias), access of password in the keychain, filling in and submitting forms on web pages, and returning data to the user through the clipboard. An example using Automator is also available.
AppleScript
The main parts of this example are an AppleScript script and a Fake workflow. We also expect that the password of the user is stored in the keychain [1]. The first thing we do is to fetch the password for the web portal from the keychain:
set domain_name to "mydomain.com" set user_name to "myusername" set pwd_cmd to "security find-generic-password" set pwd_script to pwd_cmd & " -a " & user_name & " -w" set pass_wd to do shell script pwd_script
The next step is to prompt the user for the email alias (including converting the input to lowercase [2]):
set dialog_txt to "New email alias (@" & domain_name & "):" display dialog dialog_txt default answer "" set email_alias_case to text returned of result set email_alias to _string's lowerString(email_alias_case)
The typical use for the user of a new email alias is to type the new email address into a web form. To make this easier for the user, we'll copy the new email address to the clipboard:
set the clipboard to email_alias & "@" & domain_name
The final part of the AppleScript is to execute the Fake workflow. We have to transfer three parameters (variables) to the workflow:
tell application "Fake" set variable with name "emailAlias" to email_alias set variable with name "userName" to user_name set variable with name "passWd" to pass_wd activate open "Users:aa:Applications:MakeEmailAlias.fakeworkflow" delay 1 run workflow with variables { \ emailAlias:email_alias, \ userName:user_name, \ passWd:pass_wd \ } wait until done close front document quit end tell
The only thing missing in the AppleScript code above is the loading of the text string manipulation library _string.scpt
[2]:
set _string to load script alias ( \ (path to library folder from user domain as text) & \ "Scripts:Libraries:" & "_string.scpt")
Fake workflow
The Fake workflow receives 3 parameters (variables) from the AppleScript script: the new email alias (emailAlias
), the username (userName
), and the password (passWd
) to log in to the portal. In the Fake workflow, we use these variables to fill in the correct values in the web form. The following Fake workflow is an example, and it has to be updated based on the actual web portal you are using. The example workflow consists of 4 steps (and 10 Fake workflow actions):
- Go to login web page:
- Load URL: example.com
- Log in with username and password (and wait a second):
- Set Value of HTML Element:
- with id:
user
- to:
{userName}
- with id:
- Set Value of HTML Element:
- with id:
password
- to:
{passWd}
- with id:
- Click HTML Element:
- for XPath:
/html/body/div/...
- for XPath:
- Delay:
- for:
1.0
seconds
- for:
- Set Value of HTML Element:
- Select the web page where email aliases can be added:
- Click HTML Link:
- with text:
email@mydomain.com
- with text:
- Click HTML Link:
- Add email alias (and wait tw seconds):
- Focus HTML Element:
- with id:
newalias
- with id:
- Set Value of HTML Element:
- with id:
newalias
- to:
{emailAlias}
- with id:
- Click HTML Element:
- with id:
submit
- with id:
- Delay:
- for:
2.0
seconds
- for:
In the example above, we use different approaches to identify the elements on web pages (with id, for XPath, with text). In your case, you should use the approach that is easiest for the web pages you are scripting. Fake provides a feature where you can drag the id of an element to the workflow action id value.
Notes
- To store and access data in the keychain, we use the
security
command line interface. To add a new account name with password pwd to the keychain you can do the following command:
Then, we can print this password with the following command:security add-generic-password -a name -s service -w pwd
(In the first command service is a human-readable name describing your service.)security find-generic-password -a name -w
- In the examples above I expect the AppleScript library
_string.scpt
from Brati's Lover AppleScript library.