简体   繁体   中英

In an AppleScript, how do I call/reuse a subroutine from another 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)?

This's a detailed question description. Please help, thank you very much!

I've written quite an extensive tutorial on MacScripter about script libraries. The correct syntax to use is:

use theScriptLibrary : script "text"

theScriptLibrary's removeText("hellow", "w")

You need to address the handler to a specific script object, unless you use a scripting definition file. When you're not using a scripting definition file the handler will be called to the current top level script object and there is no removeText() handler in the parenting chain. This is indeed not discussed in the AppleScript Language Guide.

Here's my tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM