Sunday, July 5, 2020

#1 2020-05-15 12:11:00 am

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

Another newbie post: why can't I call my handler?

This code is generating an error.  Can someone help me fix it?  Thanks!  :-)

Applescript:

to 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

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
   repeat with filename in filenames
       set fn to filename as string
       fn = findAndReplaceInText(fn, ".rtfd", "")
       findAndReplaceInText(fn, ".rtf", "")
       
       --set s to    
   end repeat
end tell

When I run it, I get this mysterious error

Finder got an error: Can’t continue findAndReplaceInText.



on this line:

Applescript:

findAndReplaceInText("\"The art of being wise is the art of knowing what to overlook.\".rtf", ".rtfd", "")

Help!?  :-)
Not sure how to post an actual screen shot here but I can if it will help.

Offline

 

#2 2020-05-15 01:05:41 am

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

Re: Another newbie post: why can't I call my handler?

I figured it out.  It's the old thing:

This is illegal:

Applescript:

fn = findAndReplaceInText(fn, ".rtfd", "")

Ooops!

Offline

 

#3 2020-05-15 02:33:16 am

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

Re: Another newbie post: why can't I call my handler?

Here is a version which works:

Applescript:

repeat with filename in filenames
       set fn to filename -- as string -- EDITED, the coercion is useless
       set fn to my findAndReplaceInText(fn, ".rtfd", "") -- EDITED
       my findAndReplaceInText(fn, ".rtf", "") -- EDITED
       log result -- ADDED to see the result
       --set s to    
   end repeat

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

Offline

 

#4 2020-05-15 04:24:02 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5233

Re: Another newbie post: why can't I call my handler?

wealthychef wrote:

When I run it, I get this mysterious error

Finder got an error: Can’t continue findAndReplaceInText.


Hi.

It's because you're calling the handler from within a 'tell' statement. The Finder doesn't understand the 'findAndReplaceInText' command. In such cases, you have to use 'my', as in Yvan's reply, to identify the call as belonging to the script rather than to the thing being 'told'.


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)