Run AppleScript from AppleScript

I did the site search on the subject and found only one hit which did not answer my question, so…

How do I pass parameters when calling a subroutine handler that is in another script file?

Given the following:

Script one:


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 my convertDate(dateReceived)
                     (*
                        I tried these with some variations:
                        Tell script "ConvertDate" to set dateReceived to convertDate(dateReceived)
                        Tell script file "ConvertDate" to set dateReceived to convertDate(dateReceived)
                        Tell application "ConvertDate" to set dateReceived to convertDate(dateReceived)
                     *)
			set datedSubject to dateReceived & space & subject of aMessage
			set subject to datedSubject as rich text
			set sender to "<ken@gmail.com>"
			set myRecipient to make new to recipient at end of to recipients with properties {address:"<test@gmail.com>"}
			send forwardMessage
		end tell
	end repeat
end tell

and script two:


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

This script functions properly when self contained in a single file. But I would like to have it as two separate files. Problem is I haven’t found a way to pass the variable data (dateReceived). I have tried with script two saved as a script file and also as an application file. I also tried with script two formatted as a tell script. Is it possible to pass variables (or global variables) to a separate AppleScript file? Google search wasn’t much help.

I wrote an ebook about AppleScript separation and re-union.

https://piyomarusoft.booth.pm/items/4234582

@maro: Is your book available in english?

Exists several ways to make 2 scripts to communicate.

Here is how to do it using load script command:


-- The calling script

set secondScriptAlias to alias "Apple HD:Users:123:Desktop:convertDate .scpt" -- something like THIS
set secondScript to load script secondScriptAlias -- THIS

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 secondScript's convertDate(dateReceived) -- THIS
			set datedSubject to dateReceived & space & subject of aMessage
			set subject to datedSubject as rich text
			set sender to "<ken@gmail.com>"
			set myRecipient to make new to recipient at end of to recipients with properties {address:"<test@gmail.com>"}
			send forwardMessage
		end tell
	end repeat
end tell


-- The invoked script saved in (alias "Apple HD:Users:123:Desktop:convertDate .scpt")

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

.
.
Other approach - using command run script with parameters example:


-- The calling script
set dateReceived to run script alias "Apple HD:Users:123:Desktop:convertDate .scpt" with parameters {current date}

-- The invoked script saved in (alias "Apple HD:Users:123:Desktop:convertDate .scpt")

on run (args)
	convertDate(item 1 of args)
end run

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

The process you are describing is using your second script as a Script Library. You can save an AppleScript file with a bunch of different Handler Definitions and save it to your ~/Library/Script Libraries folder.

Once a script is saved to that location, you can use that AppleScript file as a Script Library that it’s commands can be called on by any other script.

For example. I saved the code from your second script and named it “Convert_Date_Library.scpt” to this folder… ~/Library/Script Libraries/Convert_Date_Library.scpt.

The use command is the preferred method for calling on Script Libraries. In this example I assigned variable name dateLibraryScript to it.

This works for me on my machine.

use dateLibraryScript : script "Convert_Date_Library"
use scripting additions

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 dateLibraryScript's convertDate(dateReceived)
			----------
			set subject to dateReceived & space & subject of aMessage as rich text
			set sender to "<ken@gmail.com>"
			set myRecipient to make new to recipient at end of to recipients ¬
				with properties {address:"<test@gmail.com>"}
			send forwardMessage
		end tell
	end repeat
end tell

Here is some reading about using use statements in AppleScript.
https://macosxautomation.com/mavericks/usestatement/index.html

WOW!

My thanx to all, this is great information.

Since I have (or will have) a number of subroutine handler scripts I believe I will try to create the script library as mentioned…

Oh well, back to work.

Again thanx!

One note: I believe Library scripts need to be save as script bundles, and that’ with the .scptd extension. (EDIT: That is required if you’re going to add a dictionary)