Jump to content

[HOW TO] Package a workflow via the command line


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 it 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. Alfred knows not to save the value of those variables 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.

 

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"

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

ditto -ck "${workflow_dir_to_package}" "${workflow_file}"


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, package your workflow directory without any modifications. Our work is done.
  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 copied directory. Our work is done.

 

Solution as a script

#!/bin/bash

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.'
  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)"
  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

ditto -ck "${workflow_dir_to_package}" "${workflow_file}"
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.

Edited by vitor
Removed dead links
Link to comment
  • 1 year later...

I have some command line experience, but I’m mostly a neophyte. I understand about 75% of this post, but I’m not exactly sure how to apply it to a repo and end up with a finished product. Is there somewhere I can learn/read more about the necessary knowledge that isn't presented here? I tried to run the first through the command line and didn’t end up with anything, and tried to run the second as an AppleScript and had no luck either.

The ditto and zip links are dead as well.

Last login: Fri Jun 15 14:09:57 on ttys000
****:~ m******$ readonly workflow_dir="{{/Users/******/Desktop/workflow}}"
****:~ m******$ readonly output_file="{{/Users/******/Desktop/workflow}}.alfredworkflow"
****:~ m******$ 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
****:~ m******$ ditto -ck "${workflow_dir_to_package}" "${workflow_file}"
ditto: : No such file or directory
****:~ m******$

And running AS:

 

Screen Shot AppleScript.jpeg

Edited by Neberheim
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...