Sorting

I love the idea and hate the implementation of all automation technologies. They all try to make things easier, and end up making them harder. Grrr! No solution today.

2 Likes

Latest preview adds built in sorting:

2 Likes

Here’s a sketch of how sorting might be done in AppleScript (though using new Sorting command is better now that it exists):

tell application "Bike"
	tell front document
		set coverRows to my coverRows(selection rows)
		set firstCover to first item of coverRows
		set coverContainer to container row of firstCover
		set coverPreviousSibling to prev sibling row of firstCover
		
		repeat with each in reverse of my sortList(coverRows)
			if (coverPreviousSibling is missing value) then
				move each to before first row of coverContainer
			else
				move each to after coverPreviousSibling
			end if
		end repeat
		
	end tell
end tell

on coverRows(theList)
	using terms from application "Bike"
		set theCoverRows to {}
		set theCurrentLevel to null
		repeat with each in theList
			set theLevel to (level of each)
			if theCurrentLevel is null or (level of each) ≤ theCurrentLevel then
				set end of theCoverRows to each
				set theCurrentLevel to level of each
			end if
		end repeat
	end using terms from
	return theCoverRows
end coverRows

on sortList(theList)
	set theIndexList to {}
	set theSortedList to {}
	repeat (length of theList) times
		set theLowItem to null
		repeat with a from 1 to (length of theList)
			if a is not in theIndexList then
				set theCurrentItem to item a of theList
				if theLowItem is null then
					set theLowItem to theCurrentItem
					set theLowItemIndex to a
				else if name of theCurrentItem comes before name of theLowItem then
					set theLowItem to theCurrentItem
					set theLowItemIndex to a
				end if
			end if
		end repeat
		set end of theSortedList to theLowItem
		set end of theIndexList to theLowItemIndex
	end repeat
	return theSortedList
end sortList
2 Likes