• 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: Getting label from a record (solution)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting label from a record (solution)


  • Subject: Re: Getting label from a record (solution)
  • From: Arthur Knapp <email@hidden>
  • Date: Tue, 18 Nov 2003 13:49:43 -0500

Date: Tue, 18 Nov 2003 16:22:07 +0100
Subject: Re: Getting label from a record (solution)
From: EBI Aktivitet <email@hidden>

Sorry for posting such a long script,

Me too.... ;-)

The trick here is to realize that, beyond collecting the label, we really just need a good way to then get past the associated value:

tell application "BBEdit 6.1 for OS X"

set r to properties of document 1

end tell

r --> {id:89228, container:application "BBEdit 6.1 for OS X", ..., is FTP:false}

LabelsToString(r)
--
--> {"id", "container", ..., "is FTP"}

on LabelsToString(r)
try
kai of r
on error s --> "Can't get kai of { ... }."
end try

-- We find "{" and "}" rather than relying on the error message's
-- format so that this isn't just an U.S./English solution.
--
set x to FindTextItem(s, "{") + 1
set y to LastTextItem(s, "}") - 1

set s to s's text x thru y --> "id:89228, ... is FTP:false"

set a to {} -- collect labels as strings

set x to 1
set z to s's length

repeat until (x > z)
set x to CollectLabel(s, x, z, a) --> x is just after the ":"
set x to MovePastValue(s, x, z) --> x is just after the (";" & space)
end repeat

return a

end LabelsToString

on CollectLabel(s, x, z, a)
--
-- x is pointing directly at the first char of a label

set y to x + 1

if (s's character x = "|") then

repeat until (s's character y = "|")

if (s's character y = "\\") then -- possible escape: \|

set y to y + 2
else
set y to y + 1

end if
end repeat

set a's end to s's text x thru y
return y + 2 -- just after the ":"
else
repeat until (s's character y = ":")
set y to y + 1
end repeat
end if

set a's end to s's text x thru (y - 1)
return y + 1 -- just after the ":"

end CollectLabel

on MovePastValue(s, x, z) --> x is just after the (";" & space)
--
-- we need to handle:
-- keywords, (that may have spaces), ie: 'false', 'bounding rectangle', etc
-- "pipe" variables, ie: '| foo : bar , |'
-- strings, ie: '"Hello , World : Foo } { Bar"'
-- lists and records: we keep a count of "{" and "}" characters encountered

-- Now that I think about it, we really only need to handle being
-- inside of pipes, inside of quotes, or outside of either.
--
set stateOutside to 0
set stateInPiped to 1
set stateInQuote to 2

set stateOf to stateOutside

set braceCount to 0 -- we track our "nested" state

repeat until (x > z)

set c to s's character x
set x to x + 1 --> for the next loop

if (stateOf = stateOutside) then

if (c = "\"") then
set stateOf to stateInQuote

else if (c = "|") then
set stateOf to stateInPiped

else if (c = "{") then
set braceCount to braceCount + 1

else if (c = "}") then
set braceCount to braceCount - 1

else if (c = ",") then

-- check to see if we're at the "top-level"
--
if (braceCount = 0) then return x + 1
--
--> just past the (";" & space)

end if

else if (stateOf = stateInPiped) then

if (c = "\\") then -- skip the escape sequence

set x to x + 1

else if (c = "|") then

set stateOf to stateOutside

end if

else if (stateOf = stateInQuote) then

if (c = "\\") then -- skip the escape sequence

set x to x + 1

else if (c = "\"") then

set stateOf to stateOutside

end if

end if
end repeat

-- if we're here, then we reached the end of the string
--
return z + 1

end MovePastValue

on FindTextItem(s, ti)
set tids to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to ti as string
set i to s's text item 1's length
set AppleScript's text item delimiters to tids
on error m number n from f to t partial result p
set AppleScript's text item delimiters to tids
error m number n from f to t partial result p
end try
if (i = s's length) then
return 0
else
return i + 1
end if
end FindTextItem

on LastTextItem(s, ti)
set tids to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to ti as string
set i to s's text item -1's length
set AppleScript's text item delimiters to tids
on error m number n from f to t partial result p
set AppleScript's text item delimiters to tids
error m number n from f to t partial result p
end try
set z to s's length
if (i = z) then
return 0
else
return z - i
end if
end LastTextItem

{ Arthur J. Knapp;
<mailto:email@hidden>;

What...? Oh...!
}
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Getting label from a record (solution)
      • From: Christopher Nebel <email@hidden>
  • Prev by Date: Re: D'oh-plicate terminology
  • Next by Date: Re: Getting label from a record (part 2)
  • Previous by thread: Re: Getting label from a record (solution)
  • Next by thread: Re: Getting label from a record (solution)
  • Index(es):
    • Date
    • Thread