• 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: Missing ampersand
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Missing ampersand


  • Subject: Re: Missing ampersand
  • From: Yvan KOENIG <email@hidden>
  • Date: Sat, 25 Mar 2017 10:14:21 +0100


Le 25 mars 2017 à 00:33, Christopher Stone <email@hidden> a écrit :

On Mar 24, 2017, at 11:21, Jeremy Roussak <email@hidden> wrote:
Thanks. That does it.


Hey Jeremy,

Rule-of-the-thumb – never depend upon what Apple's idea of words is.

Doing so will always bite you somewhere down the line.

Always use AppleScript's text item delimiters, which you have explicit control over.

Or use a method that gives you similar explicit control.

An example using the Satimage.osax:

------------------------------------------------------------------------------

set theString to "eric v smith & jones"
set stringComponentList to find text "\\S+" in theString with regexp, all occurrences and string result

--> {"eric", "v", "smith", "&", "jones"}

------------------------------------------------------------------------------

AppleScriptObjC works quite well for this as well and returns the same result.  (This is equivalent to using AppleScript's text item delimiters.)

------------------------------------------------------------------------------

use framework "Foundation"
use scripting additions

set theString to "eric v smith & jones"

set stringComponentList to my splitstring:theString withString:" "

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on splitstring:someText withString:mySeparator -- borrowed from Yvan Koenig who borrowed it from Shane STANLEY
set theString to current application's NSString's stringWithString:someText
set theList to theString's componentsSeparatedByString:mySeparator
return theList as list
end splitstring:withString:
------------------------------------------------------------------------------

Using a regular _expression_ with AppleScriptObjC:

------------------------------------------------------------------------------

use framework "Foundation"
use scripting additions

set theString to "eric v smith & jones"

set stringComponentList to my reMatch:"\\S+" inText:theString

------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on reMatch:findPattern inText:theText
    set theNSString to current application's NSString's stringWithString:theText
    set theOptions to ((current application's NSRegularExpressionDotMatchesLineSeparators) as integer) + ((current application's NSRegularExpressionAnchorsMatchLines) as integer)
    set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:findPattern options:theOptions |error|:(missing value)
    set theFinds to theRegEx's matchesInString:theNSString options:0 range:{location:0, |length|:theNSString's |length|()}
    set theFinds to theFinds as list -- so we can loop through
    set theResult to {} -- we will add to this
    
    repeat with i from 1 to count of items of theFinds
        set theRange to (item i of theFinds)'s range()
        set end of theResult to (theNSString's substringWithRange:theRange) as string
    end repeat
    
    return theResult
    
end reMatch:inText:
------------------------------------------------------------------------------


------------------------------------------------------------------------------
Tags: @AppleScript, @Script, @ASObjC, @ccstone, @RegEx, @Match, @String, @Find
------------------------------------------------------------------------------

--
Best Regards,
Chris

The truth is that I borrowed the handler splitstring  from Shane STANLEY 😉

Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) samedi 25 mars 2017 10:13:07



 _______________________________________________
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: 
 >Missing ampersand (From: Jeremy Roussak <email@hidden>)
 >Re: Missing ampersand (From: Yvan KOENIG <email@hidden>)
 >Re: Missing ampersand (From: Jeremy Roussak <email@hidden>)
 >Re: Missing ampersand (From: Christopher Stone <email@hidden>)

  • Prev by Date: Re: Detect when I start to work...
  • Next by Date: Re: Lists to XML ?
  • Previous by thread: Re: Missing ampersand
  • Next by thread: Re: Missing ampersand
  • Index(es):
    • Date
    • Thread