Cant Get document 1 of application "Numbers"

Hi there,

I am writing a script that needs to run on the front-most open numbers document on a users machine.

Scenario:

  • User has several Numbers docs open on their machine
  • User clicks on a Numbers doc to make it the frontmost, active document
  • User runs script
  • Script runs on Document 1

Currently, I have a script that looks like what I have pasted below. However, on some users machines, it throws the error "Can’t get document 1 of application “Numbers”. (-1728)

It’s an error I can’t reproduce on my own machine, but for some users, Applescript just can’t get document 1. Even when they only have 1 document open!

I have searched this forum and I think this can be solved by emptying numbers cache files (maybe) but I’m hesitant to ask users to do that just to run a simple script.

Any ideas?

tell application "System Events" to tell process "Numbers"
	set frontmost to "true" # == Tells Numbers to set frontmost document to true
end tell

tell application "Numbers"
	tell document 1 to tell sheet 1 to tell table 1
		
		--some script
		
	end tell
end tell

I’m a bit puzzled.
Here your script behaves flawlessly.

tell application "System Events" to tell process "Numbers"
	set frontmost to "true" # == Tells Numbers to set frontmost document to true
end tell

tell application "Numbers"
	name of documents --> {"Sans titre", "Chèques YK 2016.numbers"}
	tell document 1 to tell sheet 1 to tell table 1
		count of rows --> 22
		--some script
		
	end tell
	
end tell

I’m surprised by the way you bring it to front.

It seems cleaner to use :

tell application "Numbers"
	activate # ADDED
	name of documents --> {"Sans titre", "Chèques YK 2016.numbers"}
	tell document 1 to tell sheet 1 to tell table 1
		count of rows --> 22
		--some script
		
	end tell
	
end tell

I guess that you must search deeper to identify what instruction which you don’t think about is causing your problem.

As you already know, you may send the complete script to my mailbox if you wish that I help you to find the wrongdoer.

To be sure that it’s not the culprit, I opened two excel documents in Numbers v4.0.5 and the script behaved flawlessly too. Below is the history. I apologizes but at this time, the [Format] button is missing in the forum’s window.

tell application "System Events"
	set frontmost of process "Numbers" to "true"
end tell
tell application "Numbers"
	get name of every document
		--> {"book-marks", "book-marks09x"}
	count every row of table 1 of sheet 1 of document 1
		--> 9757
end tell
Résultat :
9757

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mardi 15 novembre 2016 18:03:19

Thanks Yvan :slight_smile:
User Error I call it! I’m asking the team for more info and I’ll report back if i find a solution.

answered at end. But I’ve left this here in case others have the question

Could I ask - what is the “clear caches” trick?

I have exactly this problem of “can’t get document 1” on my machine - or more specifically, my userspace. If I log out and try it as another user, it works fine. So how do I get it to function in my userspace?

What’s the trick to making it work if it’s refusing to work?

(Note: I’ve looked in ~/Library/Caches and there’s nothing relating to iWork or Numbers; in ~/Library/Application Support/ there’s only iWork/Templates. The puzzle is that this works, as I say, in another userspace but not mine, where I normally run lots of Applescripts. (Though there are some oddities - I can’t run some scripts from the Menu bar, some don’t give dialogs when I expect them. Could it be the userspace version of Applescript that’s at fault?)
//

The answer is given at https://discussions.apple.com/thread/7532076?start=0&tstart=0 -
“I had to delete the container file at ~/Library/Containers/com.apple.iWork.Numbers and my scripts are back and working again!”

This worked for me.

Hey Charles!

Thanks for posting your fix. I’m still non the wiser as to why it’s happening for my users. I’ll get them to delete that file and see if we have any success.

Maybe it’s possible to write another script that can find and delete this file? I will investigate on Monday :slight_smile:

Adam

You may get rid of the caches items with :

# Numbers must be closed before deleting the folder

tell application "Numbers" to quit

set NumbersCacheFolder to (path to library folder from user domain as text) & "Containers:com.apple.iWork.Numbers:Data:Library:Caches:"
tell application "System Events"
	set theCaches to every disk item in folder NumbersCacheFolder
	repeat with aCache in theCaches
		try
			delete aCache
		end try
	end repeat
end tell

I’m a bit reluctant do delete the entire container item with :

# Numbers must be closed before deleting the folder

tell application "Numbers" to quit

set NumbersContainer to (path to library folder from user domain as text) & "Containers:com.apple.iWork.Numbers:"
tell application "System Events"
	delete disk item NumbersContainer
end tell

Try to apply the first one and, only if it’s not sufficient, apply the second one.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) samedi 19 novembre 2016 20:55:32

Hi, I finally got results back from several team members that were experiencing this issue and I can confirm that in every case, using yvans script to clear cache has solved this issue.

I should note that I didn’t test the second script that deletes the container.

Thanks for the solution Yvan and Charles!

Deleting the entire container would not be a good idea because it contain some files which may be useful.

(1) the User templates
(2) the datas related to collaborative work
(3) numerous preferences files which are symlinks pointing upon themselves
(4) Numbers Saved Application State
(5) SyncedPreference
(6) several cache files
(7) numerous symlinks pointing upon ‘standard’ ones

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mercredi 30 novembre 2016 10:44:46