Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

yux

macrumors newbie
Original poster
Sep 9, 2011
6
0
I am trying to write an applescript to open a Numbers file and perform some tasks with the file, say display the row count of the file. Here is what I wrote:

tell application "Numbers"
tell application "Finder"
set theFile to file "myfolder:myfile.numbers"
open theFile
end tell
-- the above part works well
tell document theFile to tell sheet 1 to tell table 1
set z to row count
end tell
end tell
display dialog ("The row count is" & z)

The open file part works well, but I cannot get the opened file referred to, no matter what. Could anyone teach me how to solve this problem? Thanks!
 
Refer to the document by name, not the path

E.g. tell document "my file"
Or tell document whose name is "my file"
Or something like that.
 
AppleScript: How to refer to a file opened by the same script

Thanks for getting back to me this fast! I changed the script as follows, but it still does not work. The error message says "Can't get document whose name = "myfile.numbers".

tell application "Numbers"
tell application "Finder"
set theFile to file "myfolder:myfile.numbers"
open theFile
end tell
-- the above part works well
tell (document whose name is "myfile.numbers") to tell sheet 1 to tell table 1
set z to row count
end tell
end tell
display dialog ("row count is" & z)
 
If I open the file manually and run the following script, or substitute the document name with document 1, it works well:

tell application "Numbers"
tell document "myfile.numbers" to tell sheet 1 to tell table 1
set z to row count
end tell
end tell
display dialog ("The row count is " & z)

It seems the script just can't recognize a file opened by itself.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.