Incorporating Applescript in Numbers
Hi all,
I have made the move from Excel to Numbers but need to include a basic script I wrote in VBA for Excel. I understand this must be re-written which is OK.
The script is very simple. I just need to display the sum total of a column of numbers based on the cell color in that column. That is, if the cell color is red I count the number in the cell and if it is yellow I do not. I would like to display the sum total of the column in one cell somewhere in the spreadsheet.
Apologies if this has been answered many times before (I couldn't find anything when I looked) but can someone clearly outline the basic steps to create a new Applescript and include it in a Numbers application?
Can the script be run automatically each time the file is opened or changed?
Thanks in advance.
Inspereid wrote:
I am using a Simple summation formula ie "= +$A2 + $B2 - $C2 ....." in the right most cell and am now getting a strange error in that cell that says:
The operator "+" expects a number, date or duration but found "."
None of the cells I am adding contain a "." but they do themselves contain their own formula in the form of "= ...."
The first thing I would do is drop the unnecessary first "+" operator.
Also if this formula is on row 2 and will be filled down from there, you don't need the row number part of the cell references. And if it will be filled ONLY down, the $ operators are also unnecessary.
But none of those are connected with the error result.
Are there any circumstances under which the formula in column A, B or C will insert a string (including the possibility of a null string) into the cell?
Examples are
=IF(Z2=0,"",Z5)
=IFERROR(expression," ")
The + operator works only with the three value types listed.
SUM() will treat text values as zeros, and not return an error, so replacing the formula with this one would return a numerical result (for number values or a mix of number and text values in A, and B and a number value in C). The - operator would still object to a text value in C2.
=SUM(A2,B2,-C2
Note that as SUM accepts a range of values as an argument, the number part of a single cell reference IS necessary in this case.
Regards,
Barry
Similar Messages
-
Use of applescript in numbers to automatically analyze data?
Hi all,
I am trying to figure out if it is possible to use applescript and numbers to achieve the following:
(1) Import a CSV file
(2) Work out an average and standard deviation for particular columns of data in that CSV file
(3) Copy and paste out those averages and standard deviations to a separate numbers file, along with the name of the original data file, so in the end I have a file that contains a number of rows, each one containing the results for each data file and the file name.
The files (from an analytical instrument) are all the same, so ideally I could use automator and/or applescript to automatically run through a directory of csv files to achieve this.
I know only a small amount of very basic programming. I don't want someone to write this for me, but if someone could offer some pointers or examples that would be very helpful. Or tell me that what I am trying to do is foolish, and use something else instead.
ThanksAs I got a sample file, I was able to build a script matching the full requirements.
Here is a revised version which may be useful for other users.
--[SCRIPT average&stdev_CSV]
Enregistrer le script en tant que Script : average&stdev_CSV.scpt
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.
aller au menu Scripts , choisir Numbers puis choisir average&stdev_CSV
Sélectionner un dossier contenant des fichiers CSV.
Le script ouvre ces fichiers dans Numbers,
calcule les valeurs MOYENNE() et ECARTYPE()
écrit celles-ci avec le nom du document dans un fichier texte.
Après traitement de tous les fichiers le fichier texte et ouvert dans Numbers.
On peut également enregistrer le script en tant que progicile (application sou 10.6.x).
Il suffira alors de glisser/déposer l'icone du dossier à traiter sur celle du script_application.
--=====
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: average&stdev_CSV.scpt
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.
go to the Scripts Menu, choose Numbers, then choose "average&stdev_CSV"
Select a folder containing CSV files.
The script open them in Numbers,
calculate the AVERAGE() and STDEV() values
write them with the file name in a text file.
When all files are treated, the text file is opened in Numbers.
You may also save the script as an application package (application under 10.6.x).
Then drag & drop the icon of the folder to treat on the scipt_application icon.
--=====
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)
2010/06/10
2010/06/11 -- now read the csv file only once
--=====
property permis : {"public.comma-separated-values-text", "public.csv"}
property AVERAGE_loc : missing value
property STDEV_loc : missing value
property delim : missing value
property deci : missing value
property altDelim : missing value
property altDeci : missing value
property dossier_temporaire : missing value
property mon_Rapport : missing value
--=====
Entry point used when we double click the script's icon
or when we call it from the Scripts menu .
on run
if my parleAnglais() then
set le_prompt to "Choose a folder containing CSV file …"
else
set le_prompt to "Choisir un dossier contenant des fichiers CSV …"
end if -- parleAnglais
my main(choose folder with prompt le_prompt without invisibles)
end run
--=====
Entry point used when we drag & drop a folder icon on the application_script's icon
on open sel
my main(sel)
end open
--=====
on main(selected_item) -- it's an alias
local isFolder, nomdurapport, les_fichiers, type_ID, un_fichier, nombrededocuments, nomdutableur
the selected folder's pathname
set selected_item to selected_item as text
tell application "System Events"
set isFolder to class of disk item selected_item is folder
end tell -- System Events
if not isFolder then
if my parleAnglais() then
error "You must select a folder containing CSV file …"
else
error "Vous devez choisir un dossier contenant des fichiers CSV …"
end if -- parleAnglais
end if -- not isFolder
Init once variables which will be used later.
set dossier_temporaire to (path to temporary items) as text (* property *)
set AVERAGE_loc to my getLocalizedFunctionName("Numbers", "AVERAGE") & "(" (* property *)
set STDEV_loc to my getLocalizedFunctionName("Numbers", "STDEV") & "(" (* property *)
set {delim, deci, altDelim, altDeci} to my getLocalized_Delimiters() (* properties *)
set nomdurapport to "resume" & my dateTimeStamp() & ".txt" (* locale *)
set mon_Rapport to dossier_temporaire & nomdurapport (* property *)
Extract the list of items stored in the selected folder.
Create the resumeyyyymmddhhmmss.txt temporary file.
tell application "System Events"
set les_fichiers to every disk item of folder selected_item
make new file at end of folder dossier_temporaire with properties {name:nomdurapport}
end tell
Scan the items stored in the folder.
Skip the folders and documents which aren't csv ones.
repeat with un_fichier in les_fichiers
tell application "System Events"
if class of un_fichier is folder then
set type_ID to "Oops, I‘m a folder" (* so it will not be deciphered *)
else
set type_ID to type identifier of un_fichier
set un_fichier to path of un_fichier
end if
end tell -- System Events
if type_ID is in my permis then my traiteun_fichier(unfichier)
end repeat -- with un_fichier
The scan is done, open the resumeyyyymmddhhmmss.txt temporary file in Numbers.
tell application "Numbers"
set nombrededocuments to count of documents
open mon_Rapport
repeat while (count of documents) = nombrededocuments
delay 0.2
end repeat
set nomdutableur to name of document 1
Set cells format to the Scientific one but I can't define the number of decimals !
tell document 1 to tell sheet 1 to tell table 1
set format of range ("B1 : " & name of last cell) to scientific
end tell
save document 1 in (selected_item & nomdutableur)
end tell -- Numbers
Delete the temporary text file
tell application "System Events" to delete disk item mon_Rapport
Reset properties so there content will not be stored in the script
set AVERAGE_loc to missing value
set STDEV_loc to missing value
set delim to missing value
set altDelim to missing value
set deci to missing value
set altDeci to missing value
set dossier_temporaire to missing value
set mon_Rapport to missing value
end main
--=====
on traiteunfichier(unFichier) (* text item *)
local dossierdetravail, nomdu_csvtemporaire, csv_temporaire, utile, cnt, part2
local |dernière|, nombreDeDocuments, nomdutableur, laPlage
tell application "System Events"
tell disk item unFichier
set dossierdetravail to path of container -- of disk item unFichier
set nomdu_csvtemporaire to name -- of disk item unFichier
end tell -- unFichier
if nomdu_csvtemporaire does not end with ".csv" then set nomdu_csvtemporaire to nomdu_csvtemporaire & ".csv"
set csv_temporaire to (dossier_temporaire & nomdu_csvtemporaire)
if exists disk item csv_temporaire then delete disk item csv_temporaire
make new file at end of folder dossier_temporaire with properties {name:nomdu_csvtemporaire}
end tell -- System Events
Read the entire file
set utile to (read file unFichier)
Check that it's matching our requirements. I'm not sure that the second test is always valid.
if (utile contains "#=-=-=-=-=-=-=-=-=") and utile contains "#=-=-=-=-=-=-=-=-=End ./conf/ccia" then
set cnt to offset of "#=-=-=-=-=-=-=-=-=" in utile
Extract the second part which isn't formatted as the first one.
set part2 to text cnt thru -1 of utile
Extract the first part to decipher it.
set utile to (text 1 thru (cnt - 1) of utile) as text
Grabs the index of lower row
set |dernière| to count of (paragraphs of utile)
An ultimate test to be sure that the file is for us.
if (|dernière| > 0) and text -13 thru -2 of (paragraph 6 of utile) is "NFitsAvg'd" then
Normalize the datas according to the local settings
if not ((utile contains delim) and utile contains deci) then
if utile contains altDelim then set utile to my remplace(utile, altDelim, delim)
if utile contains altDeci then set utile to my remplace(utile, altDeci, deci)
end if
Write the normalized beginning and the untouched part2 in a temporary csv file.
write utile to file csv_temporaire
write part2 to file csv_temporaire starting at eof
Open the temporary csv in Numbers
tell application "Numbers"
set nombreDeDocuments to count of documents
open csv_temporaire
repeat while (count of documents) = nombreDeDocuments
delay 0.2
end repeat
set nomdutableur to name of document 1
tell document 1 to tell sheet 1 to tell table 1
Insert a row at top to insert the required formulas.
add row above first row
Insert the formulas calculating the AVERAGEs and the STDEVs .
repeat with c from 2 to 16 by 2
set laPlage to (name of cell 8 of column c) & " : " & name of cell |dernière| of column c
set value of cell 1 of column c to "=" & AVERAGE_loc & laPlage & ")"
set value of cell 1 of column (c + 1) to "=" & STDEV_loc & laPlage & ")"
end repeat -- with c
Extract the calculated values to build a new row in the resume.
set une_ligne to {nomdu_csvtemporaire}
tell row 1
repeat with c from 2 to 17
copy (value of cell c) as text to end of une_ligne
--copy (value of cell c) to end of une_ligne
end repeat
end tell -- row 1
end tell -- document
Save the new Numbers document.
I don't know if it is useful but it's easy to remove or disable the instruction.
save document 1 in (dossierdetravail & nomdutableur)
close document 1 saving no (* So, if you disable the Save instruction, it will close quietly *)
end tell -- Numbers
Write the new row in the resume text file.
write (my recolle(une_ligne, tab) & return) to file mon_Rapport starting at eof
end if -- (|dernière| > 0) or
end if -- read file unFichier…
Delete the temporary csv file
tell application "System Events" to delete file csv_temporaire
end traiteunfichier
--=====
Creates a new iWork document from the Blank template and returns its name.
example:
set myNewDoc to my makeAnIworkDoc(theApp)
on makeAnIworkDoc(theApp)
local t, n
if theApp is "Pages" then
set commun to "iWork '" & my get_iWorkNum("Pages") & ":Pages.app:Contents:Resources:Templates:Blank.template:"
try
set t to ((path to applications folder as text) & commun) as alias
on error
set t to ("Western 2:Applications communes:iWork '" & commun) as alias
end try
else if theApp is "Numbers" then
set commun to "iWork '" & my get_iWorkNum("Numbers") & ":Numbers.app:Contents:Resources:Templates:Blank.nmbtemplate:"
try
set t to ((path to applications folder as text) & commun) as alias
on error
set t to ("Western 2:Applications communes:" & commun) as alias
end try
else
if my parleAnglais(theApp) then
error "The application “" & a & "“ is not accepted !"
else
error "l’application « " & a & " » n’est pas gérée !"
end if
end if
tell application theApp
set n to count of documents
open t
repeat until (count of documents) > n
delay 0.1
end repeat
set n to name of document 1
end tell -- theApp
return n
end makeAnIworkDoc
--=====
on dateTimeStamp()
return (do shell script "date +_%Y%m%d-%H%M%S")
end dateTimeStamp
--=====
Set the parameter delimiters which must be used in Numbers formulas
on getLocalized_Delimiters()
if character 2 of (0.5 as text) is "." then
return {",", ".", ";", ","}
else
return {";", ",", ",", "."}
end if
end getLocalized_Delimiters
--=====
on get_iWorkNum(a)
local verNum
tell application a to set verNum to item 1 of my decoupe(get version, ".")
if (a is "Numbers" and verNum is "2") or (a is "Pages" and verNum is "4") then
return "09"
else
return "11"
end if
end get_iWorkNum
--=====
Useful to get function's localized name if we need to build formulas
examples:
set OFFSET_loc to my getLocalizedFunctionName("Numbers", "OFFSET")
set ADDRESS_loc to my getLocalizedFunctionName(theApp, "ADDRESS")
set INDIRECT_loc to my getLocalizedFunctionName(theApp, "INDIRECT")
on getLocalizedFunctionName(theApp, x)
return my getLocalizedName(theApp, x, (path to application support as text) & "iWork '" & ¬
my get_iWorkNum(theApp) & ":Frameworks:SFTabular.framework:Versions:A:Resources:")
end getLocalizedFunctionName
--=====
on getLocalizedName(a, x, f)
tell application a to return localized string x from table "Localizable" in bundle file f
end getLocalizedName
--=====
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 decoupe(t, d)
local TIDs, l
set TIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set l to text items of t
set AppleScript's text item delimiters to TIDs
return l
end decoupe
--=====
on recolle(l, d)
local TIDs, t
set TIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d
set t to l as text
set AppleScript's text item delimiters to TIDs
return t
end recolle
--=====
replaces every occurences of d1 by d2 in the text t
on remplace(t, d1, d2)
local TIDs, l
set TIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to d1
set l to text items of t
set AppleScript's text item delimiters to d2
set t to l as text
set AppleScript's text item delimiters to TIDs
return t
end remplace
--=====
--[/SCRIPT]
Yvan KOENIG (VALLAURIS, France) vendredi 11 juin 2010 19:01:46 -
I write VB and VBA at work all day every day. And applescript has never made sense to me. I have tried everything this morning to just rename the current document. Or add a sheet, anything! I cannot figure out applescript to save my life.
Can someone please post a very simple applescript just to show how we should at least get into the numbers program.
something like this but it works:
tell application "Numbers"
activate
set MyDocumentName to name of document
display dialog MyDocumentName
end tell
It just won't go, and it makes no sense to me, it should be simple, get the name of the document that is the ONLY one open.
If anyone can help em get started, I have such high hopes for what I can do with this now.I write programs at work that literally take dozens of "man hours" of work down to a few minutes. and would love to be able to do that with Numbers.
Thanks alot for anyones help in this basic thing,
Jason
p.s. Yvan, I did look in your idisk, but it is so full of stuff, I have no idea where to look for just applescript in Numbers Kept getting workbooks. The one thing I was able to find in applescript looked nothing like the above. Way too complex for starting out.Hi Jason
At this time there is no script using the ability to drive Numbers.
The available ones are using GUI scripting.
I will create a folder dedicated to Numbers '09
I posted my first script for the new beast in:
http://discussions.apple.com/thread.jspa?threadID=1857417&tstart=0
Yvan KOENIG (from FRANCE jeudi 8 janvier 2009 20:44:15) -
Call Applescript from Numbers 09
I am in the middle of switching over from PC to MAC. I used to use Excel but have switched to numbers. I can do everything I want but it is all manual. I used to use Macro's in Excel and I am looking for a way to use applescript from Numbers.
Is there a way to link a script to a shape or put a button in the menus?
I hope this question makes sense.
Thanks
SteveNot as far as I know but you can put the script into ~/user/Library/Scripts folder so it is accessible from the menu bar at top right of screen.
You might need to open application applescript editor and change pref to show "script menu in menu bar", (not sure if it is default or not). -
Applescript on Numbers to copy rows between tables
Hi,
I am an applescript newbie, but am trying to make my book-keeping spreadsheet a bit more comfortable. And this is the problem:
I have expenses that repeat every month and I would like to not have to rewrite them each time. So I thought I make a second table where I list all the repeating data and copy them into my main table. This works simply with selecting the row and then copy and paste in to the row of the main table.
But I would like to make it nicer, and my concept is to make this copy process simpler, by memorizing the row number of the second table, (e.g. row 5 contains the rental costs for my studio ), so I would select the last row and then type the number of the expected row in table 2 and have the content be automatically copied into the selected row of table 1 (main table).
I got the script to do so except the shortcuts for the keys. And I have no idea how to make the script understand that if I press the key for number "5" to start copying row 5 of table 2 into the last row of table 1. Here is my script as far as I got (it is far from finished I know, it is a test with 4 columns named "Art des Umsatzes", "Kategorie", "Projekt", "Betrag".
If anyone could give me advice ho to define that the keystrokes become variable b and d, I think I would be able to manage the rest.
Many thanks
Hans Peter
tell application "Numbers"
tell table 2 of sheet 1 of document 1
set a to b
set tags to {"Art des Umsatzes", "Kategorie", "Projekt", "Betrag"}
set tagValues to {}
set theRange to "B" & a + 1 & ":" & "G" & a + 1
set C to 1
set setArt to value of cell C of range theRange
set C to (C + 1)
set setKat to value of cell C of range theRange
set C to (C + 1)
set setProj to value of cell C of range theRange
set C to (C + 1)
set setBetr to value of cell C of range theRange
end tell
tell table 1 of sheet 1 of document 1
set a to d
set tags to {"Art des Umsatzes", "Kategorie", "Projekt", "Betrag"}
set tagValues to {}
set theRange to "B" & a + 1 & ":" & "G" & a + 1
set C to 1
set value of cell C of range theRange to setArt
set C to (C + 1)
set value of cell C of range theRange to setKat
set C to (C + 1)
set value of cell C of range theRange to setProj
set C to (C + 1)
set value of cell C of range theRange to setBetr
end tell
end tellHi HD
thanks for your prompt and very helpful response. Although I had to add some more, your suggestion paved the way. Your simpler suggestion with the reference does not really work (sorry.)
This is the solution. what you need to do is make 2 table. The first on has one row (title row) and 5 columns (first is title again). The second has 6 rows (one title) and 5 columns (first one title). then you fill in each of the cells a word or number.
Run the script and choose one of the 5 numbers, one row will be added and filled. That's what I hoped to get, only I cannot type, I have to click the number. That is a bit a shame, do you think Automator could make a bridge to connect the script number to the keys?
Well, anyhow. Thanks again for your great help.
All the best, Hans Peter
tell application "Numbers"
tell table 2 of sheet 1 of document 1
set b to (choose from list {1, 2, 3, 4, 5} with prompt "Nummer des Schlüssel")
if the result is false then return
set a to b as integer
set tags to {"Art des Umsatzes", "Kategorie", "Projekt", "Betrag"}
set tagValues to {}
set theRange to "B" & a + 1 & ":" & "G" & a + 1
set C to 1
set setArt to value of cell C of range theRange
set C to (C + 1)
set setKat to value of cell C of range theRange
set C to (C + 1)
set setProj to value of cell C of range theRange
set C to (C + 1)
set setBetr to value of cell C of range theRange
end tell
tell table 1 of sheet 1 of document 1
set d to count row
add row below last row
set d to d + 1
set tags to {"Art des Umsatzes", "Kategorie", "Projekt", "Betrag"}
set tagValues to {}
set theRange to "B" & d & ":" & "G" & d
set C to 1
set value of cell C of range theRange to setArt
set C to (C + 1)
set value of cell C of range theRange to setKat
set C to (C + 1)
set value of cell C of range theRange to setProj
set C to (C + 1)
set value of cell C of range theRange to setBetr
end tell
end tell -
I have written a simple script that I plan to use in the future for the Numbers program. Is there folder or a way I can get the script to show up in the menu or inspector? I am thinking of something like the script folder in Photoshop.
Thanks
MikeYour Scripts folder is found in your Library. To get to your Library folder, start in the Finder.
Press and hold the option key as you open the Go menu and choose Library.
The rest of the path:
Library > Scripts > Applications > Numbers > save-scripts-here
Note: you may have to create and name one or more of the folders on this path.
To use the script(s) from the menu bar, you will need to show the Scripts menu in the menu bar. Here are the instructions from an earlier version of Mac OS X (Tiger). Yours may differ:
To make the Script menu appear:
Open the AppleScript utility located in Applications/AppleScript.
Select the "Show Script Menu in menu bar" checkbox.
Regards,
Barry -
AppleScripting Pages & Numbers
I have an issue whereby a number of colleagues have created about 280 reports in separate [but consistent] .doc files which need to be tabulated into a single spreadsheet.
Fortunately the contents of the .doc files are in uniform tables of 2 columns and 24 rows [the first column contains labels so I only need the second column]. Now I can obviously open up each file and copy then past the data into numbers but I don't want to do that as it would take a while and is asking for mistakes. Obviously my preference would be for our good friend AppleScript to do it for me.
Now I have little experience with AppleScript and as I'm against the clock a bit here I don't have luxury of trial and error, so was hoping for a little help on here. I've managed to get Pages to copy and paste the whole table into numbers ok but I'm struggling with the next step. Basically I'm after a loop which will run through all the reports, select the second column of the table and paste it into the numbers table one column after the other.
Anyone have any idea how I'd do this?
Before anyone suggests exporting as txt files and comma delimiting, or export as html using VBA etc, I've tried many of these approaches already and because of carriage returns and other formatting the table keeps getting split into the wrong number of rows.
The only app I've tried which correctly copies and pastes the data is pages and numbers.
Many thanks in advance
Antonywell, at any rate, if you don't have Word, this should do the job. This is without frills (it doesn't check to make sure you have a Numbers spreadsheet open, or to see if the files you feed it are the correct Kind, or stuff like that).
set coalesceFolder to choose folder
tell application "System Events"
set fileList to POSIX path of (files of coalesceFolder whose visible is true)
end tell
set i to 1
repeat with thisFile in fileList
tell application "Pages"
open thisFile
tell document 1
select table 1
end tell
activate
end tell
tell application "System Events"
tell process "Pages"
keystroke "c" using command down
end tell
end tell
tell application "Numbers"
set insertionCell to "A" & i
set i to i + 24
tell document 1's sheet 1's table 1
set selection range to cell insertionCell
end tell
activate
end tell
tell application "System Events"
tell process "Numbers"
keystroke "v" using command down
end tell
end tell
end repeat -
How to set a list in applescript to numbers 1 to 100?
Hey guys,
I want to have a probability function and i am going to use applescript to get a random item of the list i want to make ( 1 to 100) and then get some item of it and then make the result trigger an if statement.
So what i want to know is how do i quickly make a list consisting of numbers 1 to 100, like this?
set list1 to (1...100)Generally, it's easier to use:
random number from 1 to 100
If you actually want to create a list for some reason:
set A to {}
repeat with B from 1 to 100
set A to A & B
end repeat
(63987) -
No AppleScript in Numbers?
Am I missing AppleScript support somewhere?
I, too, was appalled at the lack of AppleScript access. I could not believe it. But, Pages and Keynote got AppleScript access (as was pointed out), so I am optimistic.
I think this version of Numbers is intended to focus on the new user interface, which is, IMHO, wonderful. It is different from Excel's user interface in the same way the Mac OS user interface is different from that of Windows. Mind you, I use Excel a lot, and have written some powerful macros in it, but I am using Numbers every chance I get because I enjoy it.
Number with AppleScript is going to be awesome. -
Help with AppleScript for Numbers - please!
I posted a question on the AppleScript section, if anyone can help...
https://discussions.apple.com/thread/3451760
Thanks!
(I'm trying to get an array into what I know of in Excel as a "range". How can this be done with numbers?)Does this help? This would work if tagValues in your script is a single value.
tell application "Numbers" to tell document 1 to tell sheet 1 to tell table 1
set selection range to range "B2:D5"
set tagvalues to 1
set value of cell in selection range to tagvalues
end tell
If tagValues is a list, you may have to set each cell separately, indexing through each value in tagValues. -
Problem with applescript for Numbers
I am new to Applescript and am haveng problems with what seems to be a pretty simple task. I want to check the value of each cell to test for a certain condition and then based on the results increment another cell. I keep getting the following error:
error "Numbers got an error: Can’t make {4, 5} into type integer." number -1700 from {4, 5} to integer
Here is my script:
tell application "Numbers" to tell document 1 to tell sheet "Sheet 1" to tell table "Table 1"
set fNineStart to 4
set fNineEnd to 12
set bNineStart to 14
set bNineEnd to 22
repeat with irow from 8 to 40
set skinscount to 0
repeat with jcol from fNineStart to fNineEnd
set minscore to value of cell (jcol & 5)
if value of cell (jcol & irow) is equal to minscore and the value of cell (jcol & 6) is equal to 1 then
set skinscount to skinscount + 1
end if
end repeat
set the value of cell (27 & irow) to skinscount
end repeat
end tell
Here is the spreadsheet:
Name
1
2
3
4
5
6
7
8
9
Front
Nine
10
11
12
13
14
15
16
17
18
Back
Nine
Total
Net
ESC
score
Skins
Par
4
4
4
5
3
4
5
3
4
36
4
5
3
4
5
3
4
4
4
36
birdie
3
3
3
4
2
3
4
2
3
27
3
4
2
3
4
2
3
3
3
27
eagle
2
2
2
3
1
2
3
1
2
18
2
3
1
2
3
1
2
2
2
18
low score
3
4
4
3
3
4
5
3
4
4
4
3
4
5
3
4
4
4
# of people with min.
1
3
1
1
1
2
4
1
2
16
2
1
2
3
3
2
1
4
2
Skins
HCP
Joe
y
3
3
4
5
5
3
4
5
4
5
38
5
5
4
4
5
3
4
4
5
39
77
74
Bill
y
7
5
4
5
5
4
4
5
4
4Hi fairchild.bill,
Real experts will I hope chime in, but I think one problem is the way you are referring cells:
Instead of:
value of cell (jcol & irow)
I think you need to use something like:
value of cell irow of column jcol
And
value of cell jcol of row irow
Studying examples of scripts for Numbers 3 at macosxautomation.com may give you some ideas on this.
SG -
Updated Table Name Changes Applescript Operation in Numbers 3.5
I've been using Applescript in Numbers for some time to copy/paste data (set cell values) from one sheet table to another sheet table.
My original script was heavily cribbed from something Yvan Koenig had posted 2-3 years ago.
I recently updated to Yosemite and Numbers 3.5 after testing to ensure my original script remained functional (and it did).
I subsequently updated my source data table by duplicating my old table and editing this new source.
I then updated my Applescript to to reference the new source table.
Testing yielded a good functional result, but Numbers changed its behavior by displaying its work while setting values in the target sheet table.
This obviously slows the script down and adds measurable processing time given the amount of data being moved.
Does anyone know how I can prevent this behavior from occurring?
I don't believe I changed anything material in my original script so I'm really not sure why Numbers changed its behavior.
I have included my script below. Thanks very much for any help!
property the_goods : {}
property patient_name : {}
property patient_count : {}
property run_date : {}
on run
set sheet_source to "1500 EXPORT"
set table_source to "OA Data"
set column_source to 1
set row_source to 2
set sheet_destination to "CLEARING HOUSE EXPORT"
set table_destination to "Export Table"
set column_destination to 1
set nb_rows to 0
set nb_columns to 244
set patient_sheet to "SESSION DB"
set patient_source to "User Input Table"
set patient_tracker to "Tracking Table"
set source_column to 3
set source_row to 2
set destination_column to 2
set date_column to 3
set patient_count_column to 3
set patient_count_row to 3
set run_date_row to 5
tell application "Numbers"
set document_source to name of front document as string
set document_destination to name of front document as string
end tell
tell application "Numbers"
tell document document_source to tell sheet patient_sheet to tell table patient_source
set patient_count to value of cell patient_count_column of row patient_count_row
set run_date to value of cell patient_count_column of row run_date_row
end tell
end tell
set row_destination to patient_count
tell application "Numbers"
tell document document_source to tell sheet sheet_source to tell table table_source
set the_goods to value of cells column_source thru (column_source + nb_columns) of rows row_source thru (row_source + nb_rows)
end tell
tell document document_destination to tell sheet sheet_destination to tell table table_destination
set r to 0
repeat with one_row in the_goods
tell row (row_destination + r)
set c to 0
repeat with one_value in one_row
set value of cell (column_destination + c - 0) to one_value
set c to c + 1
end repeat
end tell
set r to r + 1
end repeat
end tell
tell document document_destination to tell sheet patient_sheet to tell table patient_source
activate
end tell
tell document document_destination to tell sheet patient_sheet to tell table patient_source
set patient_name to value of cell source_column of row source_row
end tell
tell document document_destination to tell sheet patient_sheet to tell table patient_tracker
set value of cell destination_column of row row_destination to patient_name
set value of cell date_column of row row_destination to run_date
end tell
end tell
set the_goods to {}
set patient_name to {}
set patient_count to {}
set run_date to {}
end runHere is a simple example that copies values from a source row 3 in Table 1 to a target row 5 in Table 2.
On my aging MBA it was very fast (about a second) with 150 columns.
In addition to speed, an advantage of this approach is that you don't have to code table dimensions; the target table will expand automatically if needed.
Save the script in a script folder (in my case ~/Library/Scripts/Applications/Numbers) and run it from there by choosing it from the script menu. (Running it from Script Editor will paste the values into Script Editor instead of into Numbers as you want).
If keeping the original number format is important, substitute 'formatted value' for 'value' in the script.
SG
property src : {d:1, s:"Sheet 1", t:"Table 1", r:3}
property tgt : {d:1, s:1, t:"Table 2", r:5}
tell application "Numbers"
tell document (src's d)
tell sheet (src's s)
tell table (src's t)
tell row (src's r)
set vv to cells's value -- makes AppleScript list
set the clipboard to my makeTSV(vv)
end tell
end tell
end tell
end tell
tell document (tgt's d)
tell sheet (tgt's s)
tell table (tgt's t)
set the selection range to row (tgt's r)'s cell 1
tell application "Numbers" to activate
tell application "System Events" to keystroke "v" using {option down, shift down, command down}
end tell
end tell
end tell
end tell
to makeTSV(aLst) -- turns AppleScript list into tab-delimited string
set oTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab
set tsvStr to aLst as text
set AppleScript's text item delimiters to oTID
return tsvStr
end makeTSV -
AppleScript resources and examples for Numbers 3
As Woggledog points out in this thread Numbers 3 now has AppleScript support (as of version 3.1). The linked article in turn has a link to some helpful resources, including working examples, at Mac OS X Automation.
It appears that the AppleScript support in Numbers 3 is now on a par with Numbers 2, perhaps already slightly better, and that many old scripts will work after minor adjustments.
Does anyone have other examples or links to sites showing useful things that can be done with AppleScript and Numbers?
SGHello SG,
Sorry i used this link to get in touch with you since i don't know how to get in touch directly with participants.
I had a reply on my concern by Quinn who told me you could perhaps help.
This is all about nested popup menu.
I triad to apply the process according to this thread, but honestly I came quickly to a wall.
I took snap shots of my table to make you understand what I wish to do (certainly quite common but my skills are poor).
I wrote some comments on the pictures to help. I hope it will...
In reference to the thread, Invoice would be the "Order Form" of the thread, and 2nd picture down below would be the "Products".
The 3rd picture is a table with prices of the rooms.
There are of course tables for each type of products.
I hope it is not too confusing.
This is my invoice:
In the first column PRESTATION i want the operator to choose in a popup list what has been sold (night, lunch, diner, bar, spa, transport, …) and then in the DESCRIPTION column i want another popup list appearing with products in accordance with what the operator chose in PRESTATION.
For example the operator chooses in 1st column the NIGHT (Nuitée in french), then DESCRIPTION column same row should come up with a popup list of the rooms (A, B, C, D, E, F, G) so the operator can choose what has been purchased.
Cherry on the cake: an automated unit price of the room should also appear in the PRIX A L'UNITE TTC column (Unit Price) depending on this choice of the room, and also status of the guest (low season rental, high season, friend-family, staff, tour operator customer, ...). The status of the customer is defined in a popup list in another sheet.
And of course, I would like this process to apply to all purchased made (this is what i tried to show with this table with colored arrows):
If in column 1 you provide Night, column 2 opens on a list of rooms, if it is transport then it opens on transport providers, if bBar then it opens on list of beverages, etc ...
As I said, if the price can be displayed automatically in the desired column Unit Price ... that would be perfect.
If i want to use the process of nested popup menus proposed in the thread, with that kind of complications, it becomes quite tough.
So I had been told the things can be automatized through an Applescript process. But this I don't one thing about this language, or even Automator.
Therefore, I guess it makes it rather difficult to find a "soft solution" to my problem.
What is unclear to me is why Apple does not develop expressively this feature since it is most useful.
Anyway, thank you for your time and professionalism on this one.
Lionnel. -
How to correct an Applescript recurring error
I have been trying to find the command in Applescript without success which will allow me to do the following:-
1. Cell cclr is numbered 1551 on an iWork Numbers spreadsheet comprising 150 rows by 130 columns.
2. This cell has a value of 1 inserted into it.
3. If the cell cclr contains the figure 1 I wish to set MyRGB to the background color of another cell wfc.
4 I then wish to set the background color of yet another cell, wpf, to the color which has been set to MyRGB.
I have tried many commands which all result in errors such as:-
set cclr to 1551
set wfc to 1419
set wpf to 1549
if cell cclr of the front document of the first sheet of the first table = 1 then set MyRGB to background color of cell wfc of the first table of the first sheet of the front document
set background color of cell wpf of the front document of the first sheet of the first table to MyRGB
On running this script I get an Applescript Error:- Numbers got an error: Can't get table 1 of table 1 of sheet 1 of document 1
Can anyone kindly give me a pointer as to how to accomplish this?
Kind regards,
Weaver158.Hi,
Weaver158 wrote:
However once it is incorporated into the script I am writing I get the Applescript error as before. I have tried all sorts of different combinations of the terminology in an endeavor to find a solution to no avail.
Because you have already specified the table at the beginning of the script.
tell the first document
tell the first sheet
tell the first table -- <---these 3 lines equal -- tell table 1 of sheet of document 1
So you must remove the block "tell table 1 of sheet 1 of document 1"
Here is the script
tell application "Numbers"
activate
tell the first document
tell the first sheet
tell the first table
set nwt to text returned of ¬
(display dialog "ENTER THE TOTAL NUMBER OF THREADS IN THE WARP REPEAT DEFINING EACH THREADS COLOR
AS IT IS ENTERED USING THE COLOR PICKER" default answer "")
set nwt to nwt as integer
set z1 to 0
set sc to 1419
set do to 119
set dd to 119
set dc to 0
set de to 0
repeat until z1 = nwt
set MyRGB to choose color
set the background color of cell sc to MyRGB
set z1 to (z1 + 1)
set sc to (sc - 1)
set do to (do - 1)
set dc to (dc + 1)
set de to (de + 1)
if dc = 4 then set do to (dd - (de * 150))
if dc = 4 then set dc to 0
end repeat
set nwp to text returned of (display dialog "ENTER THE TOTAL NUMBER OF THREADS IN THE WEFT REPEAT DEFINING EACH THREADS COLOR AS IT IS ENTERED USING THE COLOR PICKER" default answer "")
set nwp to nwp as integer
set z1 to 0
set sd to 1550
set dz to 1560
set dx to 1560
set dy to 0
repeat until z1 = nwp
set MyRGB to choose color
set the background color of cell sd to MyRGB
set z1 to (z1 + 1)
set sd to (sd + 130)
set dz to (dz - 1)
set dy to (dy + 1)
if dy = 4 then set dz to (dz + 600)
if dy = 4 then set dy to 0
end repeat
set wfc to 1549
set rwfc to wfc
set sd to 1550
repeat nwp times
repeat nwt times
set MyRGB to background color of cell sd
set the background color of cell rwfc to MyRGB
set rwfc to rwfc - 1
end repeat
set sd to sd + 130
set wfc to wfc + 130
set rwfc to wfc
end repeat
set MyRGB to ""
set wpf to 1549 ---First thread in warp---
set wpfd to 1549
set wfc to 1419 ---First colour in warp---
set wfcd to 141
set cclr to 1551 ---First lift in picking plan---
set wctr to 1
set w2 to 1
if value of cell cclr = 1 then
set background color of cell wpf to (get background color of cell wfc)
end if
end tell
end tell
end tell
end tell -
Numbers 3.5 (Incorrectly?) Activates Sheet When Processing Script
My earlier post was incorrectly marked as answered, so I have re-posted the original question along with a link to the first thread.
Updated Table Name Changes Applescript Operation in Numbers 3.5
I've been using Applescript in Numbers for some time to copy/paste data (set cell values) from one sheet table to another sheet table.
My original script was heavily cribbed from something Yvan Koenig had posted 2-3 years ago.
I recently updated to Yosemite and Numbers 3.5 after testing to ensure my original script remained functional (and it did).
I subsequently updated my source data table by duplicating my old table and editing this new source.
I then updated my Applescript to to reference the new source table.
Testing yielded a good functional result, but Numbers changed its behavior by displaying its work while setting values in the target sheet table.
This obviously slows the script down and adds measurable processing time given the amount of data being moved.
Does anyone know how I can prevent this behavior from occurring?
I don't believe I changed anything material in my original script so I'm really not sure why Numbers changed its behavior.
I have included my script below. Thanks very much for any help!
property the_goods : {}
property patient_name : {}
property patient_count : {}
property run_date : {}
on run
set sheet_source to "1500 EXPORT"
set table_source to "OA Data"
set column_source to 1
set row_source to 2
set sheet_destination to "CLEARING HOUSE EXPORT"
set table_destination to "Export Table"
set column_destination to 1
set nb_rows to 0
set nb_columns to 244
set patient_sheet to "SESSION DB"
set patient_source to "User Input Table"
set patient_tracker to "Tracking Table"
set source_column to 3
set source_row to 2
set destination_column to 2
set date_column to 3
set patient_count_column to 3
set patient_count_row to 3
set run_date_row to 5
tell application "Numbers"
set document_source to name of front document as string
set document_destination to name of front document as string
end tell
tell application "Numbers"
tell document document_source to tell sheet patient_sheet to tell table patient_source
set patient_count to value of cell patient_count_column of row patient_count_row
set run_date to value of cell patient_count_column of row run_date_row
end tell
end tell
set row_destination to patient_count
tell application "Numbers"
tell document document_source to tell sheet sheet_source to tell table table_source
set the_goods to value of cells column_source thru (column_source + nb_columns) of rows row_source thru (row_source + nb_rows)
end tell
tell document document_destination to tell sheet sheet_destination to tell table table_destination
set r to 0
repeat with one_row in the_goods
tell row (row_destination + r)
set c to 0
repeat with one_value in one_row
set value of cell (column_destination + c - 0) to one_value
set c to c + 1
end repeat
end tell
set r to r + 1
end repeat
end tell
tell document document_destination to tell sheet patient_sheet to tell table patient_source
set patient_name to value of cell source_column of row source_row
end tell
tell document document_destination to tell sheet patient_sheet to tell table patient_tracker
set value of cell destination_column of row row_destination to patient_name
set value of cell date_column of row row_destination to run_date
end tell
end tell
set the_goods to {}
set patient_name to {}
set patient_count to {}
set run_date to {}
end runHello
I don't use Numbers 3 and so this is a general advice. If the on-screen update is the cause of slowdown, you may simply hide the application. Like this.
tell application "System Events" to set process "Numbers"'s visible to false
-- main code here
tell application "System Events" to set process "Numbers"'s visible to true
Good luck,
H
Maybe you are looking for
-
Can I use Icloud on a samsung glaxay tablet 10.1
Can I use Icloud on my samaung glazay yablet 10.1 for texting
-
Can iPhone Mail redirect mail to subfolders like the Apple desktop Mail app
In my Mail app on my computer, I have Rules set up so that certain emails from various people get moved to a few different subfolders I have set up. However, in my iPhone, they all come to my primary email address. Is there any way to make iPhone mai
-
When I receive an original meeting request from a LotusNotes user, I can select the .ics attachment and it gives me an option to "add to calendar" which will place it on my calendar. However, when I receive a rescheduled meeting notification, I get a
-
hey folks, question from a video newbie. I've ordered iLife 09, hasn't arrived yet but it's coming. Just got a Flip Ultra HD video camera. The purpose is to shoot quick client testimonials for our business and upload them to YouTube. It works great e
-
IWork 5.0.1 can't open iWork 9 files?
iWork 5.0.1 can't open iWork 9 files? Gmail would not allow iWork 5.0.1 file!?