Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abathur/46ee83deac0a966b886f092537983a83 to your computer and use it in GitHub Desktop.
Save abathur/46ee83deac0a966b886f092537983a83 to your computer and use it in GitHub Desktop.
Script to install and activate a macos Automator folder action workflow. This isn't fully unattended. You'll have to click through dialogs.

My macos system/dotfile bootstrap process uses brew bundle to install some apps. This takes a while to complete, so I hoped to use an Automator Folder Action workflow to watch the /Applications/ directory and launch a few apps that require logins as soon as brew bundle finishes installing them.

Automator did not do a good job of this (I did 3 test runs and it never opened each of the apps in the trigger list), but I didn't realize that until after I figured out how to actually install and activate the workflow during my bootstrap process. I wanted to go ahead and share the bits of this process in the hope someone else trying to accomplish the same will need a little less work.

Some notes:

  1. Numbered <#>_ file prefixes just for ordering in gist.
  2. I exported my workflow and saved it with the rest of my dotfiles.
  3. I'm also exporting the applescript to its plaintext format. If you just paste this into ScriptEditor and save, you'll end up with a .scpt file.
  4. If you found this because of my higher level goal (opening apps as they install in a long-running process like brew bundle), see the await_app.sh file.
# install the workflow itself
# this will trigger a dialog (two steps, IIRC)
open -W <your workflow>.workflow
# run applescript to associate the workflow with your folder(s)
# Unless you previously granted it, this should prompt you to grant:
# Automation privilege to Terminial for Folder Actions Setup
osascript install_folder_actions.applescript
# do whatever you wanted the action active for; in my case it was:
# brew bundle --global --quiet
property pathToFolderActions : ((path to workflows folder as text) & "Applications:Folder Actions:")
tell application "Folder Actions Setup"
launch
activate
if not folder actions enabled then
set folder actions enabled to true
end if
try
set addedFolderAction to make new folder action with properties {name:"<your folder>", path:"<your folder path>"}
end try
try
delay 0.5
tell addedFolderAction to make new script with properties {name:"<your workflow>", path:(pathToFolderActions & "<your workflow>.workflow")}
end try
delay 5
quit
end tell
# This file is *NOT* related to Automator workflows or folder actions.
# It just demonstrates how I ended up opening apps as the install finishes
# args: "App Name" "Reminder message"
function _todo_popup()
{
osascript -e "display dialog \"$2\" with title \"$1 TODO\"" &> /dev/null
}
function _wait_for_file(){
until [ -d "$1" ]; do # -d file True if file exists and is a directory.
# bootstrap is a slow process, so I'm using a long 10s check interval
sleep 10
done
}
# args: "App Name" (from the file name in /Applications/, excluding extension
function _wait_for_app(){
_wait_for_file "/Applications/$1.app"
}
# args: "App Name" "Reminder of what I'm supposed to do once it opens"
function _launch_installed_app()
{
_wait_for_app "$1"
open -a "$1" # -a flag opens an app by name
_todo_popup "$1" "$2"
}
function _launch_installed_apps_for_action()
{
# & sends each to background
_launch_installed_app "Evernote" "Log in" &
_launch_installed_app "Spotify" "Log in, toggle playlist downloads" &
_launch_installed_app "Microsoft Teams" "Log in" &
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment