OS X Sierra Applescript libraries: The variable xxxx is not defined

I’ve recently upgraded from OS X 10.9.5 to OS 10.12.6. When I did, my complex Applescripts broke. Specifically, my scripts can no longer see their libraries.

For example, one of the library files that I use is in a file called: server properties.scpt

In that script file, I have defined:


script serverPropertiesLibrary

-- server properties
property odbcUser : "bob"

end script

I access that library in this example call:

repeat with loopProjectSet in ProjectSets
updateReport(loopProjectSet, serverPropertiesLibrary’s AffiliateSets, serverPropertiesLibrary’s odbcUser, serverPropertiesLibrary’s odbcPass, serverPropertiesLibrary’s extranetusername, serverPropertiesLibrary’s extranetpassword)
end repeat

When I run my scripts, either as applications from the Finder, or inside Script Editor or Script Debugger, I get the error (pointing to the first instance of serverPropertiesLibrary’s):

Applescript Error:
The variable serverPropertiesLibrary is not defined.

I Googled the issue the best I could, and did read up on setting the environment variable to point to the /opt/local/Script Libraries and /usr/local/Script Libraries . I added that env variable to my bash profile, and I can see the ENV variable set after my reboot (just to be sure):

dev:~ cameron$ env
… snip
OSA_LIBRARY_PATH=/opt/local/Script Libraries:/usr/local/Script Libraries

… and I confirmed that the scripts are in place:

dev:~ cameron$ ls -alG “/opt/local/Script Libraries”
total 8376
drwxr-xr-x 10 cameron wheel 340 7 Sep 21:43 .
drwxr-xr-x 16 cameron wheel 544 7 Sep 21:43 …
-rw-r–r–@ 1 cameron wheel 6148 7 Sep 21:43 .DS_Store
-rw-r–r–@ 1 cameron wheel 1825928 5 Apr 2015 Script Library Aliases
-rw-r–r–@ 1 cameron wheel 5510 21 Apr 2016 server properties.scpt
-rw-r–r–@ 1 cameron wheel 92514 6 Apr 2015 subroutines-excel.scpt
-rw-r–r–@ 1 cameron wheel 32562 15 Aug 2014 subroutines-mail.scpt
-rw-r–r–@ 1 cameron wheel 19420 23 Jul 2013 subroutines-progressbar.scpt
-rw-r–r–@ 1 cameron wheel 121624 4 Jan 2015 subroutines-status report cloner.scpt
-rw-r–r–@ 1 cameron wheel 92360 6 Apr 2015 subroutines.scpt

Still, my script won’t run, same error. Is that bash env variable just not accessible to my logged in GUI user account? Do I need to migrate all my libraries into the script’s bundle (completely defeating the purpose of libraries)?

Sorry to bother the group is this has been covered in the past, I just can’t wrap my head around this silly issue.

Thank you for your help.

No, but it’s not clear whether you are calling them correctly – you’re certainly putting them in an atypical directory.

Are you including “use script” statements? If not, setting OSA_LIBRARY_PATH will do nothing.

The way to use a library is:

  • Store it in the library search path. ~/Libraries/Script Libraries is the typical place.

  • Include a “use script statement”, such as:

use myLib : script "server properties"
use scripting additions -- if you also intend to use scripting addition commands

set x to myLib's someHandler()

It might be easiest to start by reading the section on Script Libraries in the AppleScript Language Guide:
https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide

huge thanks for the help! I mean huge! actually, I’ve been scripting for over 20 years, it’s just that Apple screwed everything with their upgrades. I used to manage my libraries in Script Debugger; it was SD that moved my libraries there in the first place. I also read up that those 2 locations are quite common for libs as well. but, your suggestion worked, so I’ll stick with you!

I didn’t get that far. I can compile the script without complaint, but now it won’t run:

// main script:
use serverPropertiesLibrary : script “server properties.scpt”
use scripting additions

updateReport(loopProjectSet, serverPropertiesLibrary’s AffiliateSets, serverPropertiesLibrary’s odbcUser, serverPropertiesLibrary’s odbcPass, serverPropertiesLibrary’s extranetusername, serverPropertiesLibrary’s extranetpassword)

// server properties.scpt:

script serverPropertiesLibrary

property AffiliateSets : {¬
	{"igods", "http", "extranet.igods.com", "stats.igods.com", "project", "iGods", null} ¬
		}

end script

// result:
AppleScript Execution Error
Can’t make AffiliateSets into type reference.

You don’t need to make it a script object. Change it to simply:

property AffiliateSets : {¬
	{"igods", "http", "extranet.igods.com", "stats.igods.com", "project", "iGods", null} ¬
		}

(Please wrap your code in AppleScript tags using the button at the top of the message view.)

I can’t get this to work.

now I can’t access functions in my library files, e.g.:

– main script:


use script "subroutines.scpt"
-- snip
set targetProjectNameEncoded to my replace_chars(targetProjectName, " ", "%20")

– library script:


on replace_chars(txt, srch, repl)
	copy AppleScript's text item delimiters to theDelims
	set AppleScript's text item delimiters to the srch
	set the item_list to every text item of txt
	set AppleScript's text item delimiters to the repl
	set txt to the item_list as string
	set AppleScript's text item delimiters to theDelims
	return txt
end replace_chars

– result:
«script» doesn’t understand the “replace_chars” message.

“load script file” no longer seems to be the way to load scripts, either, I can’t even compile using that command (even though the documentation seems to focus on that method).

I’ve tried reading up on AppleScript libraries, but there doesn’t seem to be much about them, certainly nothing about the “use” command… it all points to “load script” which doesn’t work for me.

I knew I was going to be in trouble when I upgraded my OS, but this is beyond tedious.

Hi.

use subroutines : script "subroutines.scpt" -- The word after 'use' is a variable. You can call it whatever you like.

set targetProjecNameEncoded to subroutines's replace_chars(targetProjectName, " ", "%20")

ugh! I am going to have to rewrite so** much code. and I have 6 libraries that I use, so I’ll have to figure out which library to reference in place of “my”.

I completely regret upgrading my operating system. Script Debugger used to make this so easy. Is there even a way to analyze a script to ensure its library dependencies are intact? Or do I have to run absolutely every possible code branch to test it?

this is a nightmare. thank you all so much for your help in getting here, I couldn’t manage AppleScript without your support.

If you open your old scripts with Script Debugger 6, it will offer to migrate them. It won’t do everything for you, but it will save you a lot of work.

I was never able to get OSA_LIBRARY_PATH to work with more than one location in El Capitan, no matter what the examples show. It did work with one location specified, until Sierra.

Now, despite being able to see the OSA_LIBRARY_PATH variable is properly defined and using the “use” syntax for the libraries, Applescript can no longer find my libraries.

This is extraordinarily annoying because I need to keep libraries with the software so I can maintain revision control.