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.
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?)
//
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
# 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.
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