• 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: from ASObjC Runner to Yosemite
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: from ASObjC Runner to Yosemite


  • Subject: Re: from ASObjC Runner to Yosemite
  • From: Shane Stanley <email@hidden>
  • Date: Wed, 03 Jun 2015 21:03:58 +1000

On 3 Jun 2015, at 6:28 pm, Yvan KOENIG <email@hidden> wrote:

There was no hope that I find this function ;-)

Keep in mind that you wanted to do something to an array, so you probably needed an array method

If you have time available, of course I am curious to see the enhanced versions.

This is a version of the search and replace that can optionally use regular expressions, as well as ignoring case and ignoring diacriticals.

on searchAndReplace:someText oldText:theOldText newText:theNewText usingRegex:regexFlag ignoringCase:caseFlag ignoringDiacrits:diacritsFlag -- ignore diacrits ignored for regex
-- build options
set theOptions to 0
if caseFlag then set theOptions to (current application's NSCaseInsensitiveSearch)
if regexFlag then
set theOptions to theOptions + (current application's NSRegularExpressionSearch as integer)
else -- ignoring diacriticals only applies if not doing regex
if diacritsFlag then set theOptions to theOptions + (current application's NSDiacriticInsensitiveSearch as integer)
end if
set theString to current application's NSString's stringWithString:someText
set theString to theString's stringByReplacingOccurrencesOfString:theOldText withString:theNewText options:theOptions range:{location:0, |length|:(theString's |length|())}
return theString as text
end searchAndReplace:oldText:newText:usingRegex:ignoringCase:ignoringDiacrits:


The split string one can't be done in ASObjC without a repeat, so it's a bit more complex.

on splitString:someText withString:mySeparator usingRegex:regexFlag ignoringCase:caseFlag ignoringDiacrits:diacritsFlag
-- build options
set theOptions to 0
if caseFlag then set theOptions to (current application's NSCaseInsensitiveSearch)
if regexFlag then
set theOptions to theOptions + (current application's NSRegularExpressionSearch as integer)
else -- ignoring diacriticals only applies if not doing regex
if diacritsFlag then set theOptions to theOptions + (current application's NSDiacriticInsensitiveSearch as integer)
end if
set theString to current application's NSString's stringWithString:someText
if theOptions = 0 then -- simplest case: literal, considering case and ingoring diacrits
set theList to theString's componentsSeparatedByString:mySeparator
else
set theList to {} -- to hold strings
-- get length and set some intital variables
set theFullLength to theString's |length|()
set theLength to theFullLength
set theStart to 0
repeat
-- look for range of next match
set theRange to theString's rangeOfString:mySeparator options:theOptions range:{location:theStart, |length|:theLength}
if theRange's |length|() = 0 then -- no match found, so grab the whole string then exit repeat
set end of theList to (theString's substringWithRange:{location:theStart, |length|:theLength}) as text
exit repeat
end if
-- grab the text before the match
set end of theList to (theString's substringWithRange:{location:theStart, |length|:(location of theRange) - theStart}) as text
-- reset values for next search
set theStart to current application's NSMaxRange(theRange) -- straight after the match
set theLength to theFullLength - theStart
end repeat
end if
return theList as list
end splitString:withString:usingRegex:ignoringCase:ignoringDiacrits:



-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>

 _______________________________________________
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: 
 >from ASObjC Runner to Yosemite (From: Yvan KOENIG <email@hidden>)
 >Re: from ASObjC Runner to Yosemite (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: finder column width not working
  • Next by Date: Re: from ASObjC Runner to Yosemite
  • Previous by thread: Re: from ASObjC Runner to Yosemite
  • Next by thread: Re: from ASObjC Runner to Yosemite
  • Index(es):
    • Date
    • Thread