How to create a new Automator workflow via AppleScript

Automator is pretty goofy even on a good day. The main things to look for when programmatically creating a workflow are an action's bundle identifier and its settings. These can be found by looking at the action's Info.plist, or by using something like the following in the Script Editor:

tell application "Automator"
   set actionID to first item of (get id of Automator actions whose name is "Run Shell Script") -- or whatever
   log result
   log (get name of settings of Automator action id actionID)
end tell

For the Run Shell Script action, those would be "com.apple.RunShellScript" and {"inputMethod", "CheckedForUserDefaultShell", "source", "COMMAND_STRING", "shell"}.

The settings are whatever is in the action's user interface, and vary depending on what the action does and the settings it exposes. For the Run Shell Script action, the main ones would be "COMMAND_STRING", which is the script text in the text field, and "inputMethod", which is an index into the Pass input: menu item.

Another item to possibly keep track of is the index of the action in the workflow, but unless you are going to be moving around the actions, just adding to the end of the workflow will most likely be all you need to do.

There doesn't appear to be a property to set the type of document, but for a regular workflow you can accept the default with a keystroke, or just manually set the workflow type after the workflow is created behind Automator's choice sheet.

After all that, a script to create an Automator workflow with a Run Shell Script action would be something like:

set theScript to "for f in \"$@\"
do
   echo \"$f\"
done" -- the shell script

tell application "Automator"
   set actionID to Automator action id "com.apple.RunShellScript"
   tell (make new workflow)
      add actionID to it -- add to the end of the workflow
      tell last Automator action
         set value of setting "inputMethod" to 1 -- arguments menu
         set value of setting "COMMAND_STRING" to theScript
      end tell
   end tell
   activate
   # tell application "System Events" to keystroke return -- default workflow
end tell