In my applet, I have a small amount of code which displays a user guide which is in pdf format. The user guide is inside the applet’s resources folder. The pdf file is printed from Apple Pages.
The code has been in much the same form for 3 years. It has been in this form for 12 months:
set path_to_applet to path to me as text
set path_to_applet_alias to path_to_applet as alias
set applet_help_file to (path to resource "Help.pdf" in bundle path_to_applet_alias) as string
tell application "Finder"
open file applet_help_file
end tell
I’ve recently noticed that when running the applet in another language (French as it happens), it would produce an error:
Box].
Today, now, I get the error in English as well.
I get this error in macOS 12.3 but no other version of macOS. I have tested in macOS 10.13, 10.15 and 12.2.1 – no error.
Has anyone experienced such odd behaviour in macOS 12.3 ? Any ideas as to the cause ?
Thanks.
UPDATE: New behaviour now. Instead of showing the error nothing happens at all. SD8 runs the code but usually Preview doesn’t open and SD8 doesn’t report a result. Sometimes, Preview opens but does not open the pdf file.
After updating to 12.3 with some trepidation, the following works for me:
do shell script "open " & quoted form of POSIX path of ((path to me as text) & "Contents:Resources:help.pdf")
The help file opens infallibly in Preview, which is set as my default app for opening .pdf files.
But the following also seems to work without issue:
set path_to_applet to path to me as text
set path_to_applet_alias to path_to_applet as alias
set applet_help_file to (path to resource "help.pdf" in bundle path_to_applet_alias) as string
tell application "Finder"
open file applet_help_file
end tell
Again, the help file opens in Preview.
Both scripts were created, compiled and saved using Script Editor v 2.11. Running macOS Monterey 12.3 on Intel 64bit.
The name of the help file is case sensitive, but if I get the case wrong I get ‘missing resource’ errors rather than problems opening Preview.
Yeah but… I’ve been testing my second script (the OP’s original) repeatedly and I do sometimes find that Preview launches but the file doesn’t open. I wonder if it’s down to timing. The following may work better if you always want Preview to open the file, even if it isn’t the user’s default app for opening pdfs:
set path_to_applet to path to me as text
set path_to_applet_alias to path_to_applet as alias
set applet_help_file to (path to resource "help.pdf" in bundle path_to_applet_alias) as string
tell application "Preview"
activate
open file applet_help_file
end tell
Scroll down for the snippet – reported by the makers of Keyboard Maestro.
It is looking like a bug as it has changed for me. Started only occurring in another language then after a day or so, started happening in English. Maybe related to caching or something that happens over time ?
I’ve started making a clean macOS 12.3 VM in which to do some testing. But, probably, the solution for me will be to use a “do shell script” method in background to get around the issue.
This is becoming a pain. At first, his morning after a cold boot, I could not get the problem to surface. My current code worked. Also, this code worked:
tell application "Finder" to open applet_help_file
Now, after working properly a few times, that code opens Preview but doesn’t open the target pdf file. But, both of Fredrik71’s suggestions still work every time i.e. using “System Events” instead of Finder and calling Preview directly.
I don’t understand why the problem would be intermittent. Anyway, I guess it’s prudent to assume it will always occur and add code specifically for macOS 12.3 or just switch to System Events.
Fredrik71 has provided two working solutions, and I provide an ASObjC solution FWIW.
use framework "AppKit"
use framework "Foundation"
use scripting additions
set path_to_applet to path to me as text
set path_to_applet_alias to path_to_applet as alias
set applet_help_file to (path to resource "Help.pdf" in bundle path_to_applet_alias)
openFile(POSIX path of applet_help_file)
on openFile(thePath) -- thePath is POSIX path
set theWorkSpace to current application's NSWorkspace's sharedWorkspace()
set theURL to current application's |NSURL|'s fileURLWithPath:thePath
theWorkSpace's openURL:theURL
end openFile
BTW, if I correctly understand the dictionary entry for “path to resource”, the “in bundle” property does not appear to be necessary with the OP’s script.
set applet_help_file to (path to resource "Help.pdf")
tell application "Finder" to open applet_help_file
Hello! The last time I was logged into this forum was in May of 2007! Anyway…
I am getting the same error with Unity when Unity tries to tell another application to open a file. Be it Unity->Blender or Unity->Photoshop.
I also found someone online saying it is an error happening when Photoshop tries to tell Illustrator to open files.
And now I see here that scripts are having the same issue. I just wanted to drop in a note saying this is indeed an issue with 12.3 and goes beyond AppleScript. I just updated to 12.3 and it started.
-OwlBoy
Model: iMac Pro
Browser: Safari 605.1.15
Operating System: macOS 12