I was texting Tara a lot prior to us starting to date, and I noticed that her first and last name had the same number of characters. Then I noticed that my first name, Joseph, and last name, Puccio, also have the same number of characters. I wanted to point this out to her but I didn't want her to know I had been thinking that much about her name, so I decided to kind of burry the insight in noise, by making this Facebook post where I wrote some AppleScript that went through all Facebook friends and found the ones whose name matched this criterion and tagged them in the post.

read more
on friendExtractionGivenan(id, type)
	
	
	if type is equal to "indirect" then
		tell application "Safari"
			set doc to front document
			set done_loading to false
			do JavaScript "
		window.location.assign('https://www.facebook.com/" & id & "?sk=friends&v=friends');
		" in doc
			delay 2
		end tell
	end if
	
	if type is equal to "direct" then
		tell application "Safari"
			set doc to front document
			set done_loading to false
			do JavaScript "
		window.location.assign('https://www.facebook.com/profile.php?id=" & id & "&sk=friends&v=friends');
		" in doc
			delay 2
		end tell
	end if
	
	
	
	
	tell application "Safari"
		repeat while done_loading is not equal to true
			set doc to front document
			set done_loading to do JavaScript "
			var done = false;
			 if (document.readyState=='complete'){
			 	done=true;
			}
			
			done;
				" in doc
			delay 1
		end repeat
	end tell
	
	
	
	tell application "Safari"
		set doc to front document
		set this_url to URL of doc
		set done_scrolling to false
		set filtered_URLs to {}
		set completed_once to false
		set erred_once to false
		
		
		repeat while done_scrolling is not equal to true
			try
				set done_scrolling to do JavaScript "
		
		var completed_once = " & completed_once & ";
		if(completed_once!=true){
		var done;
		var number_of_trys =0;
		}
    function scrollToBottom(){
      bottom = document.body.scrollHeight;
      current = window.innerHeight+ document.body.scrollTop;
	done = false;
      if((bottom-current) >0){
        window.scrollTo(0, bottom);
        setTimeout (scrollToBottom, 3500 ); //If the loading exceeds this timeout, a try will be used.
      }
	  else {
			done = true;	
			
	  }
    };
	if(completed_once!=true){
   	 scrollToBottom();
	}
	
	if(done==true&&number_of_trys<10){//This number changes the number of allowed timeouts
		done=false;
		number_of_trys++;
		setTimeout(scrollToBottom, 2000);
		//This number sets the allotted timeout length.
		//If the loading exceeds both the timeout above and this timeout, another try will be used immediately.
	}
	
	done;
	
    " in doc
				
				set completed_once to true
				delay 1.0
			on error
				
			end try
		end repeat
		
		
		
		set URLs to false
		repeat while URLs is equal to false
			set URLs to do JavaScript "

	   URLArray = [];

	      		allLinks = document.links;
		for (i = 0; i < allLinks.length; i++) {
		
			URLArray.push(allLinks[i].href);
		}
			URLArray;
			
	  " in doc
		end repeat
		return URLs
		
	end tell
	
end friendExtractionGivenan

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
on extractionOfIDsFrom(unfiltered_links)
	set unfiltered to unfiltered_links
	set filtered to {}
	set direct_IDs to {}
	set indirect_IDs to {}
	set refined_IDs to {}
	set returned_IDs to {}
	repeat with x from 1 to count of items of unfiltered
		set n to item x of unfiltered
		if "=pb" is in n then
			if n is not in filtered then set end of filtered to n
		end if
	end repeat
	
	repeat with y from 1 to count of items of filtered
		set profile to item y of filtered
		
		if "profile.php?" is in profile then
			
			set end of direct_IDs to text ((offset of "=" in profile) + 1) thru ((offset of "&" in profile) - 1) of profile
			
		else
			set end of indirect_IDs to text 25 thru ((offset of "?" in profile) - 1) of profile
			
		end if
		
	end repeat
	
	return direct_IDs & indirect_IDs
	
end extractionOfIDsFrom


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
on nameExtractionGivenFriends(IDs)
	set names to {}
	
	repeat with a from 1 to count of items of IDs
		set id1 to item a of IDs
		tell application "Safari"
			set doc to front document
			do JavaScript "
		window.location.assign('https://graph.facebook.com/" & id1 & "?fields=first_name,last_name');
		" in doc
			
			delay 0.5
			set doc to front document
			set unFilteredNames to text of doc
			try
				set firstName to word 2 of unFilteredNames
				set lastName to word 4 of unFilteredNames
				
				set end of names to {firstName, lastName}
			end try
		end tell
		
	end repeat
	
	return names
	
end nameExtractionGivenFriends
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
on makeStatusUpdate(namesThatMeetCriterion) --note that the keyboard must be QWERTY for this to work
	
	set list1 to namesThatMeetCriterion
	set list3 to {}
	repeat with x from 1 to count of items of list1
		set n to item x of list1
		if n is not in list3 then set end of list3 to n
	end repeat
	
	set namesThatMeetCriterion to list3
	display dialog (count of items in namesThatMeetCriterion)
	repeat with a from 52 to count of items in namesThatMeetCriterion
		set currentName to item a of namesThatMeetCriterion
		set the clipboard to currentName
		tell application "Safari" to activate
		tell application "System Events"
			
			keystroke "@"
			keystroke "v" using {command down}
			delay 1
			keystroke " "
			delay 1
			keystroke return
			keystroke ","
			keystroke " "
		end tell
		
	end repeat
end makeStatusUpdate
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
on storeNames(namesThatMeetCriterion, database1)
	tell application "Database Events"
		
		repeat with a from 1 to count of items in namesThatMeetCriterion
			set anID to item a of namesThatMeetCriterion
			set firstName to item 1 of anID
			set lastName to item 2 of anID
			tell database1
				make new record with properties {name:firstName & " " & lastName}
			end tell
		end repeat
		save database1
	end tell
end storeNames
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
set theOutputFolder to POSIX path of "Macintosh HD:Users:Puccio:Documents:My Documents:Scripts:Database:"

set databaselocation to POSIX path of "Macintosh HD:Users:Puccio:Documents:My Documents:Scripts:Database:TheNames.dbev"

tell application "Database Events"
	set database1 to database databaselocation
end tell

set theNamesOfInterest to {}
tell application "Database Events"
	repeat with a from 1 to count of records in database1
		set end of theNamesOfInterest to name of record a of database1
	end repeat
	
end tell

makeStatusUpdate(theNamesOfInterest)

set dataCollected to true

if dataCollected is false then
	set processCancelled to false
	set requestingStartingID to display dialog "Press direct or indirect if you would like to poke one individual's friends" default answer "Monstermac77" with title "Poker" buttons {"Indirect", "Direct", "Cancel"} default button "Indirect"
	set button_pressed to the button returned of the result
	if the button_pressed is "Direct" then
		set startingIDType to "Direct"
		
	else if the button_pressed is "Indirect" then
		set startingIDType to "Indirect"
		
	else
		set processCancelled to true
		
	end if
	
	set startingID to text returned of requestingStartingID
	set unfiltered_links to friendExtractionGivenan(startingID, startingIDType)
	set returnedIDs to extractionOfIDsFrom(unfiltered_links)
	set names to nameExtractionGivenFriends(returnedIDs)
	
	set namesThatMeetCriterion to {}
	repeat with a from 1 to count of items in names
		set person to item a of names
		set firstName to item 1 of person
		set lastName to item 2 of person
		set firstNameCount to count of firstName
		set lastNameCount to count of lastName
		if firstNameCount is equal to lastNameCount then
			set end of namesThatMeetCriterion to {firstName, lastName}
		end if
		
	end repeat
end if