Newbie trying to make and load libraries with applescript

Hello, I’m just starting out with AS and am gettring this error. My question is why? Thanks for any help in advance!
The error is this:

Background: Ihave the following code in a script in a file /Users/rcook/RC_applescript/RC_applescript.scpt
It contains some applescript that I am trying to load in another script:

to logit(log_string, log_file)
	do shell script ¬
		"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
		"\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit

But when I put this in my other script, it gives me an error:

tell application "Finder"
	--set rclib to load script alias "RC_applescript:RC_applescript.scpt" of home
	set rclibs to (home as string) & "RC_applescript:RC_applescript.scpt"
	set libalias to file rclibs
	set rclib to load script libalias -- doesn't work yet... why?
	
end tell

The events in the debugger are:

The error is this:

Model: iMac (Retina 4K, 21.5-inch, 2017)
AppleScript: LateNight Script debugger and Catalina
Browser: Safari 537.36
Operating System: macOS 10.14

Start by removing the Finder tell block – what you’re doing has nothing to do with the Finder.

set rclibs to (path to home folder as text) & "RC_applescript:RC_applescript.scpt"
set libalias to rclibs as alias
set rclib to load script libalias 

Aha, that seems to work now, thanks! I haven’t tried calling any functions yet but the code compiles now.

Add the tell blocks at that stage, and try to stick to just the code relevant to the apps inside them. There can be subtle (and not so subtle) issues if you don’t.

Yes, they are.

Thanks! I’m making progress. I have a new error, I hope you can comment, thanks.
The error is

Here is the offending code in my “evernote.scpt” file.

set rclibs to (path to home folder as string) & "RC_applescript:RC_applescript.scpt"
set libalias to alias rclibs
set rclib to load script libalias

tell application "Finder"
	set fold to folder "current_projects:macjournal_export:subset" of home
	set filenames to the name of every document file of fold
	set filedates to the creation date of every document file of fold -- set filenames to the name of every document file of files
end tell

repeat with fn in filenames
	copy fn to fnc
	tell rclib
		set fn to findAndReplaceInText(fn, ".rtf", "")
	end tell
	fn
end repeat

Here is the code in “RC_applescript:RC_applescript.scpt”

on logit(log_string, log_file)
	do shell script ¬
		"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
		"\" >> $HOME/Library/Logs/" & log_file & ".log"
end logit

on findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AppleScript's text item delimiters to theSearchString
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to theReplacementString
	set theText to theTextItems as string
	set AppleScript's text item delimiters to ""
	return theText
end findAndReplaceInText

Here is a simple reproducer.
In “evernote.scpt” I have this:

set rclibs to (path to home folder as string) & "RC_applescript:RC_applescript.scpt"
set libalias to alias rclibs
set rclib to load script libalias

tell rclib
	set its rclibx to (its rclibx) + 7
end tell


And in “RC_applescript:RC_applescript.scpt” I have this:

property rclibx : 5

But I get this error:

I don’t get it. I declared rclibx as a property, so why is it not found in the library which successfully loaded?

Your caller script requires at least one simple change

repeat with fn in filenames
	copy fn to fnc -- what need for that ?
	tell rclib
		set fn to its findAndReplaceInText(fn, ".rtf", "") -- REQUIRED EDITION
	end tell
	log fn -- EDITED for Script Editor
end repeat

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 15 mai 2020 10:50:07

Here it I don’t get such error:

set rclibs to (path to home folder as string) & "RC_applescript:RC_applescript.scpt"
set libalias to alias rclibs
set rclib to load script libalias

tell rclib
	set its rclibx to (its rclibx) + 7
	its rclibx --> 12
end tell

What puzzle me, but maybe I misunderstand something,
after running it once I assumed that running the caller a second time I would get the result : 19.
In practice I get 12.

I wish to add that Apple defined carefully locations for script libraries:

(path to library folder from local domain as string) & "Script Libraries:"
log result (*SSD 1000:Library:Script Libraries:*)
(path to library folder from user domain as string) & "Script Libraries:"
log result (*SSD 1000:Users:**********:Library:Script Libraries:*)

Of course, we must create the final subfolder “Script Libraries” by ourself.
Using such location is, at least, good practice but it also allow us to use a simpler caller:

use scripting additions
use rclib : script "RC_applescript"

tell rclib
	set its rclibx to (its rclibx) + 7
	its rclibx --> 12
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 15 mai 2020 11:00:54


The question is for identical code why do you get this error and I do not? I use LateNight Software’s Script Debugger. And if I test with Script Editor and it gives the same error.

May you zip you two files and send them attached to a mail addressed to

koenigyvan mac com ?

I would be able to test in the two editors.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 15 mai 2020 12:03:42

That’s because you’re reloading it anew from the file each time.

Like you, it works fine here.

Thank you Shane

@wealthychef
You may put the “library” in the folder :

(path to library folder from local domain as string) & "Script Libraries:"

If you get a correct behavior in such configuration we would know that the problem is related to the original location, maybe a problem of permissions of the folder “RC_applescript”.

You may create a new account and try the couple of files in this account.
If you get a correct behavior in such configuration, we would know that the problem is related to the original account.
If it’s that, reboot in Safe Mode.
In French it’s described as
Démarrer en mode sans échec (seuls les éléments essentiels de macOS sont lancés ; certaines fonctionnalités et apps risquent de ne pas fonctionner correctement)
Maintenez la touche Maj enfoncée immédiatement après le démarrage de votre Mac (certains ordinateurs Mac émettent une tonalité de démarrage). Relâchez la touche Maj lorsque l’indicateur de progression apparaît.

Often, doing that reset the user’s account in a clean state.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 15 mai 2020 15:43:00

Thanks, yes, I just sent you the two files, please test!
I don’t think it will fail for you either, but maybe there is some glaring typo or something going on.

I received your files.
With your library file I got the same odd behavior.
I created a new blank file, copied the contents of your original script, matter os safe I pasted in a blank TextEdit text document to clean possible garbage, copied the contents of this doc and pasted it in the new blank script.
I save it with the name “RC_applescript.scpt” and Bingo, the new script behave flawlessly.
Try to apply the same scheme on your side.

Here I will try to find what is wrong in the original.
It may be long because this one contain several paragraphs of comments which make it longer than what you posted.

In fact the original file is awfully garbled. I really don’t understand how it has transformed this nway. Even the resource fork is garbled.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 15 mai 2020 15:57:22

Sorry about this weird pathname thing, I use git repos to organize all my code… I’ll have to make my Script Libraries folder into a repo it sounds like.
Anyhow, I made the requisite directory this in Terminal:

and tried this:

and I got the following AppleScript Execution error:

I shouldn’t have to specify the path right? Just put the file in ~/Library/Script\ Libraries and then gave its name. Should work but won’t load

Ah yes, good idea, switch user accounts. I also will reboot. BRB… must post this before I reboot. :slight_smile:

BINGO.

You just save your script while it was in Debug mode in Script Debugger.
Reopen it in Script Debugger, disable the Debug mode and resave.

You will retrieve a clean script.

I was writing a mail for Shane Stanley when I thought to such scheme.
Before sending it, I remember messages in Late Night weekly summary from users describing odd behaviors whose cause was such a wrong action.
So, I opened the clean script in SD, activated Debug mode and saved.
For sure I got a clone of your “clobbered file”.
I’m surprised that you didn’t took care to the beetle appearing at the right bottom of your script icon. It doesn’t appear on the file that I received.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 15 mai 2020 16:51:36

OK I tried this on a bran new test account with nothing running and nothing installed, right after a fresh reboot, and it still did not work! I’m scratching my head.

In “/Users/test/Downloads/from_rcook/RC_Applescript.scpt”:


property x : 5

In “/Users/test/Downloads/from_rcook/Test Library Load.scpt”:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- THIS DOES NOT WORK YET.  WHY?  
set rclib to load script file ((path to home folder as string) & "Downloads:from_rcook:RC_applescript.scpt")
tell rclib
	set its rclibx to (its rclibx) + 7
end tell

I still get the same weird error:

YAYA! RESULT IS 12! THANK YOU. :slight_smile: :lol:
I now have working technology I can build on. Thank you all so much. :smiley:
Side note with respect to your surprise: I had no idea that saving in debug mode would be different than any other mode. I guess I still don’t know what an appescript really really is deep in its heart. :slight_smile: