Jump to content

How to package workflows via the command line


vitor

Recommended Posts

Introduction

 

Alfred workflows are simply zipped directories with an altered extension. Those are great news if you want to automate packaging your workflows for distribution, as you can simply (using ditto in this example, but zip would work just as well):

 

ditto -ck "{{/path/to/your/workflow_directory}}" "{{/path/to/your/output_file}}.alfredworkflow"


And that’s it. A zipped directory of your workflow with the custom .alfredworkflow extension. Names enclosed in double curly brackets are examples (they dependend on your situation and must be edited accordingly).

 

Problem


The trouble with that solution is that it packages your workflow as is. Most of the time that won’t be a problem, but it becomes one when you have Workflow Environment Variables with “Don't Export” activated or have made local changed to your configuration. Alfred knows not to save the value of those variables or your custom configuration when exporting your workflow, but your zipping utility does not. Because of that, if you want to package your workflow yourself you need to set those variables (and only those) to empty values and exclude the prefs.plist file.

 

Solution

 

# You need only set these two variables, and the rest will work as is
readonly workflow_dir="{{/path/to/your/workflow_directory}}"
readonly output_file="{{/path/to/your/output_file}}.alfredworkflow"

# Leave this section as is
if /usr/libexec/PlistBuddy -c 'Print variablesdontexport' "${workflow_dir}/info.plist" &> /dev/null; then
  readonly workflow_dir_to_package="$(mktemp -d)"
  cp -R "${workflow_dir}/"* "${workflow_dir_to_package}"

  readonly tmp_info_plist="${workflow_dir_to_package}/info.plist"
  /usr/libexec/PlistBuddy -c 'print variablesdontexport' "${tmp_info_plist}" | grep '    ' | sed -E 's/ {4}//' | xargs -I {} /usr/libexec/PlistBuddy -c "set variables:'{}' ''" "${tmp_info_plist}"
else
  readonly workflow_dir_to_package="${workflow_dir}"
fi

DITTONORSRC=1 /usr/bin/ditto -ck "${workflow_dir_to_package}" "${workflow_file}"
/usr/bin/zip "${workflow_file}" --delete 'prefs.plist' > /dev/null


So, what does this do?

  1. Check the info.plist in your workflow directory for the variablesdontexport property.
  2. If the property does not exist, jump to step 5.
  3. If the property exists, copy your entire workflow to a temporary directory (we’ll make changes to the info.plist and don’t want to do them on our current one).
  4. Check every variable in variablesdontexport and set them to empty values in the info.plist of the copied directory (we don’t want to outright delete the variables as the reference would be lost as well).
  5. Package the directory to a workflow.
  6. Delete the `prefs.plist`, if it exists, from the packaged workflow. Our work is done.

 

Solution as a script

 

#!/bin/zsh

readonly workflow_dir="${1}"
readonly info_plist="${workflow_dir}/info.plist"

if [[ "$#" -ne 1 ]] || [[ ! -f "${info_plist}" ]]; then
  echo 'You need to give this script a single argument: the path to a valid workflow directory.'
  echo 'The workflow will be saved to the Desktop.'
  exit 1
fi

readonly workflow_name="$(/usr/libexec/PlistBuddy -c 'print name' "${info_plist}")"
readonly workflow_file="${HOME}/Desktop/${workflow_name}.alfredworkflow"

if /usr/libexec/PlistBuddy -c 'print variablesdontexport' "${info_plist}" &> /dev/null; then
  readonly workflow_dir_to_package="$(mktemp -d)"
  /bin/cp -R "${workflow_dir}/"* "${workflow_dir_to_package}"

  readonly tmp_info_plist="${workflow_dir_to_package}/info.plist"
  /usr/libexec/PlistBuddy -c 'Print variablesdontexport' "${tmp_info_plist}" | grep '    ' | sed -E 's/ {4}//' | xargs -I {} /usr/libexec/PlistBuddy -c "Set variables:'{}' ''" "${tmp_info_plist}"
else
  readonly workflow_dir_to_package="${workflow_dir}"
fi

DITTONORSRC=1 /usr/bin/ditto -ck "${workflow_dir_to_package}" "${workflow_file}"
/usr/bin/zip "${workflow_file}" --delete 'prefs.plist' > /dev/null
echo "Exported worflow to ${workflow_file}."


To use that script, give it a single argument: the path to the workflow you want to package. It’ll save it to the Desktop with the name automatically determined.

Link to comment
Share on other sites

  • vitor changed the title to How to package workflows via the command line
  • vitor locked this topic
Guest
This topic is now closed to further replies.
×
×
  • Create New...