Get Previous/Next List Item

I have a script that generates some HTML files for a photo album. The primary navigation is a set of links underneath the picture, which go to the adjacent (prev/next) files.

At the moment, I’m using this snippet to wrap around the ends of the list (so the first and last files will link to each other). It functions correctly, but I’ve been wondering if there’s a better way to do this.

if (counter - 1) is not 0 then
			get counter - 1
		else
			get count fileList
		end if
		set prev to getWebName(item result of fileList)
		
		if (counter + 1) < ((count fileList) + 1) then
			get counter + 1
		else
			get 1
		end if
		set next to getWebName(item result of fileList)

Model: Mac mini
AppleScript: 1.10
Browser: Safari 412
Operating System: Mac OS X (10.4)

Hi guardian34,

This isn’t a “better way”, but it uses less lines:

get (counter + (count fileList) - 2) mod (count fileList) + 1
set prev to getWebName(item result of fileList)

get (counter) mod (count of fileList) + 1
set next to getWebName(item result of fileList)

Best wishes

John M

Hi,

Here’s another example with mod:

set i to 0
repeat 20 times
set i to (i mod 5) + 1
display dialog i
end repeat

Editted: sorry John, it’s the same as your next item and I missed the previous.

Thanks John M, that works nicely, and it looks much better!