Tuesday, February 20, 2007

AppleScript: Convert OmniGraffle documents to PDF

The following AppleScript converts all selected OmniGraffle drawings (*.graffle) to PDF. It is based on Greg Titus's script published here. While Greg's script watches a folder, this script simply converts all selected files. Note: There's no error handling and you may want to change the export settings. If you have CocoThumbX installed, you may want to uncomment line where sub routine "createThumb" is called and change the path to CocoThumbX.
-- converts selected OmniGraffle files to PDF -- based on Greg Titus's script found at -- http://forums.omnigroup.com/showthread.php?t=106&highlight=export+applescript -- -- by jevopi, 2007 tell application "Finder" set these_items to the selection end tell repeat with i from 1 to the count of these_items set this_item to (item i of these_items) as alias set this_info to info for this_item -- insert actions here for: this_item set item_path to POSIX path of this_item set item_folder to (parent of (item i of these_items)) as string set item_name to name of (item i of these_items) set item_ext to name extension of (item i of these_items) set exp_name to my rename(item_name, item_ext, "pdf") set exp_path to item_folder & exp_name set msg to "Path: " & item_path & ", Exp: " & exp_path --display dialog msg buttons {"OK"} default button 1 my omniConvert(this_item, item_path, exp_path) -- Uncomment this line if you have CocoThumb installed -- my createThumb(exp_path) end repeat -- create icon with CocoThumbX on createThumb(exp_path) set theApplication to Drive:Applications:Tools:CocoThumbX" set theFile to alias exp_path tell application "Finder" open theFile using theApplication end tell end createThumb -- this sub-routine just comes up with the new name on rename(item_name, item_ext, new_extension) tell application "Finder" if the item_ext is "" then set the trimmed_name to the file_name else set the trimmed_name to text 1 thru -((length of item_ext) + 2) of the item_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 omniConvert(source_file, source_path, target_path) with timeout of 900 seconds tell application "OmniGraffle" -- 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 oldBackground to draws background of 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 false set draws background of current export settings to false -- open the file if it isn't already open set needToOpen to (count (documents whose path is source_path)) is 0 if needToOpen then open source_file end if -- do the export set docsWithPath to documents whose path is source_path 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 draws background of current export settings to oldBackground end tell end timeout end omniConvert

No comments: