Using library from parent app/bundle

Can anyone suggest a way to Use Library in a .scpt/.scptd being run from an enclosing bundle/app? Ive tried putting it in either bundles script libraries but still its not found for some reason when I test.

Example: App with Use Library “Test” run script somescript.scpt/somescript.scptd with Use Library “Test”

It will only run if I put the “Test” library into ~/Library/Script Libraries not if I put in SomeApp.app/Contents/Resources/Script Libraries

I am not sure, but I think you should use script library from app’s bundle with other way:

You app (main script) should load script library into variable and then use this variable as a reference to the library. And to get location of this library into the bundle is better using (path to me) & “/Contents/Resources/Script Libraries/…”. Or, using path to resources command.

Otherwise, you have to force the user of your application to copy the built-in libraries to the computer folder ~/Library/Script Libraries.

Yeah I agree. It works well with my libraries themselves, but Shane has solved alot of the Mojave threading issues with the Dialog Toolkit Plus and Use “Dialog Toolkit Plus” has to be in all of the places I use it forcing the sub-scripts to be bundles. Sounds simple enough but it gets complicated when calling Dialogs from subroutines in my libraries.

I researched specifying location of library and couldn’t find much other then a suggestion to save as Framework and thats a learning curve I dont have alot of time for.

Any advice is appreciated!

Calling handlers is simple. Example:

set numberLibrary to (load script file "NumberLib.scpt")

tell numberLibrary
	factorial(10) -- calling one handler
	areaOfCircle from 12 -- calling other handler
end tell

A suggestion to save as Framework is the same thing: you force the user to install your Framework.

Hi.

What code are you actually using? The ‘use’ syntax for a library with the file name “Test.scpt” or “Test.scptd” is:

use myLib : script "Test" -- If you'll be calling the library's handlers.
-- Or just:
use script "Test" -- If the library defines its own terminology.

Do you mean that it won’t compile, or that you’re trying to run the library?

Sorry for the confusion. I am using both, however the issue I was having was with:

use script “Test” – the library defines its own terminology.

When I ran the code in Script Debugger, It was requiring it to be in ~/Library/Script Libraries. I had hoped saving as scpt and having in the bundle of the parent app would resolve this or that there would be a solution of indicating where the library was in the plist or through another option.

I see. I’m not experiencing any problem myself in either Script Debugger or Script Editor, although it’s a bit of a chicken-or-egg situation setting it up. You can’t compile the ‘use’ command until the library’s been added to the script bundle and there’s no bundle until you’ve compiled and saved the script!

Using this code as a simple library …

on doIt()
	say "You have successfully called me."
end doIt

… and this as a prospective script bundle or applet containing it …

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use myLib : script "My Lib"

tell myLib to doIt()

beep

… you have to save the library somewhere convenient, say your desktop, as “My Lib.scpt” and write the main script either without the ‘use myLib …’ line or with it commented out. Then compile the script and save it in the appropriate bundle or application form. Once that’s done, you can add the library to the bundle and recompile the main script with the ‘use myLib …’ line included/enabled.

Script Debugger:
As far as I can see, with Script Debugger, you have to prepare the library by putting it in a folder called “Script Libraries” in your convenient place. Then click the button whose icon looks like a cool box in SD’s tool bar. This should open a panel of gumpf which includes a pane called “Resources”. If you click the “+” icon at the bottom of this, you’ll get a choose file or folder dialog in which you navigate to and select your prepared “Script Libraries” folder. SD copies the folder and its contents to the main script’s “Resources” folder and you can now recompile the script with all the relevant lines and resave it.

Script Editor:
With Script Editor, you click the “Bundle Contents” button in the tool bar, which reveals a similar panel. You can either drag your “Script Libraries” folder into the “Resources” pane or create one there using the cog button menu and drag the library script onto its icon. Recompile with the ‘use’ line and save.

It works for me. :confused:

FYI, if you start the document in Script Debugger by using File → New Script from Template… (or the tab equivalent) and choose either an application or a script bundle (there’s a check box), the subsequent Untitled document comes with a bundle, so you don’t have to save first.

You can also control-click in the Resources area and choose Add Script Libraries Folder, then just drag your library script over the created folder.

Ah. Thanks, Shane. It shows how little I use script bundles myself. :rolleyes:

Just to add to what Shane said, in Script Editor — once you’ve saved a script as a script bundle or script application — there’s a toolbar button on the top right of the script window that shows the bundle contents (or you can type ⌘-0 to get the same thing). You can drag/drop any files or folders you like straight in.