Using Automator to ease Apple Script development

Apple Scripts can be hard. However there is a nifty little trick to make creating them a bit easier by using Automator.

First open Automator and create a new Workflow:

Now press the Record button (you may then need to grant the privileges in System Preferences => Security & Privacy):

If you are done doing the stuff you want to automate, press the stop recording button:
image

Now you will see something like this, listing all the actions you performed:

The cool thing is that you can now just select them all (cmd+a) and copy them to your clipboard (cmd+v).
This will actually put the corresponding Apple Script into your Clipboard and you can now use it in the BTT Apple Script editor or any other and continue working on it.

-- Click the “<fill in title>” tab.
delay 3.005162
set timeoutSeconds to 2.000000
set uiScript to "click radio button 2 of tab group 1 of window \"Demo\" of application process \"Finder\""
my doWithTimeout( uiScript, timeoutSeconds )

-- Click the “<fill in title>” tab.
delay 1.001447
set timeoutSeconds to 2.000000
set uiScript to "click radio button 2 of tab group 1 of window \"Applications\" of application process \"Finder\""
my doWithTimeout( uiScript, timeoutSeconds )

-- Click the “<fill in title>” tab.
delay 0.619061
set timeoutSeconds to 2.000000
set uiScript to "click radio button 2 of tab group 1 of window \"Debug\" of application process \"Finder\""
my doWithTimeout( uiScript, timeoutSeconds )

-- Click the “<fill in title>” tab.
delay 0.787464
set timeoutSeconds to 2.000000
set uiScript to "click radio button 1 of tab group 1 of window \"Applications\" of application process \"Finder\""
my doWithTimeout( uiScript, timeoutSeconds )

-- Click the “<fill in title>” radio button.
delay 0.720214
set timeoutSeconds to 2.000000
set uiScript to "click radio button 1 of radio group 1 of group 2 of tool bar 1 of window \"Demo\" of application process \"Finder\""
my doWithTimeout( uiScript, timeoutSeconds )

-- Click the “<fill in title>” button.
delay 1.372573
set timeoutSeconds to 2.000000
set uiScript to "click UI Element 1 of group 1 of group 1 of tool bar 1 of window \"Demo\" of application process \"Finder\""
my doWithTimeout( uiScript, timeoutSeconds )

on doWithTimeout(uiScript, timeoutSeconds)
	set endDate to (current date) + timeoutSeconds
	repeat
		try
			run script "tell application \"System Events\"
" & uiScript & "
end tell"
			exit repeat
		on error errorMessage
			if ((current date) > endDate) then
				error "Can not " & uiScript
			end if
		end try
	end repeat
end doWithTimeout