• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: SatImage Regular Expression OSAX error
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: SatImage Regular Expression OSAX error


  • Subject: Re: SatImage Regular Expression OSAX error
  • From: Christopher Stone <email@hidden>
  • Date: Thu, 15 Oct 2009 06:18:10 -0500

Hey Keith,

I use the Satimage.osax for nearly all my find/replace operations in Applescript.  (Note that it can do much more than find/replace.)

On Oct 15, 2009, at 05:00, email@hidden wrote:
I'm hoping that someone on this mailing list has some experience of using the Satimage Regular _expression_ OSAX

I've been trying to write an Applescript using this OSAX which looks for erroneous characters within file names eg. commas, brackets, slashes etc. using regular expressions. The problem I have with it is that when the "find text" command does not find any of the character searched for it returns an error. Here's a snippet of code
...
I have a couple of questions
1. Is this the correct behaviour for the OSAX

Yes.  I really didn't like this when I migrated from the Regular Expressions osax years ago, but I've gotten quite accustomed to the behavior.  It can some in handy at times.

2. if so, is the only way to handle said error to capture it in a try-on error block

Basically yes when you're using 'Find Text', although you can omit the 'on error' part to have it fail silently.  'Change' on the other hand will always fail silently.

3. Has anyone got a better way of doing this

I use handlers in a script library:

-- ======================================================================
on fnd(srcData, fndSpec, caseFlag, allOccurrencesFlag)
try
set fndResult to find text fndSpec ¬
in srcData case sensitive caseFlag all occurrences allOccurrencesFlag ¬
with regexp and string result
return fndResult
on error # errMsg number errNum
set fndResult to false
return fndResult
end try
end fnd
-- ======================================================================
-- You can easily make the find handler fail silently.
-- ======================================================================
on fnd(srcData, fndSpec, caseFlag, allOccurrencesFlag)
try
set fndResult to find text fndSpec ¬
in srcData case sensitive caseFlag all occurrences allOccurrencesFlag ¬
with regexp and string result
end try
end fnd
-- ======================================================================
on fndUsing(srcData, fndSpec, usingSpec, caseFlag, allOccurrencesFlag)
try
set fndResult to find text fndSpec ¬
in srcData case sensitive caseFlag all occurrences allOccurrencesFlag ¬
using usingSpec with regexp and string result
return fndResult
on error # errMsg number errNum
return false
end try
end fndUsing
-- ======================================================================
on cng(srcData, fndStr, cngStr, caseFlag)
set theResult to change fndStr into cngStr in srcData case sensitive caseFlag with regexp
end cng
-- ======================================================================

You can also do a mass change like this:

set fileName to "some-File_Name"
set newFileName to change {"-", "_"} into {" ", " "} in fileName

I use Script Debugger's built-in Text Substitution to quickly drop in handler calls, although I used to use TypeIt4Me for this.  Such utilities are indispensable in my opinion.

--
Chris
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >SatImage Regular Expression OSAX error (From: email@hidden)

  • Prev by Date: Re: So where did «class bmrk» come from?
  • Next by Date: Re: SatImage Regular Expression OSAX error
  • Previous by thread: SatImage Regular Expression OSAX error
  • Next by thread: Re: SatImage Regular Expression OSAX error
  • Index(es):
    • Date
    • Thread