mingsai

Numbers Export Front Document as PDF

Feb 22nd, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Derive the POSIX path to the folder in which to export
  2. -- Standard 'path to' enumerations include:
  3. -- documents folder/‌downloads folder/home folder/movies folder/music folder
  4. -- ‌pictures folder/public folder/temporary items/shared documents
  5. set targetDirectory to (path to documents folder)
  6. set targetDirectoryPOSIXPath to POSIX path of targetDirectory
  7. if targetDirectoryPOSIXPath does not end with "/" then ¬
  8.     set targetDirectoryPOSIXPath to targetDirectoryPOSIXPath & "/"
  9.  
  10. tell application id "com.apple.iWork.Numbers"
  11.     try
  12.         if not (exists document 1) then error number 1000
  13.        
  14.         try -- check to see if document has been saved
  15.             set thisDocumentHFSPath to (the file of document 1) as string
  16.             set thisDocumentPath to POSIX path of thisDocumentHFSPath
  17.             length of thisDocumentPath
  18.         on error errorMessage number errorNumber
  19.             error number 1001
  20.         end try
  21.        
  22.         set thisDocumentName to the name of document 1
  23.        
  24.         copy my deriveNewFilename(thisDocumentPath, "pdf", "-", targetDirectoryPOSIXPath) ¬
  25.             to {targetName, targetPOSIXpath}
  26.        
  27.         my UIExportToPDF(targetPOSIXpath)
  28.        
  29.     on error errorMessage number errorNumber
  30.         if errorNumber is 1000 then
  31.             set alertString to "MISSING RESOURCE"
  32.             set errorMessage to ¬
  33.                 "Please create or open a document before running this script."
  34.         else if errorNumber is 1001 then
  35.             set alertString to "STATUS ERROR"
  36.             set errorMessage to ¬
  37.                 "Please save this document before running this script."
  38.         else
  39.             set alertString to "EXPORT ERROR"
  40.         end if
  41.         display alert alertString message errorMessage buttons {"Cancel"}
  42.         error number -128
  43.     end try
  44. end tell
  45.  
  46. on UIExportToPDF(targetPOSIXpath)
  47.     -- Since this sub-routine uses the Accessibility frameworks to control Numbers, it requires one-time approval by an administrative user.
  48.     -- For more info: <http://macosxautomation.com/mavericks/guiscripting/reset.html>
  49.     tell application "Numbers" to activate
  50.     tell application "System Events"
  51.         tell process "Numbers"
  52.             click menu item "PDF…" of menu "Export To" of menu item "Export To" of menu "File" of menu bar item "File" of menu bar 1
  53.             click pop up button 1 of sheet 1 of window 1
  54.             click menu item "Best" of menu 1 of pop up button 1 of sheet 1 of window 1
  55.             click button "Next…" of sheet 1 of window 1
  56.             delay 1
  57.             keystroke targetPOSIXpath
  58.             delay 1
  59.             keystroke return
  60.             delay 1
  61.             keystroke return
  62.         end tell
  63.     end tell
  64. end UIExportToPDF
  65.  
  66. on deriveNewFilename(sourceItemPOSIXPath, newNameExtension, incrementSeparator, targetFolderPOSIXPath)
  67.     -- A sub-routine used for deriving the name and path of a new file using the name of an existing file
  68.     -- Pass in file ref in POSIX format, the new name extension, an increment separator, and any target directory (in POSIX format)
  69.     -- Name and POSIX path for new file are returned. The name is incremented if a file exists in the target location.
  70.     -- Pass a null string ("") for the target directory, to use the item's parent directory as the destination
  71.     -- Pass a null string ("") for the new name extension, to use the item's current name extension
  72.    
  73.     if targetFolderPOSIXPath is "" then
  74.         -- get the path to parent folder of the source item
  75.         set targetFolderPOSIXPath to ¬
  76.             (do shell script "dirname " & (the quoted form of sourceItemPOSIXPath))
  77.     end if
  78.     if targetFolderPOSIXPath does not end with "/" then ¬
  79.         set targetFolderPOSIXPath to targetFolderPOSIXPath & "/"
  80.     -- get the name of the source file
  81.     set the sourceItemName to ¬
  82.         (do shell script "basename " & (quoted form of the sourceItemPOSIXPath))
  83.     -- get the name extension (if any) of the source item
  84.     set the fileInfoRecord to (info for sourceItemPOSIXPath)
  85.     set the sourceItemNameExtension to (the name extension of fileInfoRecord)
  86.     if the sourceItemNameExtension is missing value then
  87.         set the sourceItemNameExtension to ""
  88.         if newNameExtension is "" then
  89.             set extensionSeparator to ""
  90.         else
  91.             set extensionSeparator to "."
  92.         end if
  93.     else
  94.         set extensionSeparator to "."
  95.     end if
  96.     -- get the base name of the source item
  97.     if the sourceItemNameExtension is "" then
  98.         set the sourceItemBaseName to sourceItemName
  99.     else
  100.         set the sourceItemBaseName to ¬
  101.             text 1 thru -((length of sourceItemNameExtension) + 2) ¬
  102.                 of sourceItemName
  103.     end if
  104.     -- generate the target file name
  105.     if the newNameExtension is "" then
  106.         set targetName to sourceItemName
  107.         set targetExtension to sourceItemNameExtension
  108.     else
  109.         set targetExtension to newNameExtension
  110.         set targetName to ¬
  111.             (the sourceItemBaseName & extensionSeparator & ¬
  112.                 targetExtension) as Unicode text
  113.     end if
  114.     -- check to see if a file named the same as the source file exists in the target folder
  115.     set targetItemPOSIXPath to targetFolderPOSIXPath & targetName
  116.     set the fileExistenceStatus to ¬
  117.         (do shell script "[ -a " & ¬
  118.             (quoted form of targetItemPOSIXPath) & ¬
  119.             " ] && echo 'true' || echo 'false'") as boolean
  120.     if fileExistenceStatus is true then
  121.         set the nameIncrement to 1
  122.         repeat
  123.             -- create a new target path with the target item name incremented
  124.             set the newName to ¬
  125.                 (the sourceItemBaseName & incrementSeparator & ¬
  126.                     (nameIncrement as Unicode text) & ¬
  127.                     extensionSeparator & targetExtension) as Unicode text
  128.             set targetItemPOSIXPath to targetFolderPOSIXPath & newName
  129.             set the fileExistenceStatus to ¬
  130.                 (do shell script "[ -a " & (quoted form of targetItemPOSIXPath) & ¬
  131.                     " ] && echo 'true' || echo 'false'") as boolean
  132.             if fileExistenceStatus is true then
  133.                 set the nameIncrement to the nameIncrement + 1
  134.             else
  135.                 set the targetPOSIXpath to (targetFolderPOSIXPath & newName)
  136.                 return {newName, targetPOSIXpath}
  137.             end if
  138.         end repeat
  139.     else
  140.         set the targetPOSIXpath to (targetFolderPOSIXPath & targetName)
  141.         return {targetName, targetPOSIXpath}
  142.     end if
  143. end deriveNewFilename
Advertisement
Add Comment
Please, Sign In to add comment