Alfred Cron

A workflow that allows for a fake cron to be run and managed through Alfred 2

View the Project on GitHub shawnrice/alfred-cron

Note: you should consider Alfred Cron to be more of a beta right now.

Alfred Cron is not a true cron

This is not a true cron in that it doesn’t run commands at a specified time. Why? Cron doesn’t make sense to have on most regular computers; for servers, it makes sense because the servers are always on. If your laptop is off, closed, or asleep, then it won’t run a cron command at the correct time. The same goes with a desktop Mac.

So, if want a command to be run daily at midnight, then it might not happen. Hence, Alfred Cron uses an “interval” system. So, if you want something run once per day, then it will run the command if it hasn’t been run for a day, which makes sense for a user computer.

Timed commands are tentatively planned for a future release, but most of the workflow needs to be rewritten to accommodate them.

Alfred Cron in action

animated screenshot

Add / Edit / Enable / Disable / Delete Jobs

Alfred Cron uses Pashua to help add and edit jobs with a nice GUI. After adding a job, it will automatically be enabled. If you want to pause a job for a while, then just disable it. If a job returns an error code, then it will be disabled, and you’ll be sent a notification that the job had an error. You can also delete jobs. Remember, deleted jobs are unrecoverable.

How do I define a cron “job”?

Jobs are written as bash commands. So, you can write full scripts, or you can write simple commands to invoke other scripts. You can even invoke Alfred Workflows via the new External Trigger workflow object.

Run a python script

If you want to run a python script (this applies to any kind of script), then you just have to have

python “/path/to/python/script.py” "arg"
as the job.

Since you can use Bash variables, you could also write something like

python "$HOME/Desktop/script.py" "args"

Bash Script

So, if you want to write a bash script, then you can do it in the “add job” dialog box.

Two example scripts are included under the "Examples" folder in the workflow directory. These might need to be tailored to your own needs. Here is one:

  #!/bin/bash

  # This script uses a nice little utility called "tag" that can be installed via
  # Homebrew with 'brew install tag'; or you can find the project on Github
  # https://github.com/jdberry/tag

  # We need to set the IFS (separator character) to deal with filenames with spaces.
  IFS=$'\n'

  # Within the last week
  # Let's color file Green
  for file in $(find -E "$HOME/Downloads" -type f -maxdepth 1 -mtime -7 -regex '.*(png|gif|jpg|jpeg|bmp|svg|tiff|tif)$');
  do
    tags=`tag -l "$file" | sed 's|'"$file"'||g'`
    if [ -z $(echo $tags | grep Green ) ]; then
      tag -a "Green" "$file"
    fi
    if [ ! -z $(echo $tags | grep Red ) ]; then
      tag -r "Red" "$file"
    fi
  done


  # Older than one week but within the last month
  # Let's remove the tags
  for file in $(find -E "$HOME/Downloads" -type f -maxdepth 1 -mtime +7 -mtime -30 -regex '.*(png|gif|jpg|jpeg|bmp|svg|tiff|tif)$');
  do
    tags=`tag -l "$file" | sed 's|'"$file"'||g'`
    if [ ! -z $(echo $tags | grep Green ) ]; then
      tag -r "Green" "$file"
    fi
    if [ ! -z $(echo $tags | grep Red ) ]; then
      tag -r "Red" "$file"
    fi
  done


  # Older than one month
  # Let's remove the old colors and move them to the "~/Pictures" folder
  for file in $(find -E "$HOME/Downloads" -type f -maxdepth 1 -mtime +30 -regex '.*(png|gif|jpg|jpeg|bmp|svg|tiff|tif)$');
  do
    tags=`tag -l "$file" | sed 's|'"$file"'||g'`
    if [ ! -z $(echo $tags | grep Green ) ]; then
      tag -r Green "$file"
    fi
    if [ ! -z $(echo $tags | grep Red ) ]; then
      tag -r Red "$file"
    fi
    mv "$file" "$HOME/Pictures"
  done

Invoking an Alfred workflow via an External Trigger

So, to run a workflow via an external trigger, you just need to run some Applescript. If the script is saved as a file, then just run

osascript "/path/to/script.scpt"

If the script is short, then you can run it as a single command. This code below uses an example workflow that can be found here.

osascript -e 'tell application "Alfred 2" to run trigger "lt" in workflow "alfred.notifier.spr" with argument "This is a notification."'

Job Errors

If a job throws an error, then Alfred Cron will automatically disable the job and flag a warning. A notification will appear that the script failed, and you will see an error message when you invoke Alfred Cron.

Logs

A log file exists for you to peruse in the Alfred Cron data directory. Log files should not exceed 2mb. When a log file hits the limit, then it will be renamed to alfred-cron.log.old, and a new file will be created. Only one older log is kept.

Alfred Cron doesn't start on boot

Alfred Cron won't run automatically when you boot up your computer or when you login. This feature is planned for a future release.

Alfred Bundler

Alfred Cron uses the Alfred Bundler to download and use libraries and utility apps (Pashua, Terminal Notifier). So, if you’re running the Alfred Cron workflow initially, it might take a few minutes to set itself up. Also, if you have Gatekeeper turned on, then you will be prompted to enter an Administrator password to “whitelist” the utility applications.

Tips

License

Alfred Cron is licensed under the GNU General Public License v3. So, if anything goes wrong with your computer (or your life) because of this workflow, then then I'm not responsible. So, make sure you check and test your scripts before you add them to the cron.