Tuesday, December 12, 2017

#1 2007-04-02 04:00:06 am

Kevin Wolfe
Member
From:: Cincinnati OH USA
Registered: 2005-10-27
Posts: 67
Website

Automator Bindings

For those working on Automator actions, I wanted to talk about the simple elegence of the bindings system for actions. (for those unfamiliar with Automator I offer the following two links: Automator Actions, and Apple's Automator page) The main pieces of background knowledge one should have to make an action with Applescript are Applescript, using XCode/InterfaceBuilder basics, bindings, property lists, (and to use, put in some Library/Automator folder on a machine running 10.4 or later ^_^ ) This is on bindings, which allows you to get the values set by the user in the Automator application to use in your script; for other information, look around our site.

My (extremely simple) example Automator action is one that takes it's input, displays a dialog with "display dialog" of the input and returns the input (basically letting you peek at what's going down the "pipe").  It allows editing of the button text for "confirm", "cancel", and "other" buttons, as well as a check box for whether or not to include the "other" button.

(Background Step: create a new "applescript pipeline stage" from the template of a similar name in XCode, name it and place it wherever you like.)

open the main.nib file (in resources) and place text views and labels as below on the view provided (note the bottom label is actually a checkbox & it's built in label

http://files.macscripter.net/unscripted/aa/pic1.jpeg


Make sure that you set their springs in the "size" inspector to like this so they stretch properly:

http://files.macscripter.net/unscripted/aa/pic2.jpeg


and make sure the containing view's springs are set with no outer springs and both sets of inner springs on?

Then with the "Parameters" object in the instance tab of the main.nib window, go to the Attributes inspector and add keys like this:

http://files.macscripter.net/unscripted/aa/pic3.jpeg


(I selected the "Automatically prepares content" button in my final version, but I don't think this is necessary.)

Then select the input text view for the confirm button and go to the bindings inspector and set it up like so:

http://files.macscripter.net/unscripted/aa/pic4.jpeg


Basically just click the "value" binding and select the correct model key path. (oh, and change some of the checkboxes)You don't have to worry about the placeholders because we'll set default values later. Repeat this with the cancel button box and other button box setting model key path appropriately.

Next select the check box for the other button and set it's "value" bindings like so:

http://files.macscripter.net/unscripted/aa/pic5.jpeg


Once again don't worry about the placeholders as well give a default later.

Select the "other button" text field again and in the bindings inspector set it's "enabled" bindings like so:

http://files.macscripter.net/unscripted/aa/pic6.jpeg


This way the "other button's" text will be disabled when the check box isn't checked! you can run and test this in IB right now (that the checkbox enables/disables the other button box, and that you can type and stretch the view as expected (don't worry too much about this, as automator is generally a fixed size, and so are the subviews you make will be an expected size))

Next quit IB and in XCode edit the script... something like this:

Applescript:


on run {input, parameters}
   set buttonList to ({|confirmButton|, |cancelButton|} of parameters)
   set defaultButton to |confirmButton| of parameters
   
   if |showOtherButton| of parameters then
       set buttonList to buttonList & (|otherButton| of parameters)
   end if
   
   display dialog input buttons buttonList default button defaultButton
   
   return input
end run

Note that the keys are enclosed in pipes ("|") but other wise reflect exactly what you put into the parameters object in IB. I am pretty sure any unicode string will work (for IB) as the Parameter object just wraps an NSMutableDictionary with an NSObjectController (but haven't tried yet, so don't cry to me if it doesn't ^_^) This means you should be able to put spaces and other special chars in your key's name if you like.

Finally we should get the info.plist to tell the action it's defaults; here's the relevant part as seen in "Property List editor"

http://files.macscripter.net/unscripted/aa/pic8.jpeg


There are only a couple more bits and pieces to building an action and using it, but I wanted to highlight the details regarding bindings.

Automator Bindings Recipe

First, set up keys in the parameters object in IB's attributes palette, (valid keys should be any string, but I only tested keys matching /[a-zA-Z]+/, which is all you'll need in most occasions unless your sense of coding style differs from mine ^_^)
Second, Bind "value"s by simply selecting the view and in it's bindings inspector select the model key in the combo box provided and deselect the "Conditionally Sets x" boxes. (selecting continuously updates value/validate immediately is recommended but not required... also feel free to set "Attribute" and other bindings.... default values will be given in the plist in step 4)
Third, To access a key in a script, use the one you typed into the Parameter object's attribute palette surrounded with "|" (pipe/vertical bar) as properties of the parameters object passed to your script.
Finally set default values in the info.plist file in the PLDefaultParameters dictionary with keys the same as the ones you set in IB, and values as you want them to have...

Other things that you should do to complete this project are mostly to do with the info.plist metadata, and installation.  Those are separate issues, and I may cover the plist entries in a separate article (installation shouldn't be a problem: place in Library/Automator). this was about bindings ^_^


Filed under: Automator, Wolfe, Bindings

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)