Sort Regular Groups by SKU Workaround

I see that you can sort Smart Groups by SKU by there no way to sort regular groups by SKU so I thought I’d see if there was a workaround for that.

I created a test folder of six fairytale 1970s Jordache t-shirts, each a different color.

The shirts have the following intital titles, SKUs and private comments. Two have no private comments.

Blue Vintage 1970s Jordache Limited Edition Microprinted Shirt XXL

394

(No Private Comment)

—

Vintage 1970s Jordache Limited Edition Microprinted Black Shirt XXL

425

25 x 23 x 6 @ 10 Pounds
42.90 Commercial Ground Advantage
74.65 Retail

—

1970s Jordache Magenta Limited Edition Microprinted Shirt XXL

247

(No Private Comment)

—-

Microprinted Yellow XXL 1970s Jordache Limited Edition Shirt 

46

96742 Hawaii
Ground Advantage Cubic:
1 Cubic Foot
Commercial $23.80
Retail $56.95
23.80 + 15% = 27.37
28.00

—

Jordache 1970s Limited Edition Green Microprinted Shirt XXL

372

Hawaii
$54 Retail
$32.68 Commercial

32.68 + 15% = 37.58
38.00

—

Limited Edition 1970s Jordache Microprinted Red Shirt XXL

124

Ground Advantage
Retail $60.95
Commercial $36.15
36.15 + 15 % = $41.57
$42

UPS $111.69

If you select the folder of items and run the following script it stores the listing’s title at the top of the listing’s private comment area for later retrieval. It will change the listing’s title to the SKU and pad this number with zeros so that all the titles have 10 digits so they can be properly sorted. If you use whole SKUs numbers above a billion then you may need to pad them with more zeros.

tell application "GarageSale 9.8.1"
	
	repeat with theListing in (get selected ebay listings)
		
		
		if sku of theListing is not greater than 0 then
			
			--Do Nothing
			
		else
			
			set the previousComment to private comment of theListing
			
			if previousComment is missing value then
				
				set the private comment of theListing to the title of theListing & return
				
			else
				
				set the private comment of theListing to the title of theListing & return & previousComment
				
			end if
			
			set PaddedSKU to rich text -10 thru -1 of ("0000000000" & the sku of theListing)
			set title of theListing to PaddedSKU
			
		end if
	end repeat
end tell

on TenDigits(theSKU)
	return text -10 thru -1 of ("0000000000" & theSKU)
end TenDigits

Below are how the titles look after running the above script.

The item’s titles, SKUs and private comment will now consist of the following data. You can see that the titles have been saved in the private comments section of each listing above any previously existing comments.

0000000394

394

Blue Vintage 1970s Jordache Limited Edition Microprinted Shirt XXL


—

0000000425

425

Vintage 1970s Jordache Limited Edition Microprinted Black Shirt XXL
25 x 23 x 6 @ 10 Pounds
42.90 Commercial Ground Advantage
74.65 Retail

—

0000000247

247

1970s Jordache Magenta Limited Edition Microprinted Shirt XXL


—-

0000000046

46

Microprinted Yellow XXL 1970s Jordache Limited Edition Shirt 
96742 Hawaii
Ground Advantage Cubic:
1 Cubic Foot
Commercial $23.80
Retail $56.95
23.80 + 15% = 27.37
28.00

—

0000000372

372

Hawaii
$54 Retail
$32.68 Commercial

Jordache 1970s Limited Edition Green Microprinted Shirt XXL
Ground Advantage:
Hawaii
$54 Retail
$32.68 Commercial

32.68 + 15% = 37.58
38.00

—

0000000124

124

Limited Edition 1970s Jordache Microprinted Red Shirt XXL
96742 Zone 8
Ground Advantage
Retail $60.95
Commercial $36.15
36.15 + 15 % = $41.57
$42

UPS $111.69

After you run the above script sort the folder by title via the contextual menu.

Ascending (select menu only)

Descending (select menu while pressing Option key)

The items are now sorted by SKU. We now have to retrieve the original titles from the private comment sections and place those back in the listing titles. We also have to remove the titles from the private comments.

To do that you select the folder and run the following script:

tell application "GarageSale 9.8.1"
	
	repeat with theListing in (get selected ebay listings)
		if private comment of theListing is not "" then
			
			set privatecomment to private comment of theListing
			
			set ReturnLocation to offset of (ASCII character 10) in privatecomment
			
			set getTitle to rich text 1 thru (ReturnLocation - 1) of privatecomment
			
			set the title of theListing to getTitle
			set newText to my removeString(privatecomment, getTitle & (ASCII character 10))
			set the private comment of theListing to newText
			
		end if
	end repeat
end tell

on removeString(theString, stringToRemove)
	set originalDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to stringToRemove
	set textItems to every text item of theString
	set AppleScript's text item delimiters to ""
	set modifiedString to textItems as string
	set AppleScript's text item delimiters to originalDelimiters
	return modifiedString
end removeString

Listings that do not have SKUs are ignored and the titles will not be temporary overwritten using the listing’s SKU.

When the folder is sorted listings without a SKU will appear in alphabetical order above or below those with SKUs depending on if you sort them ascending or descending.

After the titles are restored and private comments cleaned up the listings without SKUs will remain in the same position.

You can use the following Applescript beforehand to check is there are any missing SKUs.

If there are no SKUs missing then it will display the message “All Listings Have SKUs”.

If one or more SKUs are missing then it will copy those to the clipboard and display the message “Missing SKUs Added to Clipboard”.

tell application "GarageSale 9.8.1"
	set MissingSKUs to ""
	repeat with theListing in (get selected ebay listings)
		if sku of theListing is "" or sku of theListing is missing value then
			set MissingSKUs to MissingSKUs & title of theListing & (ASCII character (10))
		end if
	end repeat
end tell

if MissingSKUs is not "" then
	set the clipboard to MissingSKUs
	display dialog "Missing SKUs Added to Clipboard"
else
	display dialog "All Listings Have SKUs"
end if

Change “GarageSale 9.8.1” to whatever its named on your Mac.

I would work on duplicate folders of listings for testing.

If a large number of the items are to be processed it might make sense to hide GS while its working and make it visible again once the code is executed so that one does accidentally mess around with the interface while its working.

Can no longer edit original post.

Forgot to delete the above quoted part of code from the first first section of code as it does not actually do anything.

Have tested code on group with 350 listings and have successfully sorted them in descending or ascending order according to the SKU. The first section of code takes about 6 seconds, sorting by title takes 1-2 seconds and the last section of code to restore the titles and original private comments takes 6 seconds.

I’ve made some changes to further automate the process.

  1. I was able to figure out to script the previous manual step of selecting the contextual menu to sort the folder. In the example below a dialog window is used to choose which sort order to use; however, you can alternatively create a separate script for each sort order so that executes without any user input.
  2. When the SKU numbers temporarily replace the titles so the listings can be sorted by SKU they are padded with zeros to be 20 digits long. This is intended to work with whole number SKUs. Other characters in the SKUs, such as hyphens, may produce unwanted sort orders.
  3. The folder must be selected - not the text field inside the folder (i.e., what you click to edit the folder name).

The following AppleScript will first temporarily archive each listing’s title in the listing’s private comment area above any existing content. It will replace the title with the listing’s SKU.

It will next display a dialog window which gives you three choices:

  1. Ascending - Sorts titles in ascending order based on SKUs as titles; restores original titles; restores original private comments.
  2. Descending - Sorts titles in descending order based upon SKUs as titles, restores original titles; restores original private comments.
  3. Cancel - Does not sort; restores original titles; restores original private comments.

Some notifications have been added to inform the user of the process; however, these are not needed for the main code to run.

tell application "GarageSale 9.8.1"
	
	repeat with theListing in (get selected ebay listings)
		
		--if sku of theListing is not greater than 0 then
		
		if sku of theListing is "" or sku of theListing is missing value then
			
			--Do Nothing
			
		else
			
			set the previousComment to private comment of theListing
			
			if previousComment is "" or previousComment is missing value then
				
				set the private comment of theListing to the title of theListing & return
				
			else
				
				set the private comment of theListing to the title of theListing & return & previousComment
				
			end if
			
			set PaddedSKU to rich text -20 thru -1 of ("00000000000000000000" & the sku of theListing)
			set title of theListing to PaddedSKU
			
		end if
	end repeat
end tell

--

delay 3

--

display notification "SKUs have replaced titles, which will be restored from the private comments" with title "SKUs to Titles"

--

delay 3

--

set AnswerList to {"Ascending", "Descending"}

set theAnswer to choose from list AnswerList with title "Sort SKUs" with prompt "Sort Listings in What Order"

if theAnswer is false then --User Cancelled so Restore Titles & Private Comments
	
	tell application "GarageSale 9.8.1"
		
		repeat with theListing in (get selected ebay listings)
			if private comment of theListing is not "" then
				
				set privatecomment to private comment of theListing
				
				set ReturnLocation to offset of (ASCII character 10) in privatecomment
				
				set getTitle to rich text 1 thru (ReturnLocation - 1) of privatecomment
				
				set the title of theListing to getTitle
				set newText to my removeString(privatecomment, getTitle & (ASCII character 10))
				set the private comment of theListing to newText
				
			end if
		end repeat
	end tell
	
	delay 7
	
	display notification "Titles and private comments have been restored without sorting." with title "Titles & Private Comments Restored"
	
else
	
	if item 1 of theAnswer is "Ascending" then
		
		tell application "GarageSale 9.8.1" to activate
		
		delay 1
		
		tell application "System Events" to tell application process "GarageSale"
			
			set selectedFile to value of attribute "AXFocusedUIElement"
			tell selectedFile to perform action "AXShowMenu"
			
			try
				perform action "AXPress" of menu item "Sort By Title" of menu 1 of selectedFile
				delay 1
				
			end try
			
		end tell
		
		display notification "SKUs have been sorted in ascending order." with title "SKUs Sorted"
		
		delay 7
		
		tell application "GarageSale 9.8.1"
			
			repeat with theListing in (get selected ebay listings)
				if private comment of theListing is not "" then
					
					set privatecomment to private comment of theListing
					
					set ReturnLocation to offset of (ASCII character 10) in privatecomment
					
					set getTitle to rich text 1 thru (ReturnLocation - 1) of privatecomment
					
					set the title of theListing to getTitle
					set newText to my removeString(privatecomment, getTitle & (ASCII character 10))
					set the private comment of theListing to newText
					
				end if
			end repeat
		end tell
		
		--
		
		delay 3
		
		--
		
		display notification "Titles and private comments have been restored after sorting." with title "Titles & Private Comments Restored"
		
	else if item 1 of theAnswer is "Descending" then
		
		tell application "GarageSale 9.8.1" to activate
		
		delay 1
		
		tell application "System Events" to tell application process "GarageSale"
			
			set selectedFile to value of attribute "AXFocusedUIElement"
			tell selectedFile to perform action "AXShowMenu"
			
			try
				key down option
				perform action "AXPress" of menu item "Sort By Title" of menu 1 of selectedFile
				delay 1
				key up option
				
			on error
				key up option
			end try
			
		end tell
		
		display notification "SKUs have been sorted in descending order." with title "SKUs Sorted"
		
		delay 7
		
		tell application "GarageSale 9.8.1"
			
			repeat with theListing in (get selected ebay listings)
				if private comment of theListing is not "" then
					
					set privatecomment to private comment of theListing
					
					set ReturnLocation to offset of (ASCII character 10) in privatecomment
					
					set getTitle to rich text 1 thru (ReturnLocation - 1) of privatecomment
					
					set the title of theListing to getTitle
					set newText to my removeString(privatecomment, getTitle & (ASCII character 10))
					set the private comment of theListing to newText
					
				end if
			end repeat
		end tell
		
		--
		
		delay 3
		
		--
		
		display notification "Titles and private comments have been restored after sorting." with title "Titles & Private Comments Restored"
		
	end if
	
end if

on removeString(theString, stringToRemove)
	set originalDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to stringToRemove
	set textItems to every text item of theString
	set AppleScript's text item delimiters to ""
	set modifiedString to textItems as string
	set AppleScript's text item delimiters to originalDelimiters
	return modifiedString
end removeString

Ascending Sort:

tell application "GarageSale 9.8.1" to activate

delay 1

tell application "System Events" to tell application process "GarageSale"
	
	set selectedFile to value of attribute "AXFocusedUIElement"
	tell selectedFile to perform action "AXShowMenu"
	
	try
		perform action "AXPress" of menu item "Sort By Title" of menu 1 of selectedFile
		delay 1
		
	end try
	
end tell

Descending Sort:

tell application "System Events" to tell application process "GarageSale"
	
	set selectedFile to value of attribute "AXFocusedUIElement"
	tell selectedFile to perform action "AXShowMenu"
	
	try
		key down option
		perform action "AXPress" of menu item "Sort By Title" of menu 1 of selectedFile
		delay 1
		key up option
		
	on error
		key up option
	end try
	
end tell