Script Library Problem

My scripts cannot find handler scripts located in the Script Libraries folder. Given the following handler placed in the ~/Library/Script Libraries folder titled My Library:

on formatDate(dateToConvert)
	set {year:y, month:m, day:d} to dateToConvert
	tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8
end formatDate

Every attempt to call this handler fails with the 1708 error “«script» doesn’t understand the “formatDate” message.” "

Here are some of my scripts calling this handler:

use AppleScript version "2.8"
use scripting additions
set todaysDate to current date
use myLib : script "My Library"
myLib's formatDate(todaysDate)
use AppleScript version "2.8"
use scripting additions --With or without this line
set todaysDate to current date
tell script "My Library"
	formatDate(todaysDate)
end tell
use AppleScript version "2.8"
use scripting additions --With or without this line
set todaysDate to current date
script "My Library"'s formatDate(todaysDate)

…and many more. I am a bit new to Applescript and I have read dozens of on line pages and two books. I cannot figure out what I am doing wrong with this code. Some hwelp would be appreciated. Paying ahead: Thanx!

First thing I’d do is change the name of that handler and try again.

Thanx Shane… Alas, been there, done that…

Any time you change your library script you need to recompile the script calling it.

Sometimes you need to quit and relaunch Script Debugger (that’s the case with library scripts with dictionaries, at least).

I understand Ed. However, I have re-compiled many times, forced quit on the script editor many times, moved the library file to the system library script libraries folder, even upgraded MacOS to version 13 Ventura. This situation has been plaguing me for weeks. Nothing I do seems to help.

What happens if you put all you use statements together? I’m not sure it’s a good idea to mingle other code in there, like all your examples do.

Sorry Shane… I’ve been there too.

Unless you mean something other than this:

use AppleScript version "2.8"
use scripting additions
use myLib : script "My Library"
set todaysDate to current date
myLib's formatDate(todaysDate)

I appreciate the help. So I am thinking perhaps if I provide the full reply, perhaps it might help. Anyway here is what I get when I run this above script:

tell current application
	current date
		--> date "Wednesday, November 2, 2022 at 9:36:22 PM"
end tell
tell application "Script Editor"
	formatDate(date "Wednesday, November 2, 2022 at 9:36:22 PM")
		--> error number -1708
Result:
error "script \"My Library\" of «script» doesn’t understand the “formatDate” message." number -1708 from script "My Library"

Perhaps this will help… I hope so!

That script \"My Library\" of «script» is strange. Can you post the library’s code?

Of course… It is posted in my first text of this conversation. Here it is again:

on formatDate(dateToConvert)
	set {year:y, month:m, day:d} to dateToConvert
	tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8
end formatDate

That’s the handler, but I’m wondering about the full context. Is there anything else in the library?

I tested your scripts… Monterey 12.6.1

I made a script bundle library of this:

on formatDate(dateToConvert)
	set {year:y, month:m, day:d} to dateToConvert
	tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8
end formatDate

Next I run this…

use scripting additions

use theLib : script "My Library"

set todaysDate to current date
theLib's formatDate(todaysDate)

The result was: “2022-11-04”

Something that happen to me more and twice is the AppleEvents could hang or something simulare. The solution is: to reboot the computer

1 Like

FYI, I just saved the following script in my Library folder as “My Library.scptd”…

/Users/edstockly/Library/Script Libraries/My Library.scptd

use scripting additions
on formatDate(dateToConvert)
   set {year:y, month:m, day:d} to dateToConvert
   tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8
end formatDate

And called it with this script:

use scripting additions
use myLib : script "My Library"
set todaysDate to current date
myLib's formatDate(todaysDate)

And it worked.

My result was 2022-11-03 (I’m in California)

Shane… Concerning my library and your question. Initially I had an additional script in the same file (My Library). I removed it in my attempts to get it working. Right now this script is the only script in the My Library file.

Concerning other files that are in the ~Library/Script Libraries, yes there are some other files. In fact, I placed the calling script in that directory trying to get it to work. Most of these files have been added during my effort to get this working. This directory was empty when I started, in fact I just recently, last month, created the directory following instructions concerning library creation. However, in view of your comment, I moved all of the files to my user directory except for the one My Library script. After that, running the calling script produced the same result.

Ed… Yipee! That worked. Thanx ever so much.

I must admit, I was saving the scripts as .scpt files. I had no idea that they needed to be saved as .scptd. In fact, I was under the impression that the handler scripts saved as bundles had to have dictionaries with them. That is why I never tried .scptd.

I have read and re-read many sites searching for this solution. Is there some place the directs that handlers “have to” be saved as bundles? If so, I missed it and I would like to go back and read that particular site.

Regardless, thanx!

1 Like

Libraries with dictionaries must be saved as bundles. I have found that it seems to work better on ordinary libraries, but no, I haven’t found that documented anywhere. In fact I have found sample libaries using the .scpt format.

I only use .scptd format for libraries. (I’m guessing Shane does too?)

1 Like

That said, I saved your libary script with a and a .scpt file and that also worked. So go figure?

…interesting… and LOL, for now at least!

As far as I’m aware, .scptd is only required when using a dictionary. I’ve used .scpt files, and I believe @alldritt’s MarksLib is a .scpt file.

1 Like