• 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: multi-line records?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: multi-line records?


  • Subject: Re: multi-line records?
  • From: Christopher Stone <email@hidden>
  • Date: Sat, 25 Feb 2017 00:53:22 -0600

On Feb 24, 2017, at 16:35, Mitchell L Model <email@hidden> wrote:
I guess silly is in the mind of the observer.


Hey Mitchell,

Not at all.

[Example not shown] Reads more logically to me than a long string.

I too find long AppleScript lists and records abysmally difficult to read and maintain.

property largeRecord2 : { ¬
ThisIsTheFirstKey: "one", ¬
…
}

turns into

property largeRecord : ¬
…
¬
{ThisIsTheFirstKey:"one", ThisIsTheSecondKey:"two", ThisIsTheThirdKey:"three", ThisIsTheFourthKey:"four", ThisIsTheFifthKey:"five", ThisIsTheSixthKey:¬
"six"} ¬


Yes, this is a nasty mess that can't be worked around conventionally.  It's seriously frustrating that the AppleScript compiler won't allow the user to apply formatting as (s)he sees fit, but that's the way it is.

Regular AppleScript records are not particularly useful due to their glaring limitations, and I've assiduously avoided using them as much as possible for decades.

I tend to use nicely formatted text tables and do lookups with the Satimage.osax (very, very fast).

But - AppleScriptObjC changes the ballgame.

Shane no doubt will improve upon this at least a bit.

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/02/24 21:05
# dMod: 2017/02/25 00:24
# Appl: AppleScriptObjC
# Task: Converting a text table to a NSDictionary for lookups.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Converting, @Text, @Table, @NSDictionary, @Lookups
-------------------------------------------------------------------------------------------
use framework "Foundation"
-------------------------------------------------------------------------------------------
property nsDict : missing value -- stub for dictionary
property recordTable : "

CDG Charles Degaulle – Paris – France
DFW Dallas/Fort Worth International Airport – USA
HND Haneda - Tokyo – Japan
IAD Dulles - Washington DC – USA
LHR Heathrow – London – United Kingdom
OKC Will Rogers World Airport - Oklahoma City - USA
ORD O'Hare International Airport – Chicago – USA
PER Perth – Western Australia – Australia
SYD Sydney – New South Wales – Australia

"
-------------------------------------------------------------------------------------------

if nsDict = missing value then
   # Remove leading and trailing vertical whitespace:
   set recordTable to (its cngStr:"\\A\\s+|\\s+\\Z" intoString:"" inString:recordTable)

   

   set recNameList to paragraphs of (its cngStr:"(?m)\\t.+$" intoString:"" inString:recordTable)
   set recValueList to paragraphs of (its cngStr:"(?m)^.+\\t" intoString:"" inString:recordTable)
   set nsDict to current application's NSDictionary's dictionaryWithObjects:recValueList forKeys:recNameList
end if

set lookupValue to "OKC"

return lookupInDictionary(lookupValue)

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on cngStr:findString intoString:replaceString inString:dataString
   set anNSString to current application's NSString's stringWithString:dataString
   set dataString to (anNSString's stringByReplacingOccurrencesOfString:findString withString:replaceString ¬
      options:(current application's NSRegularExpressionSearch) range:{0, length of dataString}) as text
end cngStr:intoString:inString:
-------------------------------------------------------------------------------------------
on lookupInDictionary(recName)
   set returnResult to nsDict's objectForKey:recName
   if returnResult = missing value then
      return false
   else
      return returnResult as text
   end if
end lookupInDictionary
-------------------------------------------------------------------------------------------




My typical methodology would look similar to this (with the handler hidden in a library):




-------------------------------------------------------------------------------------------
# Requires the Satimage.osax to be installed!
-------------------------------------------------------------------------------------------
property recordTable : "

CDG Charles Degaulle – Paris – France
DFW Dallas/Fort Worth International Airport – USA
HND Haneda - Tokyo – Japan
IAD Dulles - Washington DC – USA
LHR Heathrow – London – United Kingdom
OKC Will Rogers World Airport - Oklahoma City - USA
ORD O'Hare International Airport – Chicago – USA
PER Perth – Western Australia – Australia
SYD Sydney – New South Wales – Australia

"
-------------------------------------------------------------------------------------------

set lookupValue to "OKC"

if fndUsing((lookupValue & "\\t+(.+)$"), "\\1", recordTable, false, true) of me ≠ false then
   return result
else
   error "Lookup value not found!"
end if

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on fndUsing(_find, _capture, _data, _all, strRslt)
   try
      set findResult to find text _find in _data using _capture all occurrences _all ¬
         string result strRslt with regexp without case sensitive
   on error
      false
   end try
end fndUsing
-------------------------------------------------------------------------------------------

--
Take Care,
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: 
 >multi-line records? (From: Mitchell L Model <email@hidden>)

  • Prev by Date: Re: Finder window views ?
  • Next by Date: Re: fsitem ?
  • Previous by thread: multi-line records?
  • Next by thread: terminal output from AppleScript
  • Index(es):
    • Date
    • Thread