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