Wednesday, June 28, 2017

#1 2017-04-12 08:30:35 am

CrzyMke
Member
Registered: 2006-06-29
Posts: 3

Script to Batch Export Numbers Files

I'm new to AppleScripting and trying to cobble together some functional code from a few different sources, and would love some help troubleshooting.

I have a big directory structure of *.numbers files that I'd like to archive for some colleagues that may not have access to macs and/or Numbers at some future date. What I'd like to do is automate a process that will crawl through that folder structure, and any time it finds a numbers file, export it to both PDF and XLS, and leave it in the same folder as the original file.

The approach I was planning to take was to use Hazel to crawl through the directory and find the proper files, then run an AppleScript to do the exporting. I cobbled together the script below, but am running into errors running the script. Right now I'm getting a class error when I try to set XLStargetFileHFSPath.

The code also includes a line at the top explicitly listing a sample file so I could debug the code before running it through Hazel - I was planning to comment that out when everything was in working order.

Appreciate any help.

Applescript:

property theFile : alias POSIX file "/Users/MacBookAir/test.numbers"
hazelProcessFile(theFile)

on hazelProcessFile(theFile)
   
   tell application "Finder"
       set documentName to the name of theFile
   end tell
   
   if documentName ends with ".numbers" then set documentName to text 1 thru -9 of documentName
   
   tell application "Finder"
       set XLSExportFileName to documentName & ".xls"
       set PDFExportFileName to documentName & ".pdf"
       set folderPath to container of (theFile as alias)
   end tell
   
   set the XLStargetFileHFSPath to folderPath & XLSExportFileName
   
   set the PDFtargetFileHFSPath to folderPath & PDFExportFileName
   
   tell application "Numbers"
       activate
       export theFile to file XLSExportFileName as Microsoft Excel
       export theFile to file PDFExportFileName as PDF
   end tell
   
end hazelProcessFile


Filed under: Export, numbers, Hazel

Offline

 

#2 2017-04-12 12:19:02 pm

t.spoon
Member
From: BFE, Massachusetts
Registered: 2013-01-12
Posts: 185

Re: Script to Batch Export Numbers Files

There are a few problems.

An Applescript Alias is a reference to the file. You can't append text to a reference.

Instead of:

Applescript:

set folderPath to container of (theFile as alias)

If you want to manipulate a path as text, you want:

Applescript:

set folderPath to (container of theFile) as text

Applescript also checks for the existence of a file when you set an alias, so they are no good for use to build paths for files that don't exist yet.

Then you're feeding Numbers the file alias for the export command, but the export command in Numbers wants a Numbers document reference as an argument, not a file reference.

For instance:

Applescript:

tell application "Numbers"
   set docReference to the front document
end tell

now "docReference" is a reference to a Numbers document.

So you want to open the document in Numbers with the alias you get, then tell it to perform the exports.

Also, you build the save paths as "XLStargetFileHFSPath" & "PDFtargetFileHFSPath" but then you don't use those, you're trying to perform the save with "XLSExportFileName" & "PDFExportFileName."

I don't have numbers to test, but I think this should work, or at least get you closer:

Applescript:

property theFile : alias POSIX file "/Volumes/Hackintosh HD/Users/work/Downloads/Test.numbers"

hazelProcessFile(theFile)

on hazelProcessFile(theFile)
   
   tell application "Finder"
       set documentName to the name of theFile
       
       if documentName ends with ".numbers" then set documentName to text 1 thru -9 of documentName
       
       set XLSExportFileName to documentName & ".xls"
       set PDFExportFileName to documentName & ".pdf"
       set folderPath to (container of theFile) as text
       
       set the XLStargetFileHFSPath to folderPath & XLSExportFileName
       set the PDFtargetFileHFSPath to folderPath & PDFExportFileName
   end tell
   
   
   tell application "Numbers"
       activate
       set docRef to open theFile
       export docRef to file XLStargetFileHFSPath as Microsoft Excel
       export docRef to file PDFtargetFileHFSPath as PDF
   end tell
   
end hazelProcessFile


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#3 2017-04-12 01:47:38 pm

CrzyMke
Member
Registered: 2006-06-29
Posts: 3

Re: Script to Batch Export Numbers Files

That help was much appreciated, t.spoon. That has definitely got me much farther along.

Now when I run your version of the script, I get an error reading: error "Numbers got an error: The document “test” could not be exported as “test”. You don’t have permission." number 6

As best as I can tell, I have read/write permissions on the folder I'm working in. Is there something silly I'm overlooking?

Offline

 

#4 2017-04-12 01:53:35 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: Script to Batch Export Numbers Files

Edit as :

Applescript:

tell application "Numbers"
   activate
   set docRef to open theFile
   set XLStargetFileHFSPath to XLStargetFileHFSPath as «class furl»
   close access (open for access XLStargetFileHFSPath) # Required for 10.12 thru 10.12.3. Don't hurt with 10.12.4 & 5
   export docRef to XLStargetFileHFSPath as Microsoft Excel
   set PDFtargetFileHFSPath to PDFtargetFileHFSPath as «class furl»
   close access (open for access PDFtargetFileHFSPath) # Required for 10.12 thru 10.12.3. Don't hurt with 10.12.4 & 5
   export docRef to file PDFtargetFileHFSPath as PDF
end tell

Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) mercredi 12 avril 2017 20:53:31

Offline

 

#5 2017-04-12 02:07:08 pm

CrzyMke
Member
Registered: 2006-06-29
Posts: 3

Re: Script to Batch Export Numbers Files

Beautiful. Thank you so much, Yvan and t.spoon. It works like a charm.

I'm pasting in the final complete script as a reference if its useful for anyone. (I also threw in a close command so I don't end up with tons of open files while this is running.)


Applescript:

property theFile : alias POSIX file "/Users/MacBookAir/test.numbers"

hazelProcessFile(theFile)

on hazelProcessFile(theFile)
   
   tell application "Finder"
       set documentName to the name of theFile
       
       if documentName ends with ".numbers" then set documentName to text 1 thru -9 of documentName
       
       set XLSExportFileName to documentName & ".xls"
       set PDFExportFileName to documentName & ".pdf"
       set folderPath to (container of theFile) as text
       
       set the XLStargetFileHFSPath to folderPath & XLSExportFileName
       set the PDFtargetFileHFSPath to folderPath & PDFExportFileName
   end tell
   
   
   tell application "Numbers"
       activate
       set docRef to open theFile
       set XLStargetFileHFSPath to XLStargetFileHFSPath as «class furl»
       close access (open for access XLStargetFileHFSPath) # Required for 10.12 thru 10.12.3. Don't hurt with 10.12.4 & 5
       export docRef to XLStargetFileHFSPath as Microsoft Excel
       set PDFtargetFileHFSPath to PDFtargetFileHFSPath as «class furl»
       close access (open for access PDFtargetFileHFSPath) # Required for 10.12 thru 10.12.3. Don't hurt with 10.12.4 & 5
       export docRef to file PDFtargetFileHFSPath as PDF
       close docRef without saving
   end tell
   
end hazelProcessFile

Offline

 

#6 2017-04-12 02:14:50 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3059

Re: Script to Batch Export Numbers Files

Thanks for the feedback.
You were facing a problem which I'm well aware of. I reported it to Apple when 10.12.4 was in beta test process.


Yvan KOENIG running Sierra 10.12.4 in French (VALLAURIS, France) mercredi 12 avril 2017 21:14:55

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.049 seconds, 11 queries executed ]

RSS (new topics) RSS (active topics)