The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniGraffle General (http://forums.omnigroup.com/forumdisplay.php?f=10)
-   -   applescripting help for graphics on shared layers (http://forums.omnigroup.com/showthread.php?t=22828)

supertwang 2011-12-07 03:30 AM

applescripting help for graphics on shared layers
 
I'm trying to write an Applescript to export all graphics in a document, including ones that lie on shared layers. The script works well, except for the shared layer graphics. For those I get an export of the entire canvas, rather than just the shared graphic.

Is this a bug, or am I doing something wrong?

heres the script

[CODE]
on getExportGraphics(export_folder)
set foundGraphics to {}
set sharedDone to {}
tell application "OmniGraffle Professional 5"
activate

set oDoc to front document
set theWin to front window


set canvasCount to count of canvases of oDoc

set origCanvas to canvas of front window

set theCanvases to every canvas in oDoc

set cindex to 0
repeat with currentCanvas in every canvas of document of front window
set canvas of front window to currentCanvas
set canvasName to name of currentCanvas
set canvasId to id of currentCanvas
-----

repeat with currentLayer in every layer of currentCanvas

set layerName to name of currentLayer

set layerClass to (class of currentLayer) as string
log "LAYER CLASS: " & layerClass
if (layerClass is "shared layer") then
set layerkey to "_shared|" & layerName
set done to 0

repeat with di from 1 to count of sharedDone
set formerlyDone to item di of sharedDone
if (formerlyDone = layerkey) then
set done to 1
set di to count of sharedDone
end if
end repeat
if (done is 0) then
set end of sharedDone to layerkey
end if
set sharedLayer to true
else
set layerkey to canvasName & "|" & layerName
set done to 0
set sharedLayer to false
end if


if (done is 0) then

-- NOTE: top-level groups are present in the (graphics of current layer)
set numGraphics to (count of graphics of currentLayer)
set numGroups to (count of groups of currentLayer)
--display dialog num

log "Canvas[" & canvasName & "] Layer: " & layerkey & " - " & layerClass & " => " & numGraphics - numGroups & " graphics, " & numGroups & " groups"
if (numGraphics > 0) then


------------------------------------------------------------------------------------------
set gindex to 0
repeat with currentGraphic in every graphic of currentLayer

set gid to id of currentGraphic
set exportedName to "graphic_" & gid
log "SHAPE: " & exportedName
--repeat with userDataItem in every user data item in currentCanvas
--end repeat

if ((count of user data of currentGraphic) is not 0) then
set userData to user data of currentGraphic
-------------------------------------------------------------------- Get Name
try
set gname to the value of user data item "name" of currentGraphic
log " NAME: " & gname
on error errMsg number errNo
set gname to ""
if errNo is -1728 then
-- no 'name' defined in userData
else if errNo is -2753 then
-- no 'name' defined in userData
else
log "ERROR: " & errMsg & " (" & errNo & ") "
end if
end try
-------------------------------------------------------------------- Get Type
try
set gtype to the value of user data item "type" of currentGraphic
log " TYPE: " & gtype
on error errMsg number errNo
set gtype to ""
if errNo is -1728 then
-- no 'type' defined in userData
else if errNo is -2753 then
-- no 'type' defined in userData
else
log "ERROR: " & errMsg & " (" & errNo & ") "
end if
end try
-------------------------------------------------------------------- Get Export
try
set gexport to the value of user data item "export" of currentGraphic
log " EXPORT: " & gexport
on error errMsg number errNo
set gexport to ""
if errNo is -1728 then
-- no 'export' defined in userData
else if errNo is -2753 then
-- no 'export' defined in userData
else
log "ERROR: " & errMsg & " (" & errNo & ") "
end if
end try

--set user data of currentGraphic to {|keyName|:valueName} & user data of currentGraphic

----------------------------------------------------- If unnamed, allow use of a qualifying one-line label as name
if (gname is "") then
try
set graphicText to the text of currentGraphic
log "GRAPHIC TEXT: " & graphicText & return
-- TRIM WHITESPACE of full text
set graphicText2 to my trim(1, graphicText)
-- check that we're dealing with a single line
set graphicLines to paragraphs of graphicText2
log "GRAPHIC LINES: " & graphicLines & return

-- only single line entries
if (length of graphicLines is 1) then
set graphicText to item 1 of graphicLines

-- no spaces allowed
if (first character of " " is not in graphicText) then
set graphicTextName to my trim(true, graphicText)
set gname to graphicTextName
end if
end if
end try
end if

-------------------------------------------------------------------- Decide to export or not
set doExport to 0
if (gexport is "true") then
set doExport to 1
if (gname is "") then
if (gtype is not "") then
set gname to gtype & "_" & gid
else
set gname to "graphic_" & gid
end if
end if
else if (gexport is "false") then
-- leave doExport at 0
else if (gname is not "") then -- if name is defined, do export for types item & state
if (gtype is "state") then
set doExport to 1
else if (gtype is "item") then
set doExport to 1
else if (gtype is "") then -- untyped, but named is assumed to be item
set doExport to 1
end if
else -- if name is NOT defined, do export for types item & state
if (gtype is "state") then
set doExport to 1
set gname to "state_" & gid
else if (gtype is "item") then
set doExport to 1
set gname to "item_" & gid
end if
end if

else
set doExport to 0
--log "HAS NO USER DATA"

set userData to {export:"false"}
end if

if (doExport is 1) then
--set pos to origin of currentGraphic
--set gx to item 1 of pos
--set gy to item 2 of pos
--set gsize to size of currentGraphic
--set gw to item 1 of gsize
--set gh to item 2 of gsize
--set box to gx & "_" & gy & "_" & gw & "_" & gh
set exportName to layerkey & "|" & gname & "|" & gindex
set user data of currentGraphic to {_exported:exportName} & user data of currentGraphic

-------------------------------------------------------------- DO EXPORT
set canvas of front window to currentCanvas

set p to properties of currentGraphic
set gr to (a reference to currentGraphic)
set selectionGraphics to {get currentGraphic}
-- set end of selectionGraphics to currentGraphic
set selection of front window to selectionGraphics

set exportFileName to export_folder & exportName & "." & exportFileExtension
log "Exporting IMAGE: " & exportName & "." & exportFileExtension & " …"
set exportType to exportFileType

-- export settings
set include border of current export settings to false

-- exports each item separately
set area type of current export settings to selected graphics

-- exports each layer in full canvas size
--set area type of current export settings to current canvas

set draws background of current export settings to false
set export scale of current export settings to myScale
set resolution of current export settings to myResolution
set border amount of current export settings to 0.0

-- do the export and save
save oDoc as exportFileType in exportFileName
-------------------------------------------------------------- DONE EXPORT

set end of foundGraphics to {|canvas|:currentCanvas, |canvasName|:canvasName, |exportName|:exportName, |layer|:currentLayer, |layerName|:layerName, |graphic|:currentGraphic, |group|:"NO"}

set gindex to gindex + 1
end if
end repeat -- currentGraphic

end if -- if numGraphics > 0

end if -- if done is 0

end repeat -- currentLayer

set cindex to cindex + 1
end repeat -- currentCanvas

set canvas of front window to origCanvas

end tell
return foundGraphics

end getExportGraphics
[/CODE]

RobTrew 2011-12-07 05:17 AM

You can deal with the shared layer(s) separately by querying on the class property.

[CODE]tell application id "OGfl"
tell front document
repeat with oCanvas in canvases
repeat with oUnsharedLayer in (layers of oCanvas where class is not shared layer) as list
-- harvest from unshared layers of each Canvas
end repeat
end repeat

tell front canvas
repeat with oSharedLayer in (layers where class is shared layer) as list
-- harvest each shared layer from just one of the canvases which parent it

end repeat
end tell
end tell
end tell

[/CODE]

supertwang 2011-12-07 10:49 AM

Looking for the iterate/select/export bit
 
Thanks Rob for your response.
The problem I'm having is not in finding and iterating over the shared layers, but rather in iterating over their contained shapes, and, in particular in actually selecting them, so that they can be individually exported as images.

The following code is what is NOT working, but might illustrate my intent better that the previous example (I've stripped it down to show just the relevant part). In your provided code, this would be inserted just under the comment line beginning with 'harvest each shared layer'...

The error I'm getting is:
"Can't set window 1 of canvas 1 of document 1 to { item 1 of every graphic of item 1 of {layer "title" of canvas id 7 of document "test.graffle", layer "bg" of canvas id 7 of document "test.graffle"} }

Any ideas??? Thanks again.
ST
[CODE]
repeat with currentGraphic in every graphic of oSharedLayer
set gclass to class of currentGraphic
log "Current SHARED graphic: " & gclass
set selectionGraphics to {}
set end of selectionGraphics to currentGraphic
set selection of front window to selectionGraphics

set exportFileName to export_folder & exportName & "." & exportFileExtension
log "Exporting IMAGE: " & exportName & "." & exportFileExtension & " …"
set exportType to exportFileType

-- export settings
set include border of current export settings to false

-- exports each item separately
set area type of current export settings to selected graphics

set draws background of current export settings to false
set export scale of current export settings to myScale
set resolution of current export settings to myResolution
set border amount of current export settings to 0.0

-- do the export and save
save oDoc as exportFileType in exportFileName
end repeat
[/CODE]

RobTrew 2011-12-07 08:33 PM

Looks to me as if you may indeed have hit a bug in the implementation of OmniGraffle's Applescript library.

I would report it through OG's main menu:
[B]Help > Send Feedback ...[/B] including a link to the url of this thread.

The second half of the following code fails (Applescript can get references to individual graphics on unshared layers but not on shared layers).

[CODE]tell application id "OGfl"
set oWin to front window
tell front document
repeat with oCanvas in canvases
repeat with oUnsharedLayer in (layers of oCanvas where class is not shared layer) as list
-- harvest from unshared layers of each Canvas
set lstID to id of graphics of oUnsharedLayer
repeat with lngID in lstID
set lngID to contents of lngID
graphics of oUnsharedLayer

-- WORKS
set oGraphic to graphic id lngID of oUnsharedLayer
set selection of oWin to {oGraphic}
selection of oWin
{}
end repeat
end repeat
end repeat

set oCanvas to front canvas
tell oCanvas
repeat with oSharedLayer in (layers where class is shared layer) as list
-- harvest each shared layer from just one of the canvases which parent it
set lstID to id of graphics of oSharedLayer
repeat with lngID in lstID
set lngID to contents of lngID
graphics of oSharedLayer

-- BOTH FAIL -- BUG IN OG APPLESCRIPT ?
set oGraphic to graphic id lngID of oSharedLayer
set oGraphic to graphic id lngID of oCanvas

set selection of oWin to {oGraphic}
selection of oWin
{}
end repeat
end repeat
end tell
end tell
end tell
[/CODE]

There may be a clue in these two screen dumps from Script Debugger. The Applescript library is not offering a properly resolved reference to the canvas from shared layers ([I]<<class >>[/I] rather than [I]canvas[/I])

[IMG]http://farm8.staticflickr.com/7024/6475347781_14e3a7587c_o.jpg[/IMG]

In contrast, this works properly from ordinary layers:
[IMG]http://farm8.staticflickr.com/7150/6475347769_61de38ab10_o.jpg[/IMG]


All times are GMT -8. The time now is 04:39 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.