0

Is there a way to load an applescript library based on a variable. What I try to achieve is this:

set basescript to "hello.scpt"

tell script basescript
    dialoger("testing")
end tell

the basescript will contain something like:

on dialoger(message)
    display dialog message
end dialoger

This works fine a long as I type it out but as soon I try to pass it like a var it keeps giving errors... Any help would be greatly appreciated

wch1zpink
  • 3,026
  • 1
  • 8
  • 19
  • Does this answer your question? [Import AppleScript methods in another AppleScript?](https://stackoverflow.com/questions/2606136/import-applescript-methods-in-another-applescript) – user3439894 Sep 02 '20 at 21:40
  • No unfortunately not. These methods all work fine but none of them seem to be working when this script name is being presented in the form of a variable as show above. – RNE CGI Sep 02 '20 at 22:02
  • If you do `set basescript to load script POSIX file "/path/to/Hello.scpt"` then `tell basescript to dialoger("testing")` will work! – user3439894 Sep 02 '20 at 22:23
  • Your solution works as suggested! Thanx – RNE CGI Sep 03 '20 at 11:28

3 Answers3

0

I use script libraries all the time. Once you get the hang of it, it becomes a huge timesaver. There are a couple of ways of loading script commands from a “Library Script” into another script.

One way is to use the load script command by setting a variable name to load script and path/to/script.

There is also another way, which in my opinion, is much more powerful. You can import library scripts using the use statement. This method removes the need of using tell statements.

For example, I saved your following code as “Hello.scpt” in my /Users/YOUR SHORT NAME/Library/Script Libraries/ folder.

on dialoger(message)
    display dialog message
end dialoger

Next, in the script in which I want to load the commands from the Library script “Hello.scpt”, this is the code I used using the use statement

use basescript : script "Hello"
use scripting additions

basescript's dialoger("testing")

By using use statements with multiple applications, you can combine terms from different sources in ways impossible using standard tell statements or tell blocks, because the tell construct only makes one terminology source available at a time.

wch1zpink
  • 3,026
  • 1
  • 8
  • 19
0

Solution:

If you do set basescript to load script POSIX file "/path/to/Hello.scpt" then tell basescript to dialoger("testing") will work!

0

Assuming that these are script libraries, you can accomplish what you want using a handler like so:

-- send the same of the script library in the first parameter
-- and the message in the second

myHandler("hello", "My Message")

on myHandler(libName, message)
    tell script libName
        dialoger(message)
    end tell
end test

Since the handler isn't processed until runtime, it will dynamically implement the correct script library passed in libName.

Ted Wrigley
  • 2,921
  • 2
  • 7
  • 17