I have an AppleScript that I have been using for the last five years. It uses a tag to identify Numbers files in my databases (internal, not indexed) that I want to open in Numbers in one single action, and then it merges the separate windows into a single window.
It often runs without error, but very often one or more of the files will not open and throws an error: “file ‘blah’ can’t be opened right now, operation not permitted”. If I click ok, my script continues and opens the files that it can.
If I close Numbers, go back to DEVONthink and open an offending file manually from its context menu, “open with > Numbers” it opens without issue, and the next script run will work as well.
I just reminded myself from my notes in the comments of my script that “not sure why, but the script fails the first time a Numbers document is opened this way if it hasn’t been previously (manually) opened in Numbers from DEVONthink.”
This seems like a clue, but I have no idea what the root cause of the “operation not permitted” is.
The results aren’t repeatable, and I have a workaround, so I have limped along.
Today I decided to reach out to the community and see if anyone has a similar experience, or solution.
Here is my script:
-- PMF 2020-09-22
-- showNumbers
-- This script opens all of the Numbers files containing the tag
-- .pmf/showNumbers
--
-- not sure why, but the script fails the first time a Numbers
-- document is opened this way if it hasn't been previously (manually) opened in Numbers from DEVONthink.
--
-- 2020-10-10
-- updated to ensure that the file extension is hidden in Finder to prevent
-- the ".Numbers" extension showing up in the tab name in the Numbers merged window
-- since this makes matching more difficult in the Alfred selectSheet script
property tagtoShow : ".pmf/showNumbers"
tell application id "DNtp"
set theFileList to {}
set allDatabases to every database
repeat with thisDatabase in allDatabases
set tagGroup to children of (create location "/Tags/" & tagtoShow in thisDatabase)
repeat with thisDoc in tagGroup
-- check for Numbers file before adding
if kind of thisDoc is "Numbers Spreadsheet" then
-- force the extension to be hidden
set thisPath to path of thisDoc
tell application "Finder" to set extension hidden of file (POSIX file thisPath as alias) to true
set end of theFileList to thisPath
end if
end repeat
end repeat
end tell
-- open documents in a single window in Numbers, first open, then merge
tell application "Numbers" to open theFileList
tell application "System Events"
tell process "Numbers"
set frontmost to true
click menu item "Merge All Windows" of menu "Window" of menu bar 1
end tell
end tell
```
Well, what happens when you launch Numbers… ? This… every time.
Add this as the second line of your script…
tell application "Numbers" to launch
This doesn’t put up the bothersome Open dialog but silently launches the app in the background. How does it behave for you now?
No Jim, I don’t see that Open dialog each time my script runs.
I used to when I had this code in my script:
-- ensure that Numbers is running
-- this was causing a problem: brings up a dialog to select a document
-- so, I dropped this code 2020-10-13
tell application "System Events"
if not (exists process "Numbers") then
tell application "Numbers" to activate
end if
end tell
I added your code as you suggested anyway, but I still have my underlying problem where on occasion one or more files refuse to open.
Do you experience the same issue if you open a list of number files with a simplified script, ie only this line
? Then it’s not a DT issue.
Did you try opening the files one by one in a loop?
1 Like
- Why are you hiding the extension?
tell application "Finder" to set extension hidden of file (POSIX file thisPath as alias) to true
set end of theFileList to thisPath
- This is overly complicated for the task
tell application "System Events"
if not (exists process "Numbers") then
tell application "Numbers" to activate
end if
end tell
Just use
tell application "Numbers" to launch -- or activate
If it’s not running, it will.
If it is running, there’s no appreciable effect or it will come to the front with activate.
- Apple apps are often fussy about their paths. Coercion to an HFS-style
posix file is often a good thing to do with them.
tell application id "DNtp" to set r to get (path of (selected records))
tell application "Numbers"
activate
repeat with theFile in r
open (theFile as POSIX file)
end repeat
end tell
Thanks Jim! …this seems to be the solution.
To be complete this is what I tried before that final (as POSIX file) fix.
- I tried replacing the call with a loop as @chrillek suggested,
- then, also following @chrillek’s suggestion, I removed DEVONthink from the equation all together and, using a list of file names, I tried both as a list and in loop.
In all cases, I got the same failing behavior indicating that it was an AppleScript, or Numbers issue.
For reference these are the files that cause this problem before adding (as POSIX file) when opening the files.
set theFileList to {¬
"/Users/paul/Databases/pAccounts (Baseline).dtBase2/Files.noindex/numbers/1/nBaseline Running Costs.numbers", ¬
"/Users/paul/Databases/pAccounts (Baseline).dtBase2/Files.noindex/numbers/5/Airbnb Completed Reservations.numbers", ¬
"/Users/paul/Databases/pAccounts (Farrelle).dtBase2/Files.noindex/numbers/c/2024 (Kayvon).numbers", ¬
"/Users/paul/Databases/pAccounts (Farrelle).dtBase2/Files.noindex/numbers/d/Smith Barney.numbers", ¬
"/Users/paul/Databases/pAccounts (Farrelle).dtBase2/Files.noindex/numbers/1/nPahatsi Running Costs.numbers", ¬
"/Users/paul/Databases/pAccounts (Farrelle).dtBase2/Files.noindex/numbers/2/nSanta Barbara Utility Costs.numbers", ¬
"/Users/paul/Databases/pAccounts (Farrelle).dtBase2/Files.noindex/numbers/3/2025 (Kayvon).numbers", ¬
"/Users/paul/Databases/pAccounts (Farrelle).dtBase2/Files.noindex/numbers/0/Personal Bills.numbers", ¬
"/Users/paul/Databases/pAccounts (Farrelle).dtBase2/Files.noindex/numbers/2/nKinsale Running Costs.numbers", ¬
"/Users/paul/Databases/pAccounts (Farrelle).dtBase2/Files.noindex/numbers/1/Cruz del Sur.numbers"¬
}
and this script then works:
tell application "Numbers"
activate
repeat with theFile in theFileList
open (theFile as POSIX file)
end repeat
end tell
I have spaces and parentheses in the names that may be causing the problem. I don’t profess to understand the lower level workings of the file naming conventions in POSIX and HFS, but (as POSIX file) seems to handle any and all of these issues.
Thanks again @BLUEFROG and @chrillek .
1 Like
Nope. First, you have the same path name with parentheses and spaces for every file, so why would the command fail for some of them? Second, these characters are not relevant in an open command with a POSIX path. These characters only cause trouble in the shell (aka “Terminal”) or in shell scripts.
You’re not alone. With its old HFS and operating system, Apple used : as a folder separator. You’d write Macintosh HD:Applications:Safari.app to address Safari in your Applications folder. While that notation was used with HFS, it’s not an inherent part of the file system itself. And when macOS moved to a Unix foundation, they very much had to use POSIX file names (i.e. / as folder separators) because all Unix utilities relied on that.
For some reason, they decided to stay with the old format as a default in their AppleScript environment. Which, imo, is stupid, because it requires people to know about ages old conventions that are meaningless today and to use POSIX path left and right.
Note that the JXA scripting environment does not require you to convert anything to POSIX paths since that is the only format it uses. To make it more interesting, though, you must pass all file names through the Path method there before giving them to open. Programming could be easy, but where would be the fun in that?
1 Like