How to create a list of list

Hi all,

I am relatively new to applescript, just know basic stuff, need help writing a script.

I have a folder which contains jpg files, they are named pg 1 Jason.jpg, pg1 carol.jpg, pg2 john.jpg, pg2 terry.jpg, pg3 phil.jpg etc.

I want to use applescript to have all pg1 named items as the 1st list in the complete list, all pg2 items as the 2nd list in the complete etc, in the end having 1 list containing many list.


Here’s what I created below

tell application "Finder"
	activate
	set workFolder to choose folder
	set imagesFoder to folder "Images" of workFolder
	set allIamges to every item of imagesFoder whose name ends with ".jpg"
	set allRtfFiles to every item of workFolder whose name ends with ".rtf"
	set uniquePageNumber to {}
	set eachPageIamges to {}
	set eachImageForOnePage to {}
	repeat with a from 1 to (count allIamges)
		tell application "Finder"
			set captionName to name of item a of allIamges
			set spaceNumber to offset of " " in captionName
			set pageNumber to (characters 1 thru (spaceNumber - 1) of captionName as string)
			if pageNumber is not in uniquePageNumber then
				copy pageNumber to end of uniquePageNumber
			end if
		end tell
	end repeat
	
	repeat with b from 1 to (count uniquePageNumber)
		set imageName to item b of uniquePageNumber
		repeat with i from 1 to (count allIamges)
			if name of item i of allIamges starts with imageName then
				copy item i of allIamges to end of eachPageIamges
			end if
		end repeat
	end repeat
end tell

Model: MiniMac
AppleScript: 2.4
Browser: Chrome 78.0
Operating System: macOS 10.10

something like this?


set mainList to {}
set firstList to {1, 2, 3}
set secondList to {4, 5, 6, 7}

set end of mainList to firstList
set end of mainList to secondList
return mainList

–-> {{1, 2, 3}, {4, 5, 6, 7}}

Hey,

Yeah, that’s the final list I want to have.

However, here’s my example,

I have one list …

set myList to {“pg1 Jammie.jpg”, “pg1 carol.jpg”, “pg2 John.jpg”, “pg2 Phil.jpg”, “pg3 Mary.jpg”}

Final list …
myFinalList should be {{“pg1 Jammie.jpg”, “pg1 carol.jpg”}, {“pg2 John.jpg”, “pg2 Phil.jpg”}, {“pg3 Mary.jpg”}}

I should have 3 lists in one list. I want all pg1s in one list, all pg2s in their own list and all pg3s in their own list.

Hope you understand what I am trying to achieve.

Thanks
Willis

Model: MiniMac
AppleScript: 2.4
Browser: Chrome 78.0
Operating System: macOS 10.10


set myList to {"pg1 Jammie.jpg", "pg1 carol.jpg", "pg2 John.jpg", "pg2 Phil.jpg", "pg3 Mary.jpg"}

set list1 to {}
set list2 to {}
set list3 to {}
repeat with i in myList
	if i begins with "pg1 " then
		set end of list1 to contents of i
	else if i begins with "pg2 " then
		set end of list2 to contents of i
	else if i begins with "pg3 " then
		set end of list3 to contents of i
	end if	
end repeat

return {list1, list2, list3}

--> {{"pg1 Jammie.jpg", "pg1 carol.jpg"}, {"pg2 John.jpg", "pg2 Phil.jpg"}, {"pg3 Mary.jpg"}}

a couple of list routines see, e.g., here:

http://www.devedge.de/mac-os-x/applescript/grundlagen/listen-lists

This returns list of alias lists


set workFolder to choose folder

tell application "Finder"
	activate
	set imagesFoder to folder "Images" of workFolder
	set theImages to every file of imagesFoder whose name ends with ".jpg"
	set theRtfFiles to every file of workFolder whose name ends with ".rtf"
	
	set pagesNames to {}
	repeat with anImage in theImages
		set anImageName to name of anImage
		set pageName to text 1 thru 3 of anImageName
		if pageName is not in pagesNames then set end of pagesNames to pageName
	end repeat
	
	set finalList to {}
	repeat with pageName in pagesNames
		set pageList to {}
		repeat with i from 1 to (count of theImages)
			set anImage to item i of theImages
			set anImageName to name of anImage
			if anImageName begins with pageName then set end of pageList to anImage as alias
		end repeat
		set end of finalList to pageList
	end repeat
end tell

return finalList

---> {{alias "Untitled:Users:123:Desktop:WorkFolder:Images:pg1 .jpg", alias "Untitled:Users:123:Desktop:WorkFolder:Images:pg1 E.jpg", alias "Untitled:Users:123:Desktop:WorkFolder:Images:pg1.jpg"}, {alias "Untitled:Users:123:Desktop:WorkFolder:Images:pg2 40D.jpg", alias "Untitled:Users:123:Desktop:WorkFolder:Images:pg2 DIGITAL_IXUS_400.jpg", alias "Untitled:Users:123:Desktop:WorkFolder:Images:pg2 G.jpg", alias "Untitled:Users:123:Desktop:WorkFolder:Images:pg2 KK.jpg", alias "Untitled:Users:123:Desktop:WorkFolder:Images:pg2_PowerShot_S40.jpg"}, {alias "Untitled:Users:123:Desktop:WorkFolder:Images:pg3 AA.jpg", alias "Untitled:Users:123:Desktop:WorkFolder:Images:pg3 Canon_40D_photoshop_import.jpg", alias "Untitled:Users:123:Desktop:WorkFolder:Images:pg3 F.jpg"}, {alias "Untitled:Users:123:Desktop:WorkFolder:Images:pg4 D.jpg"}}

Hey KniazidisR,

Wow, this is exactly want I am looking for. Thanks a mil.
I will compare it to mine to see what I wasn’t doing right, and learn more.

Hey pjh,

Your idea works as well, although I would have to create an empty list for each page, which can be anywhere between 80 and 220 pages at times.

Thanks for your help anyway

Cheers,
Willis

Model: MiniMac
AppleScript: 2.4
Browser: Chrome 78.0
Operating System: macOS 10.10