I can’t get AppleScript to tell Finder or System Events to open a file. I manually created a text file using TextEdit and saved the file to my Desktop. Then I ran some test code:
set open_this to "MyMac:Users:MyHome:Desktop:MyTest.txt"
tell application "Finder" to open file open_this
Result of that is an error: “The document “MyTest.txt” could not be opened. You don’t have permission. To view or change permissions, select the item in the Finder and choose File > Get Info”
Then I try this:
set open_this to "/Users/MyHome/Desktop/MyTest.txt"
tell application "System Events" to open file open_this
Result is: “The document “/Users/MyHome/Desktop/MyTest.txt” could not be opened. The file does not exist.”
I use Finder/System Events to open files to ensure they open in the default app for that file type. I have also made sure that my default text editor “TextEdit” has full disk access.
This is a known issue that appears to impact some but not all users running macOS 12.3 and 12.3.1. There are several threads in the Script Debugger forum on this matter:
FWIW, the following is a temporary, cut-and-paste workaround for use until this issue is fixed. It’s intended for use in AppleScripts that do not otherwise use ASObjC.
openFile("/Users/MyHome/Desktop/MyTest.txt")
on openFile(theFile) -- theFile is POSIX path
script theScript
use framework "AppKit"
use framework "Foundation"
on openTheFile(theFile)
set theWorkSpace to current application's NSWorkspace's sharedWorkspace()
set theFile to current application's |NSURL|'s fileURLWithPath:theFile
theWorkSpace's openURL:theFile
end openTheFile
end script
theScript's openTheFile(theFile)
end openFile
Thanks everyone. I’ll try these ideas. Sorry for not searching enough to find the other reports.
This is not the same behaviour as I reported in my earlier post. That behaviour seemed related to telling an app to do something in macOS 12.3. I had thought that bug was fixed in macOS 12.3.1.
The behaviour I’m reporting here is new to me and seems to have started with macOS 12.3.1. Also, it doesn’t seem to happen reliably. My applet uses “tell Finder to” quite a lot and usually it works. I’ve noticed that I don’t get the error when telling Finder to open a folder. Sometimes, switching to System Events stops the permission error. But, as in my example, not always.
I suspect there is a connection to the additional security surrounding the Desktop folder. I have tried to give Finder Full Disk Access but the problem remains.
Neophyte. The underlying issue appears to be the same with both macOS 12.3 and 12.3.1–an error is returned when a script contains the Finder’s open command. In your prior thread the specific code was:
tell application "Finder"
open file applet_help_file
end tell
In this thread the code was:
set open_this to "MyMac:Users:MyHome:Desktop:MyTest.txt"
tell application "Finder" to open file open_this
The reported errors differ significantly in the two cases, but I thought it would be helpful to be aware of both situations (which seem related to me), and that’s why I provided the link to your earlier post.
I have encountered this error with 12.3.1 when the Desktop folder is not involved, as have other users. So, it would seem unlikely that this issue is related to additional security surrounding the Desktop folder. FWIW, I just ran the following script and received the error shown.
set fileExists to "Macintosh HD:Users:Robert:Working:Test.txt" as alias --> alias "Macintosh HD:Users:Robert:Working:Test.txt"
set theFile to "Macintosh HD:Users:Robert:Working:Test.txt"
tell application "Finder" to open file theFile --> The document “Test.txt” could not be opened. You don’t have permission.
Agreed to all. I’m not sure whether to implement a workaround in my published applet or wait for macOS 12.4. If I can, I’ll do up a 12.4 beta VM and test what happens. In the meantime, I’ve lodged a bug report via Feedback Assistant.
UPDATE: I’ve managed to get a 12.4 beta VM working in Parallels. It’s the beta 3 release. Testing so far suggests no change i.e. telling Finder to open the file bangs a permission denied error. But, the file open succeeds if the file has been opened before. Testing in different folders had the same result i.e. “permission denied” error unless the file had been previously opened in Finder. This behaviour was mentioned in some other posts I saw today.
No change also with telling System Events to open the file – it bangs a file does not exist error.
I ran KniazidisR’s suggested code in the 12.4 VM and got this:
I tested Stefan’s workaround on Monterey and it works great. I also tested it with the full HFS path for files not on the desktop and that also worked well.
But if I use “System Events” instead of Finder, it works
Here is my script I’m testing to only open PDFs on my desktop
local fList, f
tell application "System Events"
set Afolder to path to desktop folder
set fList to URL of every file in Afolder whose kind is "PDF document"
repeat with f in fList
try
open location f
on error
tell me to activate
tell me to display alert (f as text)
exit repeat
end try
delay 2
end repeat
end tell
Here is the version using files instead of URLs
local fList, f
tell application "System Events"
set Afolder to path to desktop folder
set fList to every file in Afolder whose kind is "PDF document"
repeat with f in fList
try
open f
on error
tell me to activate
tell me to display alert (f as text)
exit repeat
end try
delay 1
end repeat
end tell
Also System Events is way faster at parsing with a “whose” clause than the Finder.
In macOS 12.6, I’m still getting permission errors when attempting to open files using “tell application Finder” and “open location theURL”. as @robertfern suggested I did find that “tell System Events” does work using the “open location” command.
One other note. In my testing I’ve found that if I manually open a file by double clicking on it in the Finder. The problem goes away completely for that specific file regardless of what AppleScript commands I use. Doesn’t help resolve anything but may explain why the problem appears intermittent to some users.
There are certain rules for writing Apple scripts. Although they are not as rigid as in other languages. Another language would simply send you away right away, and would not try to correct your “creations”.
The Standard Add-ons open location command does not belong to Finder or System Events. Therefore, putting it in their tell blocks is wrong without tell scripting additions keyword.
Also, after getting the list of URLs, continuing telling to them in the repeat loop no need at all. With a correctly written script, then you don’t need all these tell me, tell she, try, not try and so on.
Also, using kind property may cause additional problems for non-English system users. I recommend to use type identifier instead, which is independent from the user local.
Perhaps my suggestions won’t fix the “bug for some users” pointed out by @peavine, but a well-written script is still good practice in general.
tell application "System Events"
set fList to URL of files of documents folder whose type identifier is "com.adobe.pdf"
end tell
repeat with f in fList
open location (contents of f)
end repeat
here is my script cleaned up per your suggestions.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
local Afolder, fList, f
set Afolder to path to desktop folder
tell application "System Events"
set fList to url of every file in Afolder whose type identifier is "com.adobe.pdf"
end tell
repeat with f in fList
try
open location f
on error
activate me
display alert (f as text)
exit repeat
end try
delay 2
end repeat
Are you sure that Script Editor (or Script Debugger) also has full disk access? And if you’re saving your script as an applet, that should also get full disk access.