Topics

AppleScript script unable to respond to a script statement correctly

Ilya Shebalin
 

Hello,
I wrote a script which re-arranges items in a continuous order. The items are records each  containing two properties: text which is a paragraph of the original text and a number which denotes the paragraph's position in the original text. The items are to be ordered by placing them according to the numbered parameter values so that I could easily reverse the order by simply applying reverse property of list class to get the items sorted in a continuous ascending order. The script is as follows:

set RecList to {{ParagraphText:"On 18 Jun 2018, at 3:28 pm, Ilya Shebalin <iljashebalin2@...> wrote:", ParagraphPosition:1}, {ParagraphText:"That's how the as \"class utf8\" compiles in ScriptDebugger:", ParagraphPosition:3}, {ParagraphText:"", ParagraphPosition:4}, {ParagraphText:"Type this into a Script Debugger window:", ParagraphPosition:5}, {ParagraphText:"", ParagraphPosition:6}, {ParagraphText:"current date", ParagraphPosition:7}, {ParagraphText:"Compile, then go to the View Menu and choose Show Raw (Chevron) Syntax, and compile again. What do you see now?", ParagraphPosition:9}, {ParagraphText:"", ParagraphPosition:2}, {ParagraphText:"", ParagraphPosition:8}}
set NewRecList to {}
repeat until RecList is {}
repeat with itemRef in RecList
if ParagraphPosition of contents of itemRef = (count RecList) then
"log (count RecList):"
log (count RecList)
"log ParagraphPosition of contents of itemRef = (count RecList):"
log (ParagraphPosition of contents of itemRef = (count RecList))
set end of NewRecList to contents of itemRef
set (contents of itemRef) to missing value --a problem here!
"log (contents of ItemRef):"
log (contents of itemRef)
set RecList to every record of RecList
"log RecList:"
log RecList
"log (count RecList):"
log (count RecList)
end if
end repeat
end repeat

return {List1:NewRecList, List2:RecList}

Notice the log statements. Despite the fact that all statements resolve to true and contains expected values NewRecList contains duplicates of some items leaving alone others whereas the logic does not seemingly imply that. It should assign missing value to an item and consider only records, but it fails for that particular items.  Tell me please what mistake I made.

Happy day, 
I.S.

Shane Stanley
 

On 24 Jun 2018, at 12:07 pm, Ilya Shebalin <iljashebalin2@...> wrote:

Tell me please what mistake I made.
You're redefining the value, and length, of RecList within you loop. That's a recipe for chaos.

--
Shane Stanley <sstanley@...>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>

Ilya Shebalin
 

Could I kindly ask you to elaborate a bit?
24.06.2018, в 5:19, Shane Stanley написал(а):

On 24 Jun 2018, at 12:07 pm, Ilya Shebalin <iljashebalin2@...> wrote:

Tell me please what mistake I made.
You're redefining the value, and length, of RecList within you loop. That's a recipe for chaos.

--
Shane Stanley <sstanley@...>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>




Shane Stanley
 

On 25 Jun 2018, at 12:48 am, Ilya Shebalin <iljashebalin2@...> wrote:

Could I kindly ask you to elaborate a bit?
Let's say in one loop itemRef contains a reference to item 3 of RecList. If the loop before called "set RecList to every record of RecList", and item 2 of RecList was not a record, then what was item 4 is now going to be item 3.

You can change the contents of items in a list while looping, but you generally shouldn't do anything that might disturb the indexing.

--
Shane Stanley <sstanley@...>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>