title: In an AppleScript, how do I call/reuse a subroutine from another AppleScript?
body: <p>snippet 1a</p>
  
  <pre><code>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 "" &amp; sourceText
  set text item delimiters of AppleScript to prevTIDs
  
  return sourceText
  end removeText
  </code></pre>
  
  <p>snippet 2a</p>
  
  <pre><code>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 "" &amp; 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)
  </code></pre>
  
  <p>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 <code>/Users/henry/Library/Script\ Libraries/text.scpt</code> and in snippet 2a I replaced</p>
  
  <p>snippet 1b</p>
  
  <pre><code>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 "" &amp; sourceText
  set text item delimiters of AppleScript to prevTIDs
  
  return sourceText
  end removeText
  </code></pre>
  
  <p>with</p>
  
  <p>snippet 3</p>
  
  <pre><code>use script "text"
  </code></pre>
  
  <p>and got <strong>snippet 2b</strong> and then I ran snippet 2b, but I got an error saying <code>«script» doesn’t understand the “removeText” message.</code></p>
  
  <p>Reference: "use statement" (refer to the part found by searching <code>use script "Happy Fun Ball"</code> in <a href="https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_control_statements.html" rel="nofollow">https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/reference/ASLR_control_statements.html</a>)</p>
  
  <p>So I went back to google and found one suggesting I should save snippet 1a as "a script application".</p>
  
  <p>Reference 2: at the bottom of <a href="https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/conceptual/ASLR_about_handlers.html" rel="nofollow">https://developer.apple.com/library/mac/documentation/applescript/conceptual/applescriptlangguide/conceptual/ASLR_about_handlers.html</a></p>
  
  <p>In that one's example, it's</p>
  
  <p>snippet 4</p>
  
  <pre><code>tell application "NonStayOpen"
  launch
  stringTest("Some example text.")
  end tell
  </code></pre>
  
  <p>so I exported snippet 1a as <code>/Users/henry/Library/Script\ Libraries/text.app</code> and wrote snippet 2c</p>
  
  <p><strong>snippet 2c</strong></p>
  
  <pre><code>tell application "text"
  launch
  set theSentence to "I love Windows and I will always love Windows."
  set theSentence to removeText("Windows", theSentence)
  end tell
  </code></pre>
  
  <p>Then I ran it and got an error <code>{} doesn't match the parameters {searchText,
  sourceText} for removeText.</code></p>
  
  <p>Afterwards, I have tried firstly to append <code>removeText(searchText, sourceText)</code> to snippet 1a (getting <strong>snippet 1c</strong>) and exported it to replace <code>/Users/henry/Library/Script\ Libraries/text.app</code> but got an error when running, failed;</p>
  
  <p>secondly to replace <code>removeText(searchText, sourceText)</code> with <code>removeText()</code> in snippet 1a (getting <strong>snippet 1d</strong>) and exported it to replace <code>/Users/henry/Library/Script\ Libraries/text.app</code> but got an error when running, failed.</p>
  
  <p>In snippet 2a, how do I call/reuse a subroutine (snippet 1a) from another AppleScript or "script application" (see Reference 2)?</p>
  
  <p>This's a detailed question description. Please help, thank you very much!</p>