The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniGraffle General (http://forums.omnigroup.com/forumdisplay.php?f=10)
-   -   Applescript: saving canvases (http://forums.omnigroup.com/showthread.php?t=146)

af3556 2006-04-18 06:31 AM

Applescript: saving canvases
 
Hi All,

I'm trying to extend [URL="http://forums.omnigroup.com/showthread.php?t=106"]the 'autoexport' applescript[/URL], and wish to handle multiple-page/canvas drawings by saving each canvas as a separate image. I don't want to use the 'entire document' export area option as a) it exports full-page rt. just the objects, and b) it creates separate files in a new subdirectory (I wish to export file.graffle -> file.1.png, file.2.png, etc).

Sooo, I've tried:
[CODE]
tell application "OmniGraffle Professional"
save canvas 1 of first item of documents in file ((desktop folder as string) & "ssh.png")
end tell
[/CODE]

but that doesn't work:
[CODE]
OmniGraffle Professional got an error: NSReceiversCantHandleCommandScriptError
[/CODE]

I guess you can only "save" (export) a document - so I guess I have to iterate over each canvas and save? How do I change the active canvas?


BTW, how come there's always one document returned by 'documents'? The following is with OG open, but no open docs:

[CODE]
tell application "OmniGraffle Professional"
documents
end tell
[/CODE]
-> {document "Basic.graffle" of application "OmniGraffle Professional"}

Thanks in advance,
Ben

Greg Titus 2006-04-18 04:45 PM

Being able to save an individual canvas as you first tried is really something we ought to add...

For now, you'll have to iterate over the canvases via:

[CODE]tell application "Finder"
set saveFolder to desktop as string
end tell
tell application "OmniGraffle Professional"
set theDocument to front document
set canvasCount to count of canvases of theDocument
repeat with canvasNumber from 1 to canvasCount
set canvas of front window to canvas canvasNumber of theDocument
save theDocument as "png" in (saveFolder & "ssh" & canvasNumber & ".png")
end repeat
end tell
[/CODE]

1. You have to ask for desktop from the Finder, otherwise you'll just get back the string "desktop" instead of the actual path.
2. The way you switch canvases is by setting the active canvas on the window.
3. You need to have the - as "png" - part on the save command, or else it'll think you want to save in the standard graffle format and any "."s are just part of the name.

And the reason why you always see at least one document is because the current stencil is also available as a document for scripting.

af3556 2006-04-18 07:27 PM

Thanks for the code, and the tips.

[QUOTE]3. You need to have the - as "png" - part on the save command, or else it'll think you want to save in the standard graffle format and any "."s are just part of the name.[/QUOTE]

In the 'autoexport' script you provided (and my thanks again, this has made my workflow much more efficient!), the code is:

[CODE]
save theDoc in file target_path
[/CODE]

I presume this is in fact relying on the 'current export settings' to be set to the appropriate type? But there doesn't seem to be a 'format' property to the current export settings.

With 'save as ...', is there a list of the strings associated with each format? (or just the standard png, svg, tiff, etc?)

BTW, should I have been able to find the above in any doc? (teach a man to fish :-)

Update: I've fiddled with the 'repeat with [each canvas]; set canvas of front window ...; save ...' to find that whilst this works well for unopened documents, it upsets any documents that are actually open in OG at the time of the auto-export. i.e. I'll be editing away, and all of a sudden the script kicks in and starts flipping my canvas pages... I guess the only solution is a 'save canvas' function?

Thanks,
Ben

af3556 2006-04-25 10:25 PM

canvas IDs vs indices
 
In experimenting with manipulating canvases, I've found the AS interface a little inconsistent.

I want to essentially do the following:

[CODE]
tell application "OmniGraffle Professional"
set targetCanvas to ... (canvas to be manipulated)

-- where were we?
set theDocument to document of front window
set oldCanvas to canvas of front window -- who's on first?

-- do stuff
set canvas of front window to canvas targetCanvas of theDocument
display dialog "Done doing stuff"

-- put things back to how we found them...
set canvas of front window to canvas oldCanvas of theDocument
end tell
[/CODE]

But this doesn't work, as 'set canvas ... to canvas N' expects N to be the index of the desired canvas in the canvas list, rather than the canvas object or even the canvas ID as returned by 'canvas of ...'.


Exposition:

I have the following canvases:
[CODE]
tell application "OmniGraffle Professional"
canvas of document of front window
end tell
->
{canvas id 1 of document "ssh.graffle" of application "OmniGraffle Professional",
canvas id 3 of document "ssh.graffle" of application "OmniGraffle Professional",
canvas id 2 of document "ssh.graffle" of application "OmniGraffle Professional"}
[/CODE]
Note the order: id's {1, 3, 2}

The third canvas is active (in front) (id 2).
[CODE]
tell application "OmniGraffle Professional"
id of canvas of front window
end tell
-> 2
[/CODE]
All good so far.

How to make canvas id 3 active? 'set canvas of front window to canvas' requires an index (into the list of canvases), not a canvas id.

[BTW: Is it correct there is no 'index of' operator in AS? (e.g. index of 5 in {3, 7, 8, 5, 2, 3}) If this is correct then the only option is to search (e.g. via a repeat loop) for the canvas list index corresponding to the desired canvas id.]

It would be useful to have the canvas 'set' and 'get' operations be consistent (i.e. either ID or index for both).

Rgds,
Ben

Ken Case 2006-04-26 05:10 AM

You can refer to oldCanvas directly, you don't need to look it up by index again. Like this:

[code]
tell application "OmniGraffle Professional"
-- where were we?
set theDocument to document of front window
set oldCanvas to canvas of front window -- who's on first?

-- do stuff
set targetCanvas to canvas 2 of theDocument
set canvas of front window to targetCanvas
display dialog "Done doing stuff"

-- put things back to how we found them...
set canvas of front window to oldCanvas
end tell
[/code]

af3556 2006-04-30 11:21 PM

[CODE]
set oldCanvas to canvas of front window
...
set canvas of front window to oldCanvas
[/CODE]

Makes sense, of course. (Can you tell I just don't grok applescript? :-)

Thanks.


All times are GMT -8. The time now is 06:52 PM.

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