Trouble Accessing Scripts from Script Library

Given the following located in my Script Libraries:


on forwardMail()
	tell application "Mail"
		set theMessages to the selection
		repeat with aMessage in theMessages
			set forwardMessage to forward aMessage with opening window
			tell forwardMessage
				activate
				set dateReceived to (get date received of aMessage)
				set dateReceived to ConvertDateScript's convertDate(dateReceived)
				set subject to dateReceived & space & subject of aMessage as rich text
				set sender to "<ken@lindscape.com>"
				set myRecipient to make new to recipient at end of to recipients with properties {address:"<test@lindscape.com>"}
				send forwardMessage
			end tell
		end repeat
	end tell
end forwardMail

on convertDate(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 convertDate

I can repeatedly call the convertDate handler with the following script with no problem:


use AppleScript
use scripting additions
use myLibraryScripts : script "LibraryScripts"
set todaysDate to current date
run script myLibraryScripts's convertDate(todaysDate)

But when I try to run the forwardMail handler I always get a 1708 error. Here is my script for calling the forwardMail handler. The only change is the last line. Of course I have tried many variations. All have returned the 1708 error:


use AppleScript
use scripting additions
use myLibraryScripts : script "LibraryScripts"
set todaysDate to current date
run script myLibraryScripts's forwardMail()

Here’s the replies error printout:

tell current application
current date
→ date “Wednesday, October 26, 2022 at 1:52:19 AM”
end tell
tell application “Script Editor”
forwardMail()
→ error number -1708
Result:
error “script "LibraryScripts" of «script» doesn’t understand the “forwardMail” message.” number -1708 from script “LibraryScripts”

I have searched several sites with no help. I simply cannot understand why one handler works and the other does not. Any advice will be appreciated. Thanx…

Remove run script from run script myLibraryScripts’s forwardMail(). You call the handler which is contents of the script and not the script itself.

Calling the 2nd handler is wrong as well but it seems to you it works. because the 2nd handler returns string value, and run script accepts string value as its parameter. For example:

run script “"Hello"” returns “Hello”

In your case the date handler returns “2022-10-26”

run script commands accepts is but returns mathematical expression


run script "2022-10-26" --> 1986

2022 - 10 - 26 --> 1986

I apologize for my inability to understand. In my post, I was attempting to simplify what is occurring in order to help me figure out what I am doing incorrectly. Please let me explain. Given the handler script as previously written I am trying to call both the handlers with the following code…


use AppleScript
use scripting additions
use myLibraryScript : script "LibraryScripts"
run script myLibraryScripts's forwardMail()

…where actually I intend to call the forwardMail handler and then the forwardMail handler call the convertDate handler. But no matter how I call the forwardMail handler, I get the 1708 error “«script» doesn’t understand the “forwardMail” message.” I have tried several different iterations of the last line with various quotes and with and without the “run script” as suggested. So far, I have been unable to get the script to access and run the forwardMail handler. Please advise…

This would be the code in your “LibraryScripts” file.

on convertDate(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 convertDate

on forwardMail(dateToConvert)
	tell application "Mail"
		set theMessages to the selection
		repeat with aMessage in theMessages
			set forwardMessage to forward aMessage with opening window
			tell forwardMessage
				activate
				set dateReceived to (get date received of aMessage)
				set dateReceived to dateToConvert
				set subject to dateReceived & space & subject of aMessage as rich text
				set sender to "<ken@lindscape.com>"
				set myRecipient to make new to recipient at end of to recipients with properties {address:"<test@lindscape.com>"}
				send forwardMessage
			end tell
		end repeat
	end tell
end forwardMail

These would be the commands to run, from a separate script, to run the commands in your “LibraryScripts” file.

use myLibraryScripts : script "LibraryScripts"
use scripting additions

set todaysDate to current date
set todaysDate to myLibraryScripts's convertDate(todaysDate)
myLibraryScripts's forwardMail(todaysDate)

I certainly appreciate the responses… Still I’m apparently not communicating well so I will try again. First though, I did compile the last response from scriptor wch1zpink exactly as provided with no changes. When run I still get the dreaded 1708 error message quoted here verbatim:

Which is the situation I am trying to solve.

Second, please forget about current date or todaysDate, I was using this to test. I do not need this date. I am trying to call the library subroutine forwardMail which will itself then call the library subroutine convertDate to convert the date of the selected incoming Mail message return it to the forwardMail subroutine which amends the selected messages subject line to include the converted date and sends the message, with the adjusted subject, to the recipient. Eventually I plan to send the forwardMail subroutine some varying parameters, ergo my reason for making it a subroutine.

So I don’t think I should need to send any parameters such as a date to the forwardMail subroutine and I don’t think I need to show any requested parameter in the forwardMail subroutine. The main problem is simply accessing the forwardMail subroutine. Here are some of the several ways I have tried to use this subroutine:

use myLibraryScripts : script "LibraryScripts"
use scripting additions
myLibraryScripts's forwardMail()
tell script "LibraryScripts"
	forwardMail()
end tell
use myLibraryScripts : script "LibraryScripts"
use scripting additions
tell script "LibraryScripts"
	myLibraryScripts's forwardMail()
end tell
script "LibraryScripts"'s myLibraryScripts's forwardMail()

use myLibraryScripts : script "LibraryScripts"
use scripting additions
script "LibraryScripts"'s myLibraryScripts's forwardMail()

use myLibraryScripts : script "LibraryScripts"
use scripting additions
script "myLibraryScripts"'s myLibraryScripts's forwardMail()

These are just a few of the many iterations I have tried. All have given errors mostly the same as shown quoted above. I know that I am at fault and I am doing something incorrectly. Unfortunately, most of the thousands of internet examples of scripts calling subroutines are self contained and do not show how to use subroutine libraries. I can make this work as a single self contained file. Still, I would very much like to get it written so as to use the library functionality if for nothing more than a learning experience.

Thanx… Ken

I got a different error message, and fixed it.

I put this in the library (I always save library scripts as script bundles)


my ForwardMail()--for testing, not needed 
on ForwardMail()
	tell application "Mail"
		set theMessages to the selection
		repeat with aMessage in theMessages
			set forwardMessage to forward aMessage with opening window
			tell forwardMessage
				activate
				set dateReceived to (get date received of aMessage)
				
				--set dateReceived to ConvertDateScript's convertDate(dateReceived)
				set dateReceived to my convertDate(dateReceived)
				
				set subject to dateReceived & space & subject of aMessage as rich text
				set sender to "<ken@lindscape.com>"
				set myRecipient to make new to recipient at end of to recipients with properties {address:"<test@lindscape.com>"}
				send forwardMessage
			end tell
		end repeat
	end tell
end ForwardMail

on convertDate(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 convertDate

Then I ran this script:

use scripting additions
use myLibraryScripts : script "LibraryScripts"
set todaysDate to current date
tell myLibraryScripts
	convertDate(todaysDate)
	 ForwardMail()
end tell

First time I got an error message that it didn’t understand “ConvertDateScript’s” so I edited the script library as above.

It is possible that you were triggering the same error, but getting the wrong error message. (I’ve seen and heard that happening).

Also, make sure any time you change and save a script library you recompile the script that calls it before running, even if you haven’t made any changes.

Also, no need to use Load Script/run script with script libraries any more. I prefer tell statements.

Didn’t work: Same 1708 error…

With the ForwardMail() line of code highlighted.

And this (I tried to do a screen shot couldn’t make it work here…

[center]Script Error
script “LibraryScripts” of <>
doesn’t understand thr “ForwardMail”
message.
[Button] OK[/center]

As I copied your code verbatim and compiled both parts, I can only think that it must be a version thing. I have added my version data to this text.

Just wondering if any other scriptors are having this situation or have some other suggestions.

Regardless thanx for your effort. I appreciate you!

Model: MacBook Pro (16-inch, 2019)
Browser: Firefox 102.0
Operating System: macOS 12

Oops, hit return too soon. More system info here:

Mail Version 16.0 (3696.120.41.1.1)

Model: macOS Monterey version 12.6
AppleScript: 2.8, Script Editor Version 2.11 (227)
Browser: Firefox 102.0
Operating System: macOS 12

Everything works for me. Even without use clauses.

 -- following works as is
script "LibraryScripts"'s convertDate(current date)
script "LibraryScripts "'s forwardMail()

I saved the handlers script as plain script.

Maybe, you have in the Scripts Library 2 versions of the script with same name: 1 .scpt file and 1 scptd.file. Remove the old one which fulls the AppleScript or contains errors like in the post #1 . Or, provide the extension:


script "LibraryScripts.scpt"'s convertDate(current date)
script "LibraryScripts.scpt"'s forwardMail()

Thanx, KniazidisR… Actually I copied both scripts as provided by scriptor estockly, compiled them and ran them as provided. So I am actually calling ForwardMail and I have the LibraryScript file using the term on ForwardMail.

Model: MacBook Pro (16-inch, 2019)
AppleScript: 2.8, Script Editor Version 2.11 (227)
Browser: Firefox 102.0
Operating System: macOS 12