Saving a Numbers Document with Name Taken From a Cell On The Sheet

Ok, this is weird, and admittedly, I'm no AppleScript expert, but I have had a working AppleScript Workflow running as a service in Numbers, but when I moved it from my MBP running Sequoia 15.71 to an Apple Studio running the current version of Tahoe it worked for a short period then started generating an error.


Basically, the Service (AppleScript) grabs the name from a cell that exists on a table on the second sheet and saves it in a folder on the desktop labeled: "Expense Reports". It works flawlessly on the MBP. On the Mac Studio it throws this error:


"The document "untitled.numbers" could not be exported as "Henry Smith". You don't have permission. To view or change permissions, select the item in the Finder and choose File > Get Info"


Then it throws some other error afterwards about "Apple EventHandler failed. . . "


There's probably a "best practice" step I'm missing in my script. I'm just wondering if there's a way to explicitly grant the necessary access to the save location within the script itself to avoid the error, or perhaps there's something incorrect within the code itself.


The code that is working on the MBP is:


on run {input, parameters}


tell application "Numbers"


tell the front document


set active sheet to sheet 2


tell the active sheet


tell the first table -- Where the Name exists


set cellValue to the value of cell "B3" -- Get the name of the person on the report


if cellValue is not missing value then


display dialog “Expense Report For: " & cellValue & " Created!”


tell application "Numbers"


-- Referencing the front document in Numbers App


set theDocument to the front document


                         -- Access the second sheet and first table 


tell the second sheet of theDocument


tell the first table -- Where the name should exist


                                  set cellValue to the value of cell “B3”


                                   -- Get the name of the person on the report


end tell


end tell


-- Define the save location and filename 


set savePath to ("Macintosh HD:Users:MAIN_WORK:Desktop:Expense Reports:" as text) & cellValue & ".numbers"


-- Save the document with the derived filename


save theDocument in file savePath


end tell


else


display dialog "Cannot Save! No member name present!"


end if


end tell


end tell


end tell


end tell


return input


end run


I'd say the script is simple enough, and if anyone here can help me out, that would be great. Feel free to clean it up hopefully fix the errors.


Thanks in advance!

Posted on Nov 1, 2025 6:22 AM

Reply
Question marked as Top-ranking reply

Posted on Nov 2, 2025 1:18 PM

There are some flaws with the script.


First off is that your code for extracting the person's name seems to be duplicated... you work your way down to cell B3 of first table of the second sheet of the front document:



After verifying it's not empty (always a good idea), you then proceed to do the almost identical code again, inside another 'tell application "Numbers" block (rarely a good idea to nest 'tell application' blocks, and never a good idea (or a need) to tell the same application twice):



Either way, as far as I can see, this whole second 'tell application "Numbers" block is superfluous.


The second problem (and, I suspect, related to the issue at hand) is the line:


		set savePath to ("Macintosh HD:Users:MAIN_WORK:Desktop:Expense Reports:" as text) & cellValue & ".numbers"


Are you using MAIN_WORK as your user account?

Unless you are logged in as the user MAIN_WORK, you won't have permissions to access that user's home directory.


Direct paths like this are ALWAYS better off using the built-in tools to find common directories. For example, rewrite this line as:


set savePath to (path to desktop as text) & "Expense Reports:" & CellValue & ".numbers"


path to desktop will resolve at runtime to find the current user's Desktop Folder in their home directory.

(Note: might want to add a check that the Expense Reports folder exists before continuing).


Other problems include the fact that you say the expense report was saved before you actually save it. Your display dialog appears as soon as you extract the name, but there are all kinds of problems that can occur before the document is actually saved (such as the permissions issue you're running into, the path being incorrect, existing filename conflicts, etc.) Far, far better to send the confirmation AFTER the save command to avoid sending wrong signals to the user.


Here's my re-worked version of your script. Seems to work for me:


on run {input, parameters}
	tell application "Numbers"
		tell the front document
			tell sheet 2
				tell the first table -- Where the Name exists
					set cellValue to the value of cell "B3" -- Get the name of the person on the report
				end tell
			end tell
		end tell
		if cellValue is not missing value then
			-- Define the save location and filename 
			set savePath to (path to desktop as text) & "Expense Reports:" & cellValue & ".numbers"
			-- Save the document with the derived filename
			save document 1 in file savePath
			display dialog "Expense Report For: " & cellValue & " Created!"
		else
			display dialog "Cannot Save! No member name present!"
		end if
	end tell
	return input
end run




5 replies
Question marked as Top-ranking reply

Nov 2, 2025 1:18 PM in response to Ed M.

There are some flaws with the script.


First off is that your code for extracting the person's name seems to be duplicated... you work your way down to cell B3 of first table of the second sheet of the front document:



After verifying it's not empty (always a good idea), you then proceed to do the almost identical code again, inside another 'tell application "Numbers" block (rarely a good idea to nest 'tell application' blocks, and never a good idea (or a need) to tell the same application twice):



Either way, as far as I can see, this whole second 'tell application "Numbers" block is superfluous.


The second problem (and, I suspect, related to the issue at hand) is the line:


		set savePath to ("Macintosh HD:Users:MAIN_WORK:Desktop:Expense Reports:" as text) & cellValue & ".numbers"


Are you using MAIN_WORK as your user account?

Unless you are logged in as the user MAIN_WORK, you won't have permissions to access that user's home directory.


Direct paths like this are ALWAYS better off using the built-in tools to find common directories. For example, rewrite this line as:


set savePath to (path to desktop as text) & "Expense Reports:" & CellValue & ".numbers"


path to desktop will resolve at runtime to find the current user's Desktop Folder in their home directory.

(Note: might want to add a check that the Expense Reports folder exists before continuing).


Other problems include the fact that you say the expense report was saved before you actually save it. Your display dialog appears as soon as you extract the name, but there are all kinds of problems that can occur before the document is actually saved (such as the permissions issue you're running into, the path being incorrect, existing filename conflicts, etc.) Far, far better to send the confirmation AFTER the save command to avoid sending wrong signals to the user.


Here's my re-worked version of your script. Seems to work for me:


on run {input, parameters}
	tell application "Numbers"
		tell the front document
			tell sheet 2
				tell the first table -- Where the Name exists
					set cellValue to the value of cell "B3" -- Get the name of the person on the report
				end tell
			end tell
		end tell
		if cellValue is not missing value then
			-- Define the save location and filename 
			set savePath to (path to desktop as text) & "Expense Reports:" & cellValue & ".numbers"
			-- Save the document with the derived filename
			save document 1 in file savePath
			display dialog "Expense Report For: " & cellValue & " Created!"
		else
			display dialog "Cannot Save! No member name present!"
		end if
	end tell
	return input
end run




Nov 1, 2025 8:37 AM in response to Ed M.


Ed M. wrote:

I thought there might be something I could add to the script that would allow access so the file can be saved in that location.

EDIT: I think I'm going to try granting the Numpers.app Full Disk Access.



With each new macOS release, security seems to get more and more fiddly...


While you're at it, under Privacy & Security you might also try giving Script Editor full disk access if you haven't done so already, and Automator too (in case that is the problem with Services).


If that doesn't work for this situation then you might want to go back and remove full disk access just so you don't create unintended problems in the future when you're doing something else.


Also in Finder go to Macintosh HD:Users:MAIN_WORK:Desktop:Expense Reports:, right-click, and make sure Sharing and Permissions is set to Read & Write.


Seemingly unrelated: the fact that the error message refers to "untitled.numbers" suggests that the script was not successful in picking up the name from the cell.


SG

Saving a Numbers Document with Name Taken From a Cell On The Sheet

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.