How do you select a particular named layer within a canvas to add objects to using AppleScript in Omnigraffle?

Hi there,

I’m creating a wireframing template which has several named layers for various purposes and using a script to auto-generate a table of contents.

Because I’ve been evolving this template to include some additional content layers, I want to make sure the Omnigraffle text and line objects are drawn to a specifically named layer.

How do I make sure that the objects I’m creating get drawn to the layer I want them to?

Looking at the objects within the Layer object in the Applescript dictionary, there are only three properties: visible, locked, printable. Nothing to set it active.

There doesn’t seem to be anything within the Canvas object either that has to do with setting an active layer… it just has the collection of layer objects.

Does anyone know how to do this?

1 Like

See below an example, adapted from an earlier post, in which we first create and populate one named layer, and then another.

Essentially, you can reference layers by their name, and wrap the make shape, make line etc instructions within tell layer ... end tell lines:

use framework "Foundation"

property perFont : "GillSans"
property seatSize : 42
property Gap : 3
property seatSpace : seatSize + Gap

on run
    set memberCount to 15
    set tPerim to memberCount * (seatSpace)
    set tRadius to tPerim / (2 * pi)
    if tRadius < 40 then set tRadius to 60
    set attList to {"GBa", "JMi", "PMi", "DMa", "SMi", "PDo", "RVi", "JMi", "PMi", "DMa", "SMi", "PDo", "RVi", "JMi", "PMi", "DMa", "SMi", "PDo", "RVi", "JMi", "PMi", "DMa", "SMi", "PDo", "RVi"}
    tell application id "com.omnigroup.OmniGraffle6"
        tell canvas of front window
            # draw circular table
            set xOrigin to 150
            set yOrigin to 250
            set tDiam to 2 * tRadius
            set pointList to {0, xOrigin, yOrigin}
            make new layer at end with properties {name:"Table"}
            --    select layer id 2
            tell layer "Table"
                make new shape with properties {name:"Circle", textSize:{0.8, 0.7}, draws shadow:false, size:{tDiam, tDiam}, origin:{xOrigin, yOrigin}, textPosition:{0.1, 0.15}}
            end tell
            # now draw seats
            # - set coords of centre of table
            --set tRadius to tRadius / 2
            set xtCentre to xOrigin + tRadius
            set ytCentre to yOrigin + tRadius
            set halfSeat to seatSize / 2
            make new layer at end with properties {name:"Seats"}
            --select layer 1 ??
            repeat with i from 1 to memberCount
                #functions are in radians
                set tAngle to (i * (360 / memberCount))
                set tRadians to tAngle * (2 * pi) / 360
                # set chair offsets from centre
                set Hoff to (tRadius + halfSeat + Gap) * (my cos(tRadians))
                set Voff to (tRadius + halfSeat + Gap) * (my sin(tRadians))
                #determine coords of seat 
                set xCentre to xtCentre + Hoff
                set yCentre to ytCentre + Voff
                -- Delta
                set xPos to xCentre - halfSeat
                set yPos to yCentre - halfSeat
                # get member initials
                set perINIT to text item i of attList
                # draw a line from table centre to Chair origin
                tell layer "Seats"
                    make new line with properties {point list:{{xtCentre, ytCentre}, {xCentre, yCentre}}, thickness:0.25}
                    set shapeRef to (make new shape with properties {name:"AndGate", textSize:{1.0, 0.8}, draws shadow:false, size:{seatSize, seatSize}, text:{alignment:center, font:perFont, text:perINIT}, textRotation:90, rotation:tAngle, origin:{xPos, yPos}, thickness:0, textPosition:{0.0, 0.1}, vertical padding:0, fill color:{0.0, 1.0, 1.0, 0.304984}})
                end tell
            end repeat
        end tell
    end tell
end run

-- evalOSA :: ("JavaScript" | "AppleScript") -> String -> String
on evalOSA(strLang, strCode)
    set ca to current application
    set oScript to ca's OSAScript's alloc's initWithSource:strCode ¬
        |language|:(ca's OSALanguage's languageForName:(strLang))
    set {blnCompiled, oError} to oScript's compileAndReturnError:(reference)
    if blnCompiled then
        set {oDesc, oError} to oScript's executeAndReturnError:(reference)
        if (oError is missing value) then return oDesc's stringValue as text
    end if
    return oError's NSLocalizedDescription as text
end evalOSA

on cos(x)
    evalOSA("JavaScript", "Math.cos(" & (x as string) & ")") as real
end cos

on sin(x)
    evalOSA("JavaScript", "Math.sin(" & (x as string) & ")") as real
end sin
1 Like

Perfect! Thank you!

tell layer [layername]
– do stuff on that layer
end tell

That worked perfectly!

1 Like