Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Derive the POSIX path to the folder in which to export
- -- Standard 'path to' enumerations include:
- -- documents folder/downloads folder/home folder/movies folder/music folder
- -- pictures folder/public folder/temporary items/shared documents
- set targetDirectory to (path to documents folder)
- set targetDirectoryPOSIXPath to POSIX path of targetDirectory
- if targetDirectoryPOSIXPath does not end with "/" then ¬
- set targetDirectoryPOSIXPath to targetDirectoryPOSIXPath & "/"
- tell application id "com.apple.iWork.Numbers"
- try
- if not (exists document 1) then error number 1000
- try -- check to see if document has been saved
- set thisDocumentHFSPath to (the file of document 1) as string
- set thisDocumentPath to POSIX path of thisDocumentHFSPath
- length of thisDocumentPath
- on error errorMessage number errorNumber
- error number 1001
- end try
- set thisDocumentName to the name of document 1
- copy my deriveNewFilename(thisDocumentPath, "pdf", "-", targetDirectoryPOSIXPath) ¬
- to {targetName, targetPOSIXpath}
- my UIExportToPDF(targetPOSIXpath)
- on error errorMessage number errorNumber
- if errorNumber is 1000 then
- set alertString to "MISSING RESOURCE"
- set errorMessage to ¬
- "Please create or open a document before running this script."
- else if errorNumber is 1001 then
- set alertString to "STATUS ERROR"
- set errorMessage to ¬
- "Please save this document before running this script."
- else
- set alertString to "EXPORT ERROR"
- end if
- display alert alertString message errorMessage buttons {"Cancel"}
- error number -128
- end try
- end tell
- on UIExportToPDF(targetPOSIXpath)
- -- Since this sub-routine uses the Accessibility frameworks to control Numbers, it requires one-time approval by an administrative user.
- -- For more info: <http://macosxautomation.com/mavericks/guiscripting/reset.html>
- tell application "Numbers" to activate
- tell application "System Events"
- tell process "Numbers"
- 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
- click pop up button 1 of sheet 1 of window 1
- click menu item "Best" of menu 1 of pop up button 1 of sheet 1 of window 1
- click button "Next…" of sheet 1 of window 1
- delay 1
- keystroke targetPOSIXpath
- delay 1
- keystroke return
- delay 1
- keystroke return
- end tell
- end tell
- end UIExportToPDF
- on deriveNewFilename(sourceItemPOSIXPath, newNameExtension, incrementSeparator, targetFolderPOSIXPath)
- -- A sub-routine used for deriving the name and path of a new file using the name of an existing file
- -- Pass in file ref in POSIX format, the new name extension, an increment separator, and any target directory (in POSIX format)
- -- Name and POSIX path for new file are returned. The name is incremented if a file exists in the target location.
- -- Pass a null string ("") for the target directory, to use the item's parent directory as the destination
- -- Pass a null string ("") for the new name extension, to use the item's current name extension
- if targetFolderPOSIXPath is "" then
- -- get the path to parent folder of the source item
- set targetFolderPOSIXPath to ¬
- (do shell script "dirname " & (the quoted form of sourceItemPOSIXPath))
- end if
- if targetFolderPOSIXPath does not end with "/" then ¬
- set targetFolderPOSIXPath to targetFolderPOSIXPath & "/"
- -- get the name of the source file
- set the sourceItemName to ¬
- (do shell script "basename " & (quoted form of the sourceItemPOSIXPath))
- -- get the name extension (if any) of the source item
- set the fileInfoRecord to (info for sourceItemPOSIXPath)
- set the sourceItemNameExtension to (the name extension of fileInfoRecord)
- if the sourceItemNameExtension is missing value then
- set the sourceItemNameExtension to ""
- if newNameExtension is "" then
- set extensionSeparator to ""
- else
- set extensionSeparator to "."
- end if
- else
- set extensionSeparator to "."
- end if
- -- get the base name of the source item
- if the sourceItemNameExtension is "" then
- set the sourceItemBaseName to sourceItemName
- else
- set the sourceItemBaseName to ¬
- text 1 thru -((length of sourceItemNameExtension) + 2) ¬
- of sourceItemName
- end if
- -- generate the target file name
- if the newNameExtension is "" then
- set targetName to sourceItemName
- set targetExtension to sourceItemNameExtension
- else
- set targetExtension to newNameExtension
- set targetName to ¬
- (the sourceItemBaseName & extensionSeparator & ¬
- targetExtension) as Unicode text
- end if
- -- check to see if a file named the same as the source file exists in the target folder
- set targetItemPOSIXPath to targetFolderPOSIXPath & targetName
- set the fileExistenceStatus to ¬
- (do shell script "[ -a " & ¬
- (quoted form of targetItemPOSIXPath) & ¬
- " ] && echo 'true' || echo 'false'") as boolean
- if fileExistenceStatus is true then
- set the nameIncrement to 1
- repeat
- -- create a new target path with the target item name incremented
- set the newName to ¬
- (the sourceItemBaseName & incrementSeparator & ¬
- (nameIncrement as Unicode text) & ¬
- extensionSeparator & targetExtension) as Unicode text
- set targetItemPOSIXPath to targetFolderPOSIXPath & newName
- set the fileExistenceStatus to ¬
- (do shell script "[ -a " & (quoted form of targetItemPOSIXPath) & ¬
- " ] && echo 'true' || echo 'false'") as boolean
- if fileExistenceStatus is true then
- set the nameIncrement to the nameIncrement + 1
- else
- set the targetPOSIXpath to (targetFolderPOSIXPath & newName)
- return {newName, targetPOSIXpath}
- end if
- end repeat
- else
- set the targetPOSIXpath to (targetFolderPOSIXPath & targetName)
- return {targetName, targetPOSIXpath}
- end if
- end deriveNewFilename
Advertisement
Add Comment
Please, Sign In to add comment