Convert Numbers File to PDF

Hi,

i found a file to save numbers2pdf but whenever i try to run it it gives me an error message saying "Numbers got an error: cant get document 1, invalid index.

i have attached the code,i am using iwork09 on lion 10.8.

can you please help, i need it for work purposes to convert 500 files to pdf.

Thanks in advance

–{code}
–[SCRIPT save2Numbers&PDF.app]
(*
Enregistrer le script en tant que Script : save2Numbers&PDF.scpt
déplacer le fichier créé dans le dossier
<VolumeDeDémarrage>:Users::Library:Scripts:Applications:Numbers:
Il vous faudra peut-être créer le dossier Numbers et peut-être même le dossier Applications.

aller au menu Scripts , choisir Numbers puis choisir save2Numbers&PDF

Le script enregistre le document au format natif de Numbers
et l’enregistre dans un fichier PDF.

S’il existe déjà un PDF homonyme, il est renommé en lui ajoutant une chaîne
construite sur sa date de modification.

–=====

L’aide du Finder explique:
L’Utilitaire AppleScript permet d’activer le Menu des scripts :
Ouvrez l’Utilitaire AppleScript situé dans le dossier Applications/AppleScript.
Cochez la case “Afficher le menu des scripts dans la barre de menus”.

+++++++++

Save the script
as a Script: save2Numbers&PDF.scpt

Move the newly created file into the folder:
:Users::Library:Scripts:Applications:Numbers:
Maybe you would have to create the folder Numbers and even the folder Applications by yourself.

go to the Scripts Menu, choose Numbers, then choose save2Numbers&PDF

The script saves the document in the native Numbers format and saves it in a .pdf file

If such a .pdf already exists, it is renamed with a stamp matching its modification date.

–=====

The Finder’s Help explains:
To make the Script menu appear:
Open the AppleScript utility located in Applications/AppleScript.
Select the “Show Script Menu in menu bar” checkbox

–=====

Yvan KOENIG (VALLAURIS, France)
modifié 2010/08/17
)
property closeIt : true
(
true = closes the saved document
false = doesn’t close it *)

property theApp : “Numbers”
property theExt : “numbers”

–=====

on run
tell application theApp
activate
set le_document to front document
if modified of le_document then save le_document
delay 0.2
set thePath to path of document 1
end tell – theapp
set {xPath, xExt} to my saveAs(thePath)
if closeIt then tell application theApp to close document 1 saving no
end run

–=====

on saveAs(p)
local extension_export, type_export, nom_de_l_original, dossier_de_l_original, nom_de_l_export, chemin_de_l_export
set {extension_export, type_export} to {“pdf”, “LSDocumentTypePDF”}
set {nom_de_l_original, dossier_de_l_original} to my quelNomEtDossier(p)
if nom_de_l_original ends with theExt then
(*
¢ replace the original extension by the xExt one )
set nom_de_l_export to (text 1 thru -(1 + (length of theExt)) of nom_de_l_original) & extension_export
else
(

¢ add the xExt extension name )
set nom_de_l_export to nom_de_l_original & “.” & extension_export
end if
set chemin_de_l_export to dossier_de_l_original & nom_de_l_export
(

¢ CAUTION, When saving, Numbers doesn’t take care of an existing document.
It replaces it by the new one. )
tell application “System Events”
if exists (file chemin_de_l_export) then
(

Playing safety, we rename the existing file by inserting a modification_date_time stamp )
set name of file chemin_de_l_export to (text 1 thru -(2 + (length of extension_export)) of nom_de_l_export) & my horoDateur(modification date of file chemin_de_l_export) & “.” & extension_export
end if
end tell – System Events
(

¢ save as type_export document *)
tell application “Numbers” to save document nom_de_l_original as type_export in chemin_de_l_export
return {chemin_de_l_export, extension_export}
end saveAs

– =====

on quelNomEtDossier(f)
local nom, dossier
tell application “System Events” to tell file (f as Unicode text)
set nom to name (* Unicode text )
set dossier to path of container (
Unicode HFS path *)
end tell – to System Events
return {nom, dossier}
end quelNomEtDossier

–=====
(*
¢ Build a stamp from the modification date_time
)
on horoDateur(date_de_modification)
local les_secondes
set les_secondes to time of date_de_modification
return (“" & year of date_de_modification & text -2 thru -1 of (“0” & (month of date_de_modification as integer)) & text -2 thru -1 of (“0” & day of date_de_modification) & "” & text -2 thru -1 of (“0” & les_secondes div 3600) & text -2 thru -1 of (“0” & (les_secondes mod 3600) div 60) & text -2 thru -1 of (“0” & les_secondes mod 60))
(
Here, the stamp is “_YYYYMMDD_hhmmss” *)
end horoDateur

–=====
–[/SCRIPT]
–{code}

If anyone does have a script to convert batch of NUMBERS files to PDF, and save them in the same folder, i would really appreciate if you could share that with me.

Thanks

The asker sent me a sample file and the script exported it as PDF flawlessly.

Here is the log report issued by Script Editor :


tell application "Numbers"
	activate
	get document 1
		--> document "INVOICE - APRIL 13 - INK4U.numbers"
	get modified of document "INVOICE - APRIL 13 - INK4U.numbers"
		--> false
	get path of document 1
		--> "/Users/yvankoenig/Desktop/INVOICE - APRIL 13 - INK4U.numbers"
end tell
tell application "System Events"
	get name of file "/Users/yvankoenig/Desktop/INVOICE - APRIL 13 - INK4U.numbers"
		--> "INVOICE - APRIL 13 - INK4U.numbers"
	get path of container of file "/Users/yvankoenig/Desktop/INVOICE - APRIL 13 - INK4U.numbers"
		--> "Macintosh HD:Users:yvankoenig:Desktop:"
	exists file "Macintosh HD:Users:yvankoenig:Desktop:INVOICE - APRIL 13 - INK4U.pdf"
		--> false
end tell
tell application "Numbers"
	save document "INVOICE - APRIL 13 - INK4U.numbers" as "LSDocumentTypePDF" in "Macintosh HD:Users:yvankoenig:Desktop:INVOICE - APRIL 13 - INK4U.pdf"
	close document 1 saving no
end tell

I have no idea of what is wrong on the asker’s machine ” or in front of it.
I can’t guess that the asker tried to run the script without opening a Numbers document to apply on it :wink:

Yvan KOENIG (VALLAURIS, France) vendredi 19 avril 2013 18:46:57

I’m puzzled.
The asker’s Finder is set to display the name extension but on his machine, the instruction

set le_document to front document

returns the name without the name extension.

Here it returns the full name.

A simple workaround is to no longer use the name returned by System Events in the save handler.

Replace :


tell application "Numbers" to save document nom_de_l_original as type_export in chemin_de_l_export

by


tell application "Numbers" to save document 1 as type_export in chemin_de_l_export

I would be glad to learn why the name is shortened.

Yvan KOENIG (VALLAURIS, France) vendredi 19 avril 2013 19:42:40