Saturday, October 1, 2022

#1 2022-04-30 10:00:41 pm

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 87
Website

Can't open any file by telling Finder or System Events

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:

Applescript:

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:

Applescript:

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.

I found another scripter has the same trouble: https://developer.apple.com/forums/thread/703664.

I hope I'm missing something obvious but, I just can't see what is wrong.


Public servants are people too
Mac Studio M1 Max 24c, 32GB, 1TB; Studio Display; macOS Monterey, 12.3.1

Offline

 

#2 2022-05-01 12:43:58 am

wch1zpink
Member
Registered: 2011-08-20
Posts: 101

Re: Can't open any file by telling Finder or System Events

Try this

Applescript:

set open_this to (path to desktop as text) & "MyTest.txt"
tell application "Finder" to open alias open_this

OR

Applescript:

set open_this to (path to desktop as text) & "MyTest.txt"
tell application "Finder" to open file open_this

Last edited by wch1zpink (2022-05-01 12:48:41 am)

Offline

 

#3 2022-05-01 07:20:30 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1465

Re: Can't open any file by telling Finder or System Events

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:

https://forum.latenightsw.com

There is also the OP's previous thread in this forum:

https://macscripter.net/viewtopic.php?id=48941

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.

Applescript:

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

Last edited by peavine (2022-05-04 08:44:01 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#4 2022-05-01 07:38:55 pm

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 87
Website

Re: Can't open any file by telling Finder or System Events

peavine wrote:

There is also the OP's previous thread in this forum:
https://macscripter.net/viewtopic.php?id=48941

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.


Public servants are people too
Mac Studio M1 Max 24c, 32GB, 1TB; Studio Display; macOS Monterey, 12.3.1

Offline

 

#5 2022-05-01 08:50:56 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1465

Re: Can't open any file by telling Finder or System Events

Neophyte wrote:

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.



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:

Applescript:

tell application "Finder"
   open file applet_help_file
end tell

In this thread the code was:

Applescript:

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.

Neophyte wrote:

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.



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.

Applescript:

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.

Last edited by peavine (2022-05-01 09:14:06 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#6 2022-05-02 12:20:21 am

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 87
Website

Re: Can't open any file by telling Finder or System Events

peavine wrote:

.....

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:

{"Macintosh HD:Users:MyHome:Desktop:MyTest.txt
MyHome
read write
read only"}

On macOS 12.3.1, that returned this:

{"MyHD:Users:MyHome:Desktop:MyTest.txt
MyHome
read write
read only"}

Last edited by Neophyte (2022-05-02 04:08:20 am)


Public servants are people too
Mac Studio M1 Max 24c, 32GB, 1TB; Studio Display; macOS Monterey, 12.3.1

Offline

 

#7 2022-07-21 11:36:38 pm

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 87
Website

Re: Can't open any file by telling Finder or System Events

This is still a problem for me after updating to macOS 12.5.

I've added to my bug report via Feedback Assistant.  In the meantime, I'm using Peavine's ASOC code which works very well.


Public servants are people too
Mac Studio M1 Max 24c, 32GB, 1TB; Studio Display; macOS Monterey, 12.3.1

Offline

 

#8 2022-08-12 04:00:20 am

alastor933
Member
From:: Utrecht, NL
Registered: 2008-09-12
Posts: 615

Re: Can't open any file by telling Finder or System Events

This bug has now manifested itself in Catalina, after applying SecUpd 2022-005.

Offline

 

#9 2022-08-12 10:20:34 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11775
Website

Re: Can't open any file by telling Finder or System Events

Workaround:

Applescript:

tell application "Finder" to set theURL to URL of file "MyTest.txt"
open location theURL

Note that the desktop of the current user is the root folder of the Finder.


regards

Stefan

Offline

 

#10 2022-08-12 03:35:08 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1465

Re: Can't open any file by telling Finder or System Events

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. smile

Last edited by peavine (2022-08-12 03:37:11 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#11 2022-09-20 09:48:10 am

robertfern
Member
Registered: 2011-11-29
Posts: 202

Re: Can't open any file by telling Finder or System Events

I also get intermittent errors on Big Sur.

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

Applescript:


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

Applescript:

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.

Last edited by robertfern (2022-09-20 09:53:42 am)

Offline

 

#12 2022-09-20 09:58:35 am

phcranston
Member
Registered: 2006-11-16
Posts: 8

Re: Can't open any file by telling Finder or System Events

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.

Last edited by phcranston (2022-09-20 10:27:47 am)

Offline

 

#13 2022-09-20 10:51:01 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2531

Re: Can't open any file by telling Finder or System Events

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.

Applescript:


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

Last edited by KniazidisR (2022-09-20 11:18:47 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#14 2022-09-20 11:37:24 am

robertfern
Member
Registered: 2011-11-29
Posts: 202

Re: Can't open any file by telling Finder or System Events

Thanks Kniazidis,

here is my script cleaned up per your suggestions.

Applescript:

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

Last edited by robertfern (2022-09-20 11:39:16 am)

Offline

 

#15 2022-09-20 11:40:59 am

estockly
Member
Registered: 2009-01-03
Posts: 132

Re: Can't open any file by telling Finder or System Events

phcranston wrote:

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.




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.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)