In Automator, there is a way to quickly convert an Action into its equivalent Applescript in Automator, I think by dragging it in some tricky way. I saw this some time ago but have now forgotten. I've tried searching and can't find it.
3 Answers
In case anyone has this question, I found it, but it only works for recorded actions.
After you Record some clicks or other actions in Automator, they appear in a "Watch Me Do" action. You can then click on and drag an individual action out of that Action and release when the + sign appears. Automator will add a "Run AppleScript" action with the AppleScript that sends the same clicks and key presses to System Events.
You can also simply Copy the action you want, switch over to AppleScript Editor, and paste to get the same code.
Example: "Click Skype in the Dock" Event converts to the following AppleScript:
on run {input, parameters}
-- Click “Skype” in the Dock.
delay 7.872251
set timeoutSeconds to 2.000000
set uiScript to "click UI Element \"Skype\" of list 1 of application process \"Dock\""
my doWithTimeout( uiScript, timeoutSeconds )
return input
end run
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
How to convert recorded events in Automator:
-
I up voted this on its merits of how you gather AppleScript code from a Watch Me Do action however you said "You can also simply Copy the action you want, switch over to AppleScript Editor, and paste to get the same code." and while this might be true in some cases nonetheless it's not true in some cases as the AppleScript Code cannot always be copied exactly from Automator in this manner to AppleScript Editor and run successfully without modifying the code. Commented Jan 12, 2017 at 11:56
It's impossible. You can do similar actions in applescript, but there is no way to convert an existing automator script to applescript.
-
Might be generally true (I don't know), but it is possible for the recorded actions ("Watch Me Do") - see my solution below.– NateCommented Jan 12, 2017 at 2:48
Create your Automator file as an "Application" type.
Tell your AppleScript to run your Automator app.
Something like:
open "/Users/george/AutomatorFiles/MyScript.app"
Works for me!
-
That's useful too, though not quite what I was looking for, which was to see the code so I can understand and reuse it.– NateCommented Jan 12, 2017 at 2:47