Script for batch export png doesn't work here

Hi there,

I’m working on a book project.
I’m drawing schematics with Omnigraffle.

I’ll have two languages schematics, and I’d like to make the workflow easy so,
I have one file with all my schematics as canvas. So If I have 100 schematics for my book, I’ll have 100 canvas

One canvas = 4 layers:

  • 1 layer named FR & 1 named EN which are the text annotations translated
  • 1 layer named Image which contains the common parts
  • 1 calque named Frame which contains a frame.

I need a script which I run and which:
for each canvas, export 2 pngs:

  • 1 named upon the Canvas named + “_EN” with all layer EN + Image + Frame visible (all objects selected)
  • 1 named upon the Canvas named + “_FR” with all layer FR+ Image + Frame visible (all objects selected)

I read a lot the AppleScript Dictionnary for functions and syntax and wrote this one :

-- Define the fixed paths for the OmniGraffle file and export folders
set omnigraffleFilePath to "/Users/julien/DATA/TestScript/Schemas.graffle"
set exportFolderFR to "/Users/julien/DATA/TestScript/FR"
set exportFolderEN to "/Users/julien/DATA/TestScript/EN"

-- Open OmniGraffle and the diagram file
tell application "OmniGraffle"
	-- Open the OmniGraffle file
	open omnigraffleFilePath
	set currentDocument to front document
	
	-- Iterate through each canvas in the document
	repeat with i from 1 to count of canvases of currentDocument
		set currentCanvas to canvas i of currentDocument
		set canvasName to name of currentCanvas
		
		-- Export FR: Make FR, Images, and Frame layers visible
		tell currentCanvas
			-- Hide all layers
			repeat with j from 1 to count of layers of currentCanvas
				set visible of layer j of currentCanvas to false
			end repeat
			
			-- Show only the layers necessary for FR export
			set visible of layer "FR" of currentCanvas to true
			set visible of layer "Images" of currentCanvas to true
			set visible of layer "Frame" of currentCanvas to true
			
			-- Define the export path for FR (POSIX format)
			set exportPathFR to exportFolderFR & "/" & canvasName & "_FR.png"
			
			-- Export settings: scale, resolution, region size, border, etc.
			set exportProperties to {scale:1.0, resolution:300.0, regionorigin:{0, 0}, regionsize:{1440, 1080}, includeborder:true, borderamount:10, htmlimagetype:"png", readonly:true, drawsbackground:true, copylinkedimages:true, useartboards:false}
			
			-- Export with all the necessary parameters
			export currentCanvas to file exportPathFR as "PNG" with properties exportProperties
		end tell
		
		-- Export EN: Make EN, Images, and Frame layers visible
		tell currentCanvas
			-- Hide all layers
			repeat with j from 1 to count of layers of currentCanvas
				set visible of layer j of currentCanvas to false
			end repeat
			
			-- Show only the layers necessary for EN export
			set visible of layer "EN" of currentCanvas to true
			set visible of layer "Images" of currentCanvas to true
			set visible of layer "Frame" of currentCanvas to true
			
			-- Define the export path for EN (POSIX format)
			set exportPathEN to exportFolderEN & "/" & canvasName & "_EN.png"
			
			-- Export with the necessary parameters
			export currentCanvas to file exportPathEN as "PNG" with properties exportProperties
		end tell
	end repeat
	
	-- Close the document after export
	close currentDocument 
end tell

-- Display a notification for export completion
display dialog "Export completed in folders: " & exportFolderFR & " and " & exportFolderEN 

I run it. AppleScript throws me an exception:

Omnigraffle error: an export parameter is missing

Would someone help me here ?

If the JS way can be used, I can also give it a try :-)

Best regards,
Julien B.

Here is an updated copy of your script that should work! The following changes have been made:

  • The canvas shown in the active window is changed to match currentCanvas when looping through all your canvases.

  • When exporting currentDocument is the primary argument, and you specify what in the document you want to export with the scope property. In this case, using scope current canvas.

  • Changed file to POSIX file when specifying the destination of the export command (necessary if using a slash (/) delimited path string).

  • Changed the resolution export property. This property represents a multiplier, where 1.0 equals 72 dpi, so for 300 dpi you would use 4.166667.

  • Remove some extraneous export properties that don’t apply when your export scope is current canvas.

-- Define the fixed paths for the OmniGraffle file and export folders
set omnigraffleFilePath to "/Users/julien/DATA/TestScript/Schemas.graffle"
set exportFolderFR to "/Users/julien/DATA/TestScript/FR"
set exportFolderEN to "/Users/julien/DATA/TestScript/EN"

-- Open OmniGraffle and the diagram file
tell application "OmniGraffle"
	-- Open the OmniGraffle file
	open omnigraffleFilePath
	set currentDocument to front document
	
	-- Iterate through each canvas in the document
	repeat with i from 1 to count of canvases of currentDocument
		set currentCanvas to canvas i of currentDocument
		set canvasName to name of currentCanvas
		
		set canvas of front window to currentCanvas
		
		-- Export FR: Make FR, Images, and Frame layers visible
		tell currentCanvas
			-- Hide all layers
			repeat with j from 1 to count of layers of currentCanvas
				set visible of layer j of currentCanvas to false
			end repeat
			
			-- Show only the layers necessary for FR export
			set visible of layer "FR" of currentCanvas to true
			set visible of layer "Images" of currentCanvas to true
			set visible of layer "Frame" of currentCanvas to true
			
			-- Define the export path for FR (POSIX format)
			set exportPathFR to exportFolderFR & "/" & canvasName & "_FR.png"
			
			-- Export settings: scale, resolution, region size, border, etc.
			set exportProperties to {scale:1.0, resolution:4.1666667, drawsbackground:true, useartboards:false}
			
			-- Export with all the necessary parameters
			export currentDocument scope current canvas to POSIX file exportPathFR as "PNG" with properties exportProperties
		end tell
		
		-- Export EN: Make EN, Images, and Frame layers visible
		tell currentCanvas
			-- Hide all layers
			repeat with j from 1 to count of layers of currentCanvas
				set visible of layer j of currentCanvas to false
			end repeat
			
			-- Show only the layers necessary for EN export
			set visible of layer "EN" of currentCanvas to true
			set visible of layer "Images" of currentCanvas to true
			set visible of layer "Frame" of currentCanvas to true
			
			-- Define the export path for EN (POSIX format)
			set exportPathEN to exportFolderEN & "/" & canvasName & "_EN.png"
			
			-- Export with the necessary parameters
			export currentDocument scope current canvas to POSIX file exportPathEN as "PNG" with properties exportProperties
		end tell
	end repeat
	
	-- Close the document after export
	close currentDocument
end tell

-- Display a notification for export completion
display dialog "Export completed in folders: " & exportFolderFR & " and " & exportFolderEN
1 Like

Hi @valyria and thanks a lot.

I had two errors.

I replaced

set exportPathFR to exportFolderFR & "/" & canvasName & "_FR.png"

by

set exportPathFR to (POSIX path of exportFolderFR) & canvasName & "_FR.png"

and the other export too.

Here is the final code, for reference purpose.

-- Définir les chemins fixes du fichier OmniGraffle et des dossiers d'exportation
set omnigraffleFilePath to POSIX file "/Users/julien/DATA/TestScript/Schemas.graffle"
set exportFolderFR to POSIX file "/Users/julien/DATA/TestScript/FR/"
set exportFolderEN to POSIX file "/Users/julien/DATA/TestScript/EN/"

-- Open OmniGraffle and the diagram file
tell application "OmniGraffle"
	-- Open the OmniGraffle file
	open omnigraffleFilePath
	set currentDocument to front document
	
	-- Iterate through each canvas in the document
	repeat with i from 1 to count of canvases of currentDocument
		set currentCanvas to canvas i of currentDocument
		set canvasName to name of currentCanvas
		
		set canvas of front window to currentCanvas
		
		-- Export FR: Make FR, Images, and Frame layers visible
		tell currentCanvas
			-- Hide all layers
			repeat with j from 1 to count of layers of currentCanvas
				set visible of layer j of currentCanvas to false
			end repeat
			
			-- Show only the layers necessary for FR export
			set visible of layer "FR" of currentCanvas to true
			set visible of layer "Images" of currentCanvas to true
			set visible of layer "Frame" of currentCanvas to true
			
			-- Define the export path for FR (POSIX format)
			set exportPathFR to (POSIX path of exportFolderFR) & canvasName & "_FR.png"
			
			-- Export settings: scale, resolution, region size, border, etc.
			set exportProperties to {scale:1.0, resolution:4.1666667, drawsbackground:true, useartboards:false}
			
			-- Export with all the necessary parameters
			export currentDocument scope current canvas to POSIX file exportPathFR as "PNG" with properties exportProperties
		end tell
		
		-- Export EN: Make EN, Images, and Frame layers visible
		tell currentCanvas
			-- Hide all layers
			repeat with j from 1 to count of layers of currentCanvas
				set visible of layer j of currentCanvas to false
			end repeat
			
			-- Show only the layers necessary for EN export
			set visible of layer "EN" of currentCanvas to true
			set visible of layer "Images" of currentCanvas to true
			set visible of layer "Frame" of currentCanvas to true
			
			-- Define the export path for EN (POSIX format)
			set exportPathEN to (POSIX path of exportFolderEN) & canvasName & "_EN.png"
			
			
			-- Export with the necessary parameters
			export currentDocument scope current canvas to POSIX file exportPathEN as "PNG" with properties exportProperties
		end tell
	end repeat
	
	-- Close the document after export
	close currentDocument
end tell

-- Display a notification for export completion
display dialog "Export completed in folders: " & exportFolderFR & " and " & exportFolderEN