View Single Post
Below is a script which reads omnigraffle docs from a directory then exports them as png thumbnail files.

Two questions:

1.) Currently I have to go into OmniGraffle and set the scale (e.g., 10%) to create my thumbnails. How do you do it programatically?
2.) I trap an error and display a message when opening a document. How do I close it? My logic doesn't work. Documents which are converted OK close automatically?


property new_extension : "png"
-- the folder to watch
property watch_folder : "Macintosh HD:Users:jjkirby:Desktop:images"
-- the subfolder inside the watch folder where the exported documents should go
property export_foldername : "thumbs"
-- amount of time to wait in between checking for file changes
property seconds_between_checks : 10

tell application "Finder"
-- set up the watch folder and create the export folder inside
tell application "Finder"
set watch_folder to watch_folder as alias
if not (exists folder export_foldername of watch_folder) then
make new folder at watch_folder with properties {name:export_foldername}
end if
set the export_folder to (folder export_foldername of watch_folder) as alias
end tell

-- repeat forever
-- get all the graffle files in the watch folder
set fileList to files of watch_folder whose kind is "OmniGraffle document"
repeat with aFile in fileList
-- figure out what the new exported name would be
set new_name to my rename(aFile, new_extension)
set the target_path to (((export_folder as string) & new_name) as string)

-- if there isn't an export file or if the graffle file is newer, do the export
if (not (exists document file new_name of export_folder)) or modification date of aFile > modification date of document file new_name of export_folder then
my process_item(aFile as alias, new_name, export_folder)
end if
end repeat

-- wait before we look again
delay seconds_between_checks
end repeat
end tell

-- this sub-routine just comes up with the new name
on rename(this_item, new_extension)
tell application "Finder"
set the file_name to the name of this_item
set file_extension to the name extension of this_item
if the file_extension is "" then
set the trimmed_name to the file_name
set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
end if
set target_name to (the trimmed_name & "." & new_extension) as string
end tell
return the target_name
end rename

-- this sub-routine does the export
on process_item(source_file, new_name, results_folder)

set the source_item to the POSIX path of the source_file
set the target_path to (((results_folder as string) & new_name) as string)
with timeout of 900 seconds
tell application "OmniGraffle Professional 5.0"
-- save the current export settings so we can replace them later
set oldAreaType to area type of current export settings
set oldBorder to include border of current export settings
set oldScale to export scale of current export settings
--set scale of current export settings to 50 as integer
--set image scale to 10 as integer for current export settings

-- here is where you set the export settings you want
set area type of current export settings to all graphics
set include border of current export settings to true

-- open the file if it isn't already open
set needToOpen to (count (documents whose path is source_item)) is 0
if needToOpen then
open source_file
end if

-- do the export

set docsWithPath to documents whose path is source_item

set theDoc to first item of docsWithPath

save theDoc in file target_path

-- if the file wasn't already open, close it again
if needToOpen then
close theDoc
end if

-- put the original export settings back
set area type of current export settings to oldAreaType
set include border of current export settings to oldBorder
-- set scale of current export settings to oldScale
on error ErrMsg number errorNumber
close source_file saving no
display dialog "Unable To Convert To Thumb. " & source_item as text
end try

end tell
end timeout
end process_item