Applescripts not working - syntax error

I rely heavily on Applescripts that call script handlers in my Script Libraries folders. Out of the blue, for the last week or so, several of my scripts have stopped working with this error message:

Screenshot 2024-10-02 at 9.26.27 AM

I have checked the called scripts to make sure that they work independently, and they do. I have replaced all of the scripts in the Script Libraries folders with new copies. But I still get the same message. Any ideas? Thanks.

Mac Mini 2023 M2 Pro
Sonoma 14.4.1

Some more info. This script has always worked before:

tell application "System Events" to set myPath to path to me
tell script "getScriptName" to set scriptName to getScriptName(myPath)

Now, I get this:
Screenshot 2024-10-02 at 10.25.57 AM

The script that it is calling is this:

on getScriptName(myPath)
	set scriptName to ""
	try
		tell application "System Events" to set scriptName to do shell script "rawName=" & quoted form of (get name of myPath) & "; printf '%s' \"${rawName%.*}\""
	end try
	if scriptName = "" then
		set myPath to myPath as string
		tell application "Finder"
			try
				set scriptName to name of file myPath as string
			on error
				set AppleScript's text item delimiters to ":"
				set scriptName to text item -2 of myPath as string
				if scriptName = "" then set scriptName to text item -2 of myPath as string
			end try
			set AppleScript's text item delimiters to "."
			set scriptName to text item 1 of scriptName as string
			set AppleScript's text item delimiters to ""
		end tell
	end if
	return scriptName
end getScriptName

If I try to open the “getScriptName” script in the Finder, I get this message:
Screenshot 2024-10-02 at 10.28.14 AM

Fortunately, I have text copies of all of my scripts, so I can create a new script which works fine from the Script Editor. But if I save it and try to open the new script from the Finder, I get the same error message.

Make sure Script Editor and whatever application or process that is executing the script has full disk access. After one of the errors pops up, you should go to Settings/Security privacy to see if there are any prompts.

Just a data point, but your code, installed as described, works as expected here in Sonoma 14.5 on Intel.

I am a huge fan of using script libraries. However, sometimes I need to take a step back and look at the situation and ask myself “is what I am looking to achieve made easier using a script library?”

For example, these following 3 lines of AppleScript code seems to accomplish the same thing

set myName to name of (info for (path to me))
set myNameExtension to name extension of (info for (path to me))
set scriptName to text 1 thru ((offset of myNameExtension in myName) - 2) of myName

Thanks. I will try your script.
What’s weird is that if I use my text copies of the scripts that won’t open to create new scripts, and save the new scripts as script bundles, they open without any problem.

@BabyG there are a couple of different ways to use Script Libraries. If I was using your code this is how my approach would be. (by the way I edited your library code just slightly… now when you look at the logs you will not see any errors)

Keeping the name of your library scripts different than any of the command names within those scripts, I created a script named “Get_Script_Name_Library.scpt” with the following code and saved it to the ~/Library/Script Libraries/ folder.

on getScriptName(myPath)
	set scriptName to ""
	try
		set scriptName to do shell script "rawName=" & quoted form of (get name of myPath) & "; printf '%s' \"${rawName%.*}\""
	end try
	if scriptName = "" then
		set myPath to myPath as string
		tell application "Finder"
			try
				set scriptName to name of file myPath as string
			on error
				set AppleScript's text item delimiters to ":"
				set scriptName to text item -2 of myPath as string
				if scriptName = "" then set scriptName to text item -2 of myPath as string
			end try
			set AppleScript's text item delimiters to "."
			set scriptName to text item 1 of scriptName as string
			set AppleScript's text item delimiters to ""
		end tell
	end if
	return scriptName
end getScriptName

Then, since I only use one main general library script which contains about 40 different handlers, I only need to add two lines to the top of all of my scripts, which enables me to run the code in my Library script file very easily.

use myLibrary : script "Get_Script_Name_Library"
use scripting additions

tell application "System Events" to set myPath to path to me

set scriptName to myLibrary's getScriptName(myPath)

Having done all of this, I’m not receiving any errors at all.

Thanks wch1zpink. I tried your method, but I still get the same “Some data could not be read” error on my systems (it affects my work and home computers). I am at a loss.

Hi @BabyG.

I’m afraid I’ve no idea why scripts that you’re saving can’t be opened. I’ll see if I can think of anything, but meanwhile here are a few points about your code, if you’re interested:

  • path to is a StandardAdditions command, so there’s no need to tell System Events to execute it.
  • do shell script is also a Scripting Additions command, which may be why wch1zpink’s version of your script omits the tell application “System Events” in that line. But in fact System Events (or the Finder) is needed there to cover (get name of myPath) in that line. An alternative would be to use a basename command in the shell script:
* set scriptName to (do shell script "basename " & (quoted form of POSIX path of myPath) & " | sed 's/\\.[^.]*$//'")
  • I don’t understand the point of the if scriptName = “” block, since I can’t see the Finder being any more successful than the shell script if that fails (unless the shell script line’s faulty, of course :wink:).
  • None of the as string coercions in the Finder code are necessary, since all the items except for myPath are already strings (now called ‘text’) and the Finder, like System Events, can handle aliases directly.
  • The try … on error block in that section looks as if it’s meant to force an error if myPath is a folder so that the trailing colon in the path can be dealt with appropriately. But there won’t be an error if myPath is a script because the Finder regards both files and bundles as files. In any case, even if myPath is text and refers to a folder, the name can be got with name of item myPath.
  • Notwithstanding that the try block isn’t required, the second and third lines in the on error section do exactly the same thing.

I’m circling back to this. To reiterate, almost all of my scripts for the last few months cannot be used from the Script Editor for a variety of reasons, as follows:

  1. Some don’t open at all.

  2. Some give a message that the script can’t be opened:
    Screenshot 2024-12-12 at 9.25.25 AM

  3. Some open with a blank screen

  4. Some open, but when I try to compile it I get this message:
    Screenshot 2024-12-12 at 9.23.31 AM

  5. Some open, but give a message referring to a script library in the script that it is of the wrong type

Interestingly, I can still use some of my scripts by selecting them from the AppleScript Script menu.

As I noted above, I have copies of all of my scripts saved as text files (from before the time when the problems started) - resaving the scripts from the saved text files results in the same problems. I created a new script

tell application "Finder" to activate

saved it, then tried to open it - same issue.

I have gone to great lengths to try to figure out what is going on. With my latest attempt, I wiped my hard drive, did a clean install of the operating system, updated the OS, and booted in Safe Mode with nothing attached except my mouse, keyboard, monitor, Ethernet cable and power cord. Same problems. I am desperate here. Any help would be greatly appreciated. Thanks.

Mac Mini 2023
Apple M2 Pro
Sequoia 15.1.1 (24B91)