Jun 22, 2015

Mac OS X Automator / AppleScript Service for AddOns

Automate AddOn Downloads from Curse

Curse Mac Client Support

The Mac OS X gets no love from AddOn update scripts. I guess I could bitch and moan about this, but really, what would that accomplish?  Instead, let's solve the issue and move on (and indulge my inner wannabe programmer). My solution?


Curse Updates

I have an account with Curse and I've flagged my AddOns as favorites. I'll check every few days to see if my AddOns have been updated, and download the newer copies.
Curse My Favorites
Safari then extracts the zip files and places these in my Downloads folder (if using Chrome, you'll need to unzip first -- or get fancy and add a check to the AppleScript for zips).
Normally, I'd have to manually move these AddOns to my Warcraft / Interface / AddOns folder -- not a big deal, but kind of a pain in the ass.  How to resolve?  Well, I can't fix Curse's lack of notification, but I can address the movement from Downloads to AddOs. How? Automator with AppleScript!

Automator Work Flow

First, I created a new Automator Service. This will add my custom service to the context menu, so when I select multiple downloaded AddOns within my Downloads, I have the option to "Move WoW AddOns" and they automagically move! Pretty slick, eh?

The beauty of this, at least to me, is that Automator handles the selection of multiple objects (folders) and passes that selection to an AppleScript. The AppleScript then checks if the AddOn folder is a standalone AddOn (like TradeSkillMaster) or a parent file containing multiple standalone AddOns (like DBM). The AppleScript then moves the AddOn folder to the AddOn directory and cleans up after itself.

The Automator workflow is a service that receives folders in Finder, and only has two steps: Get Selected Finder Items and Run AppleScript.

Automator Service Settings

The very first thing to establish is that this workflow will receive the selected objects (option click or select all, your mileage may vary) in Finder.  For me, this is the AddOns I just downloaded from Curse.com. This selection is then passed to the next action in the workflow.
Service Settings

Get Selected Finder Items

The entire purpose here is to capture the objects selected and pass them to the AppleScript.
Get Selected Finder Items Action

Run AppleScript

This is the meat of the workflow. It uses a basic if ... else construct to determine if the selected object (passed as a collection from Finder) is a standalone AddOn or a parent collection of standalone AddOns.
Run AppleScript

When you add the "Run AppleScript" action, a default code structure is placed. Replace that with the following AppleScript.

(* This script will process each item selected within Finder and process the item (assuming each item is an AddOn folder or a folder with a collection of AddOns).
With each folder in the selected items, check that the folder contains a file with the lua extension. 
If the selected item (variable this_item) contains zero lua files then it is a parent folder containing multiple AddOns (like DBM).  In this case, begin a second loop over the folders within the item (the variable more_items) and move each folder to the standard AddOns folder. Once all more_items are processed, the parent directory (this_item) is deleted from the source folder.
Hack: a move screws up the count of files so the more_items object is duplicated to the AddOns directory instead of being moved.
Otherwise, the selected item (this_item) contains one or more lua files and is considered a as a standalone AddOn, and is moved to the standard AddOns folder *)

on run {input, parameters}
 tell application "Finder"
  -- define desintation folder
  set wow_addons_folder to folder "AddOns" of folder "Interface" of folder "World of Warcraft" of folder (path to applications folder) as alias
  -- display dialog "wow_addons_folder: " & wow_addons_folder
  set selected_items to selection
  repeat with this_item in selected_items
   -- display dialog "this_item: " & this_item
   set this_item_alias to this_item as alias
   -- count the children objects with a lua file extension
   set number_of_files to count of (files in folder this_item_alias whose name extension is "lua")
   --display dialog "number_of_files: " & number_of_files
   if number_of_files is equal to 0 then
    -- no lua objects, so assume a parent addon folder containing a collection of stand alone addon files. start secondary looping
    repeat with another_item in this_item
     -- display dialog "another item: " & another_item
     -- display dialog "Moving " & (name of another_item) & " to " & (name of wow_addons_folder)
     -- hack: move screws up the count and causes errors. duplicate this object instead of moving it.
     duplicate another_item to wow_addons_folder with replacing
    end repeat
    -- after processing each of the children, remove the parent object to avoid moving empty folders to addons folder
    -- display dialog "deleting " & this_item
    delete this_item
   else
    -- display dialog "Moving " & (name of this_item) & " to " & (name of wow_addons_folder)
    move this_item to wow_addons_folder with replacing
   end if
  end repeat
 end tell
 return input
end run

Save As Service

The final step is to save the service so that it's available in your services context menu. I'm naming mine "WoW AddOns".
Save Service as "WoW AddOns"

Now, I can context menu or services icon to bulk process the selection.
Yes, I'm that lazy
Now that my AddOns are updated, time to log in and chase the next achievement!

No comments:

Post a Comment