In an AppleScript, how to call/reuse a subroutine from another AppleScript

applescript

snippet 1a

on removeText(searchText, sourceText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to searchText
set sourceText to text items of sourceText

set text item delimiters of AppleScript to ""
set sourceText to "" & sourceText
set text item delimiters of AppleScript to prevTIDs

return sourceText
end removeText

snippet 2a

on removeText(searchText, sourceText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to searchText
set sourceText to text items of sourceText

set text item delimiters of AppleScript to ""
set sourceText to "" & sourceText
set text item delimiters of AppleScript to prevTIDs

return sourceText
end removeText

set theSentence to "I love Windows and I will always love Windows."
set theSentence to removeText("Windows", theSentence)

I found this subroutine (snippet 1a) is handy in snippet 2a and want to reuse it by calling its name. I googled for howto. Then I saved snippet 1a as /Users/henry/Library/Script\ Libraries/text.scpt and in snippet 2a I replaced

snippet 1b

on removeText(searchText, sourceText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to searchText
set sourceText to text items of sourceText

set text item delimiters of AppleScript to ""
set sourceText to "" & sourceText
set text item delimiters of AppleScript to prevTIDs

return sourceText
end removeText

with

snippet 3

use script "text"

and got snippet 2b and then I ran snippet 2b, but I got an error saying «script» doesn’t understand the “removeText” message.

Reference: "use statement" (refer to the part found by searching use script "Happy Fun Ball" in https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_control_statements.html)

So I went back to google and found one suggesting I should save snippet 1a as "a script application".

Reference 2: at the bottom of https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/conceptual/ASLR_about_handlers.html

In that one's example, it's

snippet 4

tell application "NonStayOpen"
launch
stringTest("Some example text.")
end tell

so I exported snippet 1a as /Users/henry/Library/Script\ Libraries/text.app and wrote snippet 2c

snippet 2c

tell application "text"
launch
set theSentence to "I love Windows and I will always love Windows."
set theSentence to removeText("Windows", theSentence)
end tell

Then I ran it and got an error {} doesn't match the parameters {searchText,
sourceText} for removeText.

Afterwards, I have tried firstly to append removeText(searchText, sourceText) to snippet 1a (getting snippet 1c) and exported it to replace /Users/henry/Library/Script\ Libraries/text.app but got an error when running, failed;

secondly to replace removeText(searchText, sourceText) with removeText() in snippet 1a (getting snippet 1d) and exported it to replace /Users/henry/Library/Script\ Libraries/text.app but got an error when running, failed.

In snippet 2a, how do I call/reuse a subroutine (snippet 1a) from another AppleScript or "script application" (see Reference 2)?

Best Answer

Save this as text.scpt in Script Libraries folder

on removeText(searchText, sourceText)
    set {TID, text item delimiters} to {text item delimiters, searchText}
    set sourceText to text items of sourceText
    set text item delimiters of AppleScript to ""
    set sourceText to sourceText as text
    set text item delimiters to TID
    return sourceText
end removeText

Call it like this in another script:

set theSentence to "I love Windows and I will always love Windows."
tell script "text" to set theSentence to removeText("Windows", theSentence)