BibDesk Script Hook Sample 1

Here is an example for an AppleScript you can set for a Change Field script hook. It generates the "Url" field from a custom "Aid" field when the value of the "Aid" field is set in the editor.

property theFieldToSet : "Url"
property theFormatString : "{Aid}"
property theRequiredFields : {"Aid"}

using terms from application "BibDesk"
    on perform BibDesk action with publications thePubs for script hook theScriptHook
        -- get some properties from the script hook
        set theName to the name of theScriptHook
        set changedField to the field name of theScriptHook
        -- check if we get the right script hook event
        if theName is not "Change Field" then return
        if changedField is not in theRequiredFields then return
        repeat with aPub in thePubs
            tell aPub
                -- check that we can set the field
                set shouldSet to true
                if (value of field theFieldToSet is not "") then
                    set shouldSet to false
                    repeat with aField in theRequiredFields
                        if (value of field aField is "") then
                            set shouldSet to false
                        end if
                    end repeat
                end if
                if shouldSet then
                    -- generate a new value for the field
                    set theField to field theFieldToSet
                    set newValue to parse format theFormatString for theField
                    if newValue is not "" then
                        set value of theField to newValue
                    end if
                end if
            end tell
        end repeat
    end perform BibDesk action with publications
end using terms from

The main thing that should be done in the script is to define a `perform BibDesk action with publications' subroutine to handle the script hook. An important thing to note is that you should bracket the whole subroutine in `using terms from application "BibDesk"', so that certain AppleScript definitions (such as the subroutine name) are recognized.

For another sample script, look in the Scripts Menu of BibDesk. To view its source, select the "Sample Script Hook" menu item with the Option key held down.