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:
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.
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:
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:
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.
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.
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 ).
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.