How To See A Document

The following code runs OK if the spreadsheet is visible but doesn’t if it has not been opened.

I’d like to add the code to open the document so I can see it rather than doing it manually. Could someone tell me what I need to add.

Thanks


tell application "Numbers"
	activate
	tell document "Test"
		tell sheet "Sheet 1"
			tell table "Table 1"
				local myVar, i, ar
				set ar to {} as string
				set i to 1
				repeat with i from 2 to 6
					set myVar to value of cell ("C" & i)
					set ar to ar & myVar & ", "
				end repeat
				display dialog ar
			end tell
		end tell
	end tell
end tell

Hi.

Believe it or not, you need an ‘open’ command. :slight_smile:

This requires an alias or file specifier to identify the file. One easy way to get it is to use ‘choose file’ and store the result as a property in the script. The dialog should only appear when you run the script for the first time after compiling it or if something subsequently happens to make the alias invalid.

property spreadsheetAlias : missing value

local theDoc, myVar, i, ar

tell application "Numbers"
	activate
	try
		set theDoc to (open spreadsheetAlias)
		theDoc
	on error
		set spreadsheetAlias to (choose file)
		set theDoc to (open spreadsheetAlias)
	end try
	
	tell theDoc
		tell sheet 1
			tell table 1
				set ar to ""
				set i to 1
				repeat with i from 2 to 6
					set myVar to value of cell ("C" & i) -- or: set myVar to value of cell i of column "C"
					set ar to ar & myVar & ", "
				end repeat
				display dialog ar
			end tell
		end tell
	end tell
end tell

Thanks Nigel. That works well. If you don’t mind I have a couple of questions. I’m new to Mac as well as AppleScript.

  1. What exactly is spreadsheetAlias. Is it just a global variable?

  2. In the “try” section “theDoc” is on it’s own. What exactly does this do?

  3. Regarding the variable ar, you replaced - set ar to {} as string - with set ar to “”. Are these equivalent?

Thanks,

Hello

I would not extract the values thru a loop.

This scheme is more efficient :


--=====

tell application "Numbers" to tell document 1 (* replace 1 by the variable of your choice *)
	tell sheet 1 to tell table 1
		tell column 3
			set les_valeurs to value of cells 2 thru 6
		end tell
	end tell
end tell
set ar to my recolle(les_valeurs, ", ")

--=====

on recolle(l, d)
	local oTIDs, t
	set oTIDs 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 oTIDs
	return t
end recolle

--=====

I don’t know if it matters to you but concatenating values as you do (using a comma + a space) may give an awful result if the values are numbers and the decimal separator is comma.

Assume that the numbers are
10.0
12.0
9.0
6.0
7.0

On a system using the decimal period, the concatenation return : “10.0, 12.0, 9.0, 6.0, 7.0”
On a system using the decimal comma, the concatenation return : “10,0, 12,0, 9,0, 6,0, 7,0”

Yvan KOENIG (VALLAURIS, France) mercredi 13 avril 2011 23:17:12

It’s a property. Very similar to a global variable, but it’s given a value when the script’s compiled. This value, or any other value the property may have acquired during a run of the script, is saved back into the script file at the end of the run so that the property has that value at the beginning of the next run.

I’ve given it the initial placeholder value ‘missing value’, but it’s intended to hold an alias to your spreadsheet file. If it’s not an alias to a file that Numbers can open, the ‘on error’ part of the ‘try’ statement asks you to choose a file. On subsequent runs, the value will still be the alias and you won’t have to choose again ” unless you recompile the script, of course.

I noticed when testing my code that ‘open’-ing the initial ‘missing value’ didn’t trigger the error needed to go into the ‘on error’ section, but simply left ‘theDoc’ undefined. This caused an error at the ‘tell theDoc’ line, where we didn’t want it. The solitary ‘theDoc’ (perhaps better written as ‘get theDoc’) attemps to get the value of ‘theDoc’ and does error if it’s undefined, thus forcing the execution of the ‘on error’ section.

They have the same end result, but ‘{} as string’ makes an empty list and then derives an empty string from it, whereas “” simply makes an empty string.

Thank you Yvan and Nigel. Lots of good info.