Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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)?

share|improve this question
    
Please don't cross post :) If you don't get the desired response, request a moderator migrate it rather than reposting the question yourself. –  grgarside Jun 17 at 12:27

1 Answer 1

up vote 0 down vote accepted

This is what I use in many AppleScripts:

set iTunesFunctions to load script "Users:jim:Library:iTunes:Scripts:iTunes Functions.scpt" as alias
CaseCorrection() of iTunesFunctions

If you saved the file containing snippet1a into "MyFunctions.scpt", your code would look like:

set MyFunctions to load script "MyFunctions.scpt" as alias
snippet1a(searchText, sourceText) of MyFunctions
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.