Skip to content

Dock Icon Changer

One of my FaceSpan 4 examples demonstrated how to manipulate an application’s Dock icon. In this example, I rewrite that example using FaceSpan 5. Here’s the original code:

property pAppIcon : missing value
property pSmileImage : missing value
property pCoolImage : missing value

on awake from nib theObject
    -- cache the images we'll be using
    set pAppIcon to load image "NSApplicationIcon"
    set pSmileImage to load image "smile"
    set pCoolImage to load image "cool"
end awake from nib

on clicked theObject
    local theName

    -- For radio buttons, theObject is the cell that was clicked
    -- In our case, we have named the cells 'none', 'smile' and 'cool'

    set theName to name of theObject
    if theName is "smile" then
        set image of image view "preview" of window "main" to pSmileImage
        set icon image to pSmileImage
    else if theName is "cool" then
        set image of image view "preview" of window "main" to pCoolImage
        set icon image to pCoolImage
        -- otherwise, reset the dock icon to the application's icon
        set image of image view "preview" of window "main" to my pAppIcon
        set icon image to my pAppIcon
    end if
end clicked

Much of this code is concerned with managing the images that appear in the example. The balance of the code responds to radio button selections by altering the images in the ‘preview’ image view and the application’s dock icon.

Here’s the code rewritten to take advantage of FaceSpan 5:

on action theObject
    if my id is theObject's id then
        local theImageName

        --  Read the dockImageName property of the selected radio button
        set theImageName to my selection's dockImageName

        --  Display the image in the preview view and the dock icon
        set my application's dock image to theImageName
        set image of my window's preview to my application's dock image
    end if
end action

You’ll notice the following differences (beyond its being considerably shorter):

  • the code deals in the names of images (located within the application bundle) rather than having to explicitly load images, and retain references to the loaded images.
  • references to the “preview” image view are made using the “preview” property of the window object rather than having to give full object specifiers (though that is possible).
  • the code reads the name of the dock image from “dockImageName” property defined in each radio button object.
  • the implicit scope in FaceSpan 5 is the object to which the code belongs (a box object in this case) rather than the application as is the case in FaceSpan 4 (and AppleScript Studio).

Here’s how the project appears in the FaceSpan IDE:


Its not enough to simply recreate the FaceSpan 4 example in FaceSpan 5. Lets take advantage of some additional dock functionality: FaceSpan 5 allows you to add progress and count badges.

Here’s the code that adds a Safari-style progress bar to the Dock icon:

    set my application's dock progress value to 0.8 -- 80%
    set my application's dock progress style to Safari progress

And here’s the code that adds a Mail style count to the Dock icon:

    set my application's dock count value to 2007
    set my application's dock count style to Mail count

When its all put together, here’s how it appears in the running application:

And here is the finished project: Dock Icon Change (FS5).