AppleScript for randomly subsetting Listings

I’m actively updating my GarageSale workflow so I can handle increased inventory, and I found myself writing some new scripts as helpers. It seems reasonable to share them here in case anybody finds a use for them in their own work.

This one is actually part of an experiment (or design sketch), but it turned out to be just as annoying to write as any AppleScript ever was, and it’s general-purpose enough to perhaps be useful for other folks.

-- this script for GarageSale assumes you have
-- 1. selected some listings
-- 2. want to change the state of a random proportion of those
-- 3. don't care if the total number is exact
--
-- It depends on two explicit handlers to make or "undo" the change

-- in this example, the "change" is adding or removing a red Tag 


-- this handler will make the desired change to a Listing
on makeMyChange(thisListing)
	tell application "GarageSale"
		set the tag of thisListing to 1
	end tell
end makeMyChange


-- this handler will "undo" the change
-- NOTE: be careful, since you are responsible for knowing what "undo" means here and coding it
on clearMyChange(thisListing)
	tell application "GarageSale"
		set the tag of thisListing to 0
	end tell
end clearMyChange


-- the run handler below will
-- 1. open a dialog to ask you for a percentage of the current selected Listings to change
-- 2. apply makeMyChange() to a fraction of those
-- 3. apply clearMyChange() to the other ones
-- 4. (probably) keep all selected items in the same Groups, and selected

-- NOTE: either makeMyChange() or clearMyChange() is applied to every selected Listing!
-- if you want "nothing to happen" in the second case, edit clearMyChange() to be an empty handler

on run
	set theReply to (display dialog "Percentage (as integer) of selected listings to affect?" default answer "20" buttons {"Cancel", "OK"} default button "OK")
	
	if button returned of theReply is "OK" then
		set percent to (text returned of theReply as integer)
		
		tell application "GarageSale"
			
			set selectionList to (get selected ebay listings)
			
			repeat with thisOne in selectionList
				set chance to (random number from 0 to 99)
				
				-- modify makeMyChange() and clearMyChange() so this works in the way you intend:
				if chance ≤ percent then
					my makeMyChange(thisOne)
				else
					my clearMyChange(thisOne)
				end if
			end repeat
		end tell
	end if
end run

Why is this useful?

Well, I have a habit of stopping, modifying, and re-starting a random subset of running Listings every day. There are several ways of selecting a random subset of Listings, but many of them I can think if will move them around into new Groups—and I don’t want that to happen.

So with this kind of script, I can select all my running Listings (or all of them in a particular Group), run the script to tag them, and then without moving them I can make adjustments via a Smart Group that shows me my running Listings with Tag=Red.

And then when I’m done, I can use the GarageSale Tag tool (in the toolbar) to de-tag the updated Listings.

That seems like a great script, thanks for sharing. Not only about the idea behind but because of the possibilities to learn from it how to use AppleScript.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.