If I took time to write a script allowing you to copy a table from your document to another one in which it will be the unique one, itâs clearly because there is no way (at least at this time) to print a single table of a defined sheet thru AppleScript.
I really donât understand what is complicated with my script.
Itâs not guaranteed to be able to fit your needs because it assumes that the sheet embedding the table to print is at front.
If itâs not, you will have to bring it to front.
This requires a convoluted handler.
I wrote one with the help of Nigel Garvey.
I added it to the script embedded in my late message.
CAUTION :
I didnât changed the explanations.
The script is designed for a document containing at least two sheets.
I named the first sheet but here the name is unused.
The second sheet is supposed to be named âmy second sheetâ
Itâs supposedto embed a table named âTable_to_copyâ.
It may embed other tables.
I assume that when we call the script, itâs a table of the first script which is at front.
The script call the handler SelectSheet which of course is designed to select the sheet named âmy second sheetâ in the document at front. This is why the handler is called with :
my selectSheet(1, sheet2)
In a more complicated script it would be useful to replace the parameter 1 by the name of the document.
When the sheet is selected, the script select every cells of the required table.
Then, we retrieve my original code.
--{code}
--[SCRIPT print_a_table]
(*
Enregistrer le script en tant que Script ou Application : print_a_table.xxx
déplacer le fichier ainsi créé dans le dossier
<VolumeDeDémarrage>:Users:<votreCompte>:Library:Scripts:Applications:Numbers:
Il vous faudra peut-ĂÂȘtre crĂ©er le dossier Numbers et peut-ĂÂȘtre mĂÂȘme le dossier Applications.
SĂ©lectionner une partie de table, une table ou mĂÂȘme plusieurs tables.
Aller au menu Scripts , choisir Numbers puis choisir print_a_table
Le script crée un nouveau document depuis le modÚle Vide,
supprime la table créée par défaut et colle le contenu du presse-papiers.
Il envoie enfin la commande d'impression.
--=====
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".
Sous 10.6.x,
aller dans le panneau "GĂ©nĂ©ral" du dialogue PrĂ©fĂ©rences de l'Ăâ°diteur Applescript
puis cocher la case "Afficher le menu des scripts dans la barre des menus".
--=====
Save the script as a Script or an Application : print_a_table.xxx
Move the newly created file into the folder:
<startup Volume>:Users:<yourAccount>:Library:Scripts:Applications:Numbers:
Maybe you would have to create the folder Numbers and even the folder Applications by yourself.
Select a subset of a table, a table or even several tables.
Go to the Scripts Menu, choose Numbers, then choose "print_a_table"
The script create a new document based on the Blank template,
remove the default table and paste the clipboard's contents.
At last, it issue the print command.
--=====
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.
Under 10.6.x,
go to the General panel of AppleScript Editor's Preferences dialog box
and check the "Show Script menu in menu bar" option.
--=====
Yvan KOENIG (VALLAURIS, France)
2011/02/20
*)
--=====
property theApp : "Numbers"
property sheet1 : "my first sheet"
property sheet2 : "my second sheet"
property tableToCopy : "Table_to_copy"
--=====
on run
-- run script do_your_duty
my do_your_duty()
end run
--===
on do_your_duty()
--script do_your_duty
my activateGUIscripting()
my selectSheet(1, sheet2)
tell application "Numbers" to tell document 1 to tell sheet sheet2 to tell table tableToCopy
set selection range to range ("A1:" & name of last cell)
end tell
(*
Copy the selected items *)
my raccourci(theApp, "c", "c") (* cmd + c *)
(*
Create a new document based upon Blank.template *)
set myNewDoc to my makeAnIworkDoc(theApp)
(*
Delete the default table *)
tell application "Numbers" to tell document 1 to tell sheet 1 to delete table 1
my raccourci(theApp, "v", "c") (* cmd + v = Paste *)
my raccourci(theApp, "p", "c") (* cmd + p = Print *)
my raccourci(theApp, return, "") (* return = validate the Print command*)
--end script
end do_your_duty
--=====
(*
Creates a new iWork document from the Blank template and returns its name.
example:
set myNewDoc to my makeAnIworkDoc(theApp)
*)
on makeAnIworkDoc(the_app)
local maybe, path_to_the_App, nb_doc, doc_name
if the_app is "Pages" then
tell application "Pages"
set nb_doc to count of documents
make new document with properties {template name:item 1 of templates}
end tell
else if the_app is "Numbers" then
tell application "System Events" to set maybe to the_app is in title of every application process
if not maybe then tell application theApp to activate
tell application "System Events"
set path_to_the_App to get application file of application process the_app
end tell
tell application "Numbers"
set nb_doc to count of documents
open ((path_to_the_App as text) & "Contents:Resources:Templates:Blank.nmbtemplate:")
end tell
else
if my parleAnglais(theApp) then
error "The application "" & the_app & "" is not accepted !"
else
error "l'application « " & the_app & " » n'est pas gĂ©rĂ©e !"
end if
end if
tell application the_app
repeat until (count of documents) > nb_doc
delay 0.1
end repeat
set doc_name to name of document 1
end tell -- the_App
return doc_name
end makeAnIworkDoc
--=====
on parleAnglais()
local z
try
tell application "Numbers" to set z to localized string "Cancel"
on error
set z to "Cancel"
end try
return (z is not "Annuler")
end parleAnglais
--=====
on activateGUIscripting()
(* to be sure than GUI scripting will be active *)
tell application "System Events"
if not (UI elements enabled) then set (UI elements enabled) to true
end tell
end activateGUIscripting
--=====
(*
==== Uses GUIscripting ====
*)
(*
This handler may be used to 'type' text, invisible characters if the third parameter is an empty string.
It may be used to 'type' keyboard raccourcis if the third parameter describe the required modifier keys.
I changed its name « shortcut » to « raccourci » to get rid of a name conflict in Smile.
*)
on raccourci(a, t, d)
local k
tell application a to activate
tell application "System Events" to tell application process a
set frontmost to true
try
t * 1
if d is "" then
key code t
else if d is "c" then
key code t using {command down}
else if d is "a" then
key code t using {option down}
else if d is "k" then
key code t using {control down}
else if d is "s" then
key code t using {shift down}
else if d is in {"ac", "ca"} then
key code t using {command down, option down}
else if d is in {"as", "sa"} then
key code t using {shift down, option down}
else if d is in {"sc", "cs"} then
key code t using {command down, shift down}
else if d is in {"kc", "ck"} then
key code t using {command down, control down}
else if d is in {"ks", "sk"} then
key code t using {shift down, control down}
else if (d contains "c") and (d contains "s") and d contains "k" then
key code t using {command down, shift down, control down}
else if (d contains "c") and (d contains "s") and d contains "a" then
key code t using {command down, shift down, option down}
end if
on error
repeat with k in t
if d is "" then
keystroke (k as text)
else if d is "c" then
keystroke (k as text) using {command down}
else if d is "a" then
keystroke k using {option down}
else if d is "k" then
keystroke (k as text) using {control down}
else if d is "s" then
keystroke k using {shift down}
else if d is in {"ac", "ca"} then
keystroke (k as text) using {command down, option down}
else if d is in {"as", "sa"} then
keystroke (k as text) using {shift down, option down}
else if d is in {"sc", "cs"} then
keystroke (k as text) using {command down, shift down}
else if d is in {"kc", "ck"} then
keystroke (k as text) using {command down, control down}
else if d is in {"ks", "sk"} then
keystroke (k as text) using {shift down, control down}
else if (d contains "c") and (d contains "s") and d contains "k" then
keystroke (k as text) using {command down, shift down, control down}
else if (d contains "c") and (d contains "s") and d contains "a" then
keystroke (k as text) using {command down, shift down, option down}
end if
end repeat
end try
end tell
end raccourci
--=====
(*
==== Uses GUIscripting ====
most of this handler is from Nigel Garvey
*)
on selectSheet(theDoc, theSheet)
script myScript
property listeObjets : {}
local maybe, targetSheetRow
tell application "Numbers"
activate
set theDoc to name of document theDoc (* useful if the passed value is a number *)
tell document theDoc to set my listeObjets to name of sheets
end tell -- "Numbers".
set maybe to theSheet is in my listeObjets
set my listeObjets to {} -- So it will not be saved in the script *)
if not maybe then
if my parleAnglais() then
error "The sheet "" & theSheet & "" is unavailable in the spreadsheet "" & theDoc & "" !"
else
error "La feuille « " & theSheet & " » n'existe pas dans le tableur « " & theDoc & " » ! "
end if -- my parleAnglais
end if -- not maybe
set maybe to 5 > (system attribute "sys2")
tell application "System Events" to tell application process "Numbers"
tell outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window theDoc
if maybe then (* macOS X 10.4.x
'(value of attributes contains 0)': '(value of attribute "AXDisclosureLevel" is 0)' sometimes works in Tiger, sometimes not.
The only possible instances of 0 amongst the attributes are the disclosure level of a sheet row and the index of the first row, which represents a sheet anyway.
Another possibility is '(value of attribute -1 is 0)', which makes me uneasy. *)
set targetSheetRow to first row where ((value of attributes contains 0) and (value of first static text is theSheet))
else (* macOS X 10.5.x or higher *)
set targetSheetRow to first row where ((value of attribute "AXDisclosureLevel" is 0) and ((groups is {}) and (value of first static text is theSheet)) or (value of first group's first static text is theSheet))
end if -- maybe.
tell targetSheetRow to set value of attribute "AXSelected" to true
set cnt to 0
repeat (*
Must way that Numbers becomes ready to receive the value *)
try
tell targetSheetRow to set value of attribute "AXDisclosing" to true
exit repeat
on error
set cnt to cnt + 1
delay 0.5 -- half a second
end try
end repeat
end tell -- outline.
end tell -- "System Events".
tell application "Numbers" to tell document theDoc to tell sheet theSheet to tell table 1
with timeout of 20 * 60 seconds (*
WITH this setting, the script will be able to wait 20 minutes for the asked value.
I hope that the document will not be so huge that this delay prove to be too short. *)
value of cell "A1"
end timeout
end tell -- "Numbers".
tell application "System Events" to tell application process "Numbers" (*
Do the trick one more time to be sure that the sheet is open *)
tell targetSheetRow to set value of attribute "AXDisclosing" to true
end tell -- "System Events".
end script
run myScript
end selectSheet
--=====
--[/SCRIPT]
--{code}
Yvan KOENIG (VALLAURIS, France) mardi 1 mars 2011 22:35:10