Saturday, July 4, 2020

#1 2020-05-14 11:47:08 pm

wealthychef
Member
Registered: 2005-05-09
Posts: 19

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: 

Finder got an error: Can’t make document file "RC_applescript.scpt" of folder "RC_applescript" of folder "rcook" of folder "Users" of startup disk into type file.



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: 

Applescript:

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:   

Applescript:

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: 

get file "1TB SSD Internal:Users:rcook:RC_applescript:RC_applescript.scpt"
load script document file "RC_applescript.scpt" of folder "RC_applescript" of folder "rcook" of folder "Users" of startup disk
load script «class docf» "RC_applescript.scpt" of «class cfol» "RC_applescript" of «class cfol» "rcook" of «class cfol» "Users" of «class sdsk»


The error is this: 

Finder got an error: Can’t make document file "RC_applescript.scpt" of folder "RC_applescript" of folder "rcook" of folder "Users" of startup disk into type file.



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

Offline

 

#2 2020-05-15 12:12:45 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6395

Re: Newbie trying to make and load libraries with applescript

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

Applescript:

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

Last edited by Shane Stanley (2020-05-15 12:13:10 am)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2020-05-15 12:43:44 am

wealthychef
Member
Registered: 2005-05-09
Posts: 19

Re: Newbie trying to make and load libraries with applescript

Shane Stanley wrote:

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

Applescript:

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.

Last edited by wealthychef (2020-05-15 12:54:07 am)

Offline

 

#4 2020-05-15 12:52:47 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6395

Re: Newbie trying to make and load libraries with applescript

wealthychef wrote:

What I'm doing actually will have to do with the Finder and other applications later if I ever get simple things like this to work.



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.

Are handlers not usable inside of tell blocks?



Yes, they are.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2020-05-15 01:13:11 am

wealthychef
Member
Registered: 2005-05-09
Posts: 19

Re: Newbie trying to make and load libraries with applescript

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

«data scpt<long hex string redacted>» doesn’t understand the “findAndReplaceInText” message.



Here is the offending code in my "evernote.scpt" file. 

Applescript:

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"

Applescript:

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

Offline

 

#6 2020-05-15 01:39:11 am

wealthychef
Member
Registered: 2005-05-09
Posts: 19

Re: Newbie trying to make and load libraries with applescript

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

Applescript:

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:

Applescript:

property rclibx : 5

But I get this error:

Can’t get rclibx of «data scpt4D617259332E30300000(snip-snip)AAABAAA750747407617344420001000CFADEDEAD».



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

Offline

 

#7 2020-05-15 02:50:15 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: Newbie trying to make and load libraries with applescript

wealthychef wrote:

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

«data scpt<long hex string redacted>» doesn’t understand the “findAndReplaceInText” message.



Here is the offending code in my "evernote.scpt" file. 

Applescript:

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"

Applescript:

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



Your caller script requires at least one simple change

Applescript:

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

Offline

 

#8 2020-05-15 03:01:36 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: Newbie trying to make and load libraries with applescript

wealthychef wrote:

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

Applescript:

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:

Applescript:

property rclibx : 5

But I get this error:

Can’t get rclibx of «data scpt4D617259332E30300000(snip-snip)AAABAAA750747407617344420001000CFADEDEAD».



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



Here it I don't get such error:

Applescript:

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:

Applescript:

(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:

Applescript:

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

Last edited by Yvan Koenig (2020-05-15 03:26:52 am)

Offline

 

#9 2020-05-15 03:57:35 am

wealthychef
Member
Registered: 2005-05-09
Posts: 19

Re: Newbie trying to make and load libraries with applescript

Yvan Koenig wrote:
wealthychef wrote:

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

Applescript:

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:

Applescript:

property rclibx : 5

But I get this error:

Can’t get rclibx of «data scpt4D617259332E30300000(snip-snip)AAABAAA750747407617344420001000CFADEDEAD».



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



Here it I don't get such error:

Applescript:

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

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. 

Offline

 

#10 2020-05-15 04:03:48 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: Newbie trying to make and load libraries with applescript

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

koenigyvan <at> mac <dot> 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

Last edited by Yvan Koenig (2020-05-15 04:05:04 am)

Offline

 

#11 2020-05-15 06:14:46 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6395

Re: Newbie trying to make and load libraries with applescript

Yvan Koenig wrote:

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.



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

Like you, it works fine here.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#12 2020-05-15 07:43:14 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: Newbie trying to make and load libraries with applescript

Shane Stanley wrote:
Yvan Koenig wrote:

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.



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 :

Applescript:

(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

Offline

 

#13 2020-05-15 07:43:19 am

wealthychef
Member
Registered: 2005-05-09
Posts: 19

Re: Newbie trying to make and load libraries with applescript

Yvan Koenig wrote:

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

koenigyvan <at> mac <dot> 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



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.

Offline

 

#14 2020-05-15 07:57:29 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: Newbie trying to make and load libraries with applescript

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

Last edited by Yvan Koenig (2020-05-15 08:27:12 am)

Offline

 

#15 2020-05-15 08:27:03 am

wealthychef
Member
Registered: 2005-05-09
Posts: 19

Re: Newbie trying to make and load libraries with applescript

Yvan Koenig wrote:


You may put the “library” in the folder :

Applescript:

(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".



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:

mkdir ~/Library/"Script Libraries"/  # is this what you mean by library folder from local domain?
cp RC_applescript.scpt ~/Library/"Script Libraries"/


and tried this:

set rclib to load script file "RC_applescript.scpt"


and I got the following AppleScript Execution error:

Can’t make file "RC_applescript.scpt" into type file.



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

Yvan Koenig wrote:


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.

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



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

Offline

 

#16 2020-05-15 08:51:43 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

Re: Newbie trying to make and load libraries with applescript

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

Offline

 

#17 2020-05-15 09:23:58 am

wealthychef
Member
Registered: 2005-05-09
Posts: 19

Re: Newbie trying to make and load libraries with applescript

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":

Applescript:


property x : 5

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

Applescript:


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: 

Can’t get rclibx of «data scpt4D617259332E30300000<blah blah>FADEDEAD».

Offline

 

#18 2020-05-15 09:26:42 am

wealthychef
Member
Registered: 2005-05-09
Posts: 19

Re: Newbie trying to make and load libraries with applescript

Yvan Koenig wrote:

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



YAYA!  RESULT IS 12!  THANK YOU.   :-)  lol
I now have working technology I can build on.  Thank you all so much.  :-D
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.  :-)

Last edited by wealthychef (2020-05-15 09:28:00 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)