Script ignoring tests when reading form a file.

I moderate a fairly active forum and any little bit of help I can get the better. Recently our developers added the vaispy mod to the forums. This allows for a real time feed of what is happening. I decided to try and write a script that scanned the ispy XML for specific NO NOs on these forums. It works pretty good, though I’m sure there are some issues with my script. However, when my script gets to the point where it needs to check if the flagged XML even has been flagged previously it tends to ignore my pid.txt file and re-open the link in Firefox.

Sample Site with vaispy: http://forums.finalgear.com/vaispy.php
Sample XML with vaispy: http://forums.finalgear.com/vaispy.php?do=xml

Sample naughty.txt

Sample pid.txt

How the script works (Should)

Loads Flags “naughtywords” to a list
Activates Safari
Loads vaispy XML
Gathers source
Parses the source into a list using the tag in the XML
Loops through each item in the list and extracts whether it is a new thread or new post
If it is a new thread it then extracts the title
if it is a new post it extracts the preview
The script then scans the extracted Title -or- Preview against a the naughty words list using offset of
If scan is positive it grabes the postid from the event list
The postID is compared against a txt file of previously flagged postids
If it is there it should ignore the flag and continue on to the next event
If it is NOT there it should add the postid to the pid.txt assemble a URL and open that URL in firefox

What happens
When the script reaches the part where it needs to compare the postid against the pid.txt sometimes it will ignore the test if found = 0 then and then open the URL EVEN if found ≠0
EG the postid is already in the pid.txt but the script still opens it in Firefox.

set region to "na"
set XMLURL to "http://" & region & ".<REMOVED>.com/board/vaispy-secret.php?do=xml"

-- Load flags
set naughtylist to {}
set Shows to paragraphs of (read POSIX file "/Users/aaon/Desktop/naughty.txt")
repeat with nextLine in Shows
	if length of nextLine is greater than 0 then
		copy nextLine to the end of naughtylist
	end if
end repeat

-- Load Safari
tell application "Safari"
	-- open safari and prepare to load XML
	activate
	make new document at end of documents
	set URL of document 1 to ""
	my waitforload()
	
	repeat
		-- Load XML
		set URL of tab 1 of front window to XMLURL
		delay 10
		-- Get page source
		set currentTab to current tab of front window
		set currentSource to currentTab's source
		
		-- Load source into array
		set event_array to {}
		set tid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "</event>"
		set event_array to text items of currentSource
		
		-- Loop through array
		repeat with myevent in event_array
			try
				
				-- Check if end of array
				set event_test to offset of "</events>" in myevent
				if event_test = 0 then
					
					-- Check alert type
					set alerttype to my gbt(myevent, "<what>", "</what>")
					
					-- Alert is New Thread
					if alerttype = "New Thread" then
						set scan to my gbt(myevent, "<title>", "</title>")
						-- Alert is New Post	
					else
						set scan to my gbt(myevent, "<preview>", "</preview>")
					end if
					
					-- Compare scan to Flagslist
					repeat with naughtyword in naughtylist
						
						-- Check for Flags
						set test to offset of naughtyword in scan
						
						-- Test fails
						if test ≠ 0 then
							
							-- Get postID
							set postid to my gbt(myevent, "<postid>", "</postid>")
							
							-- Load postid list for comparison
							set pid to {}
							set Shows to paragraphs of (read POSIX file "/Users/aaon/Desktop/pid.txt")
							repeat with nextLine in Shows
								if length of nextLine is greater than 0 then
									copy nextLine to the end of pid
								end if
							end repeat
							delay 1
							-- Compare postid to postid list
							set found to 0
							repeat with s_pid in pid
								--display dialog "Post ID:" & postid & "   Scan Post ID:" & s_pid
								delay 0.25
								if s_pid = postid then
									set found to 1
								else
									set found to 0
								end if
							end repeat
							
							-- If postID not found
							if found = 0 then
								
								-- Asemble URL
								set FFURL to "http://" & region & ".<REMOVED>.com/board/showthread.php?p=" & postid & "&highlight=" & naughtyword
								
								-- Log postID. Open link
								do shell script "echo " & quoted form of postid & ">> ~/Desktop/pid.txt"
								delay 1
								do shell script "open -a Firefox " & quoted form of FFURL
							end if
							
						end if
						
					end repeat
					
				end if
			on error
				delay 10
				set URL of tab 1 of front window to XMLURL
				delay 10
			end try
		end repeat
		delay 10
	end repeat
	
end tell


on gbt(myText, fd, sd)
	try
		set tid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to fd
		set a to text item 2 of myText
		set AppleScript's text item delimiters to sd
		set b to text item 1 of a
		return b
	on error
		tell application "Safari"
			delay 10
			set URL of tab 1 of front window to XMLURL
		end tell
	end try
end gbt

on waitforload()
	--check if page has loaded
	set loadflag to 0
	repeat until loadflag is 1
		delay 0.5
		tell application "Safari"
			set test_html to source of document 1
		end tell
		try
			set zarg to text ((count of characters in test_html) - 10) thru (count of characters in test_html) of test_html
			if "</html>" is in text ((count of characters in test_html) - 10) thru (count of characters in test_html) of test_html then
				set loadflag to 1
			end if
		end try
	end repeat
end waitforload

Hi aaon.

I haven’t worked through the entire script yet, but some immediate points:

¢ Your variable name ‘XMLURL’ is a reserved word in the XMLLib OSAX. However, this should only cause problems if someone with XMLLib installed on their machine tries to compile the script from your source code.

¢ Also, you try to access ‘XMLURL’ in your gbt() handler, where it’s undefined.

¢ In a repeat of the type ‘repeat with loopVariable in listVariable’, the value of ‘loopVariable’ isn’t an item from the list, but a reference to that item in the list. So in your first repeat loop, you’re copying that reference to to the end of 'naughtyList, not the actual text value from ‘Shows’:

set naughtylist to {}
set Shows to paragraphs of "dog
cat
crab
ladies" --(read POSIX file "/Users/aaon/Desktop/naughty.txt")
repeat with nextLine in Shows
	if length of nextLine is greater than 0 then
		copy nextLine to the end of naughtylist
	end if
end repeat

return naughtylist
--> {item 1 of {"dog", "cat", "crab", "ladies"}, item 2 of {"dog", "cat", "crab", "ladies"}, item 3 of {"dog", "cat", "crab", "ladies"}, item 4 of {"dog", "cat", "crab", "ladies"}}

Later on, you ‘repeat with naughtyword in naughtylist’, so each value of ‘naughtyword’ is a reference to an item in a list full of references to items in another list! It can get horrendously long:

set naughtylist to {}
set Shows to paragraphs of "dog
cat
crab
ladies" --(read POSIX file "/Users/aaon/Desktop/naughty.txt")
repeat with nextLine in Shows
	if length of nextLine is greater than 0 then
		copy nextLine to the end of naughtylist
	end if
end repeat

repeat with naughtyword in naughtylist
	return naughtyword -- Just to show us the value of naughtyword first time round the repeat.
end repeat
--> item 1 of {item 1 of {"dog", "cat", "crab", "ladies"}, item 2 of {"dog", "cat", "crab", "ladies"}, item 3 of {"dog", "cat", "crab", "ladies"}, item 4 of {"dog", "cat", "crab", "ladies"}}

From the same start point, you do the exactly same things with ‘s_pid’ and ‘pid’. ‘s_pid’ will have the same values as ‘naughtyword’ ” ie. references ” so it’s very unlikely to have the same value as ‘postid’ where you compare them and ‘found’ will always be set to 0.

You could use ‘repeat with i from 1 to whatever’ repeats instead, but the easiest fix would be to use ‘contents’ to resolve the loop variable reference each time:

set naughtylist to {}
set Shows to paragraphs of "dog
cat
crab
ladies" --(read POSIX file "/Users/aaon/Desktop/naughty.txt")
repeat with nextLine in Shows
	if length of nextLine is greater than 0 then
		copy nextLine's contents to the end of naughtylist -- NB. 'contents'
	end if
end repeat

repeat with naughtyword in naughtylist
	return naughtyword's contents -- ditto.
end repeat
--> "dog"

Thanks NG! I took what you said into consideration, and I think I understood you. It has been a good 3 years since I have used Applescript so for all intense and purpose im a noob.

I made some modifications to the script based on your suggestions and it appears to be running correctly. If you don’t mind taking another look and letting me know if the modifications I made to the script are what you intended.

set region to "na"
set XMLURL to "http://" & region & ".<URL REMOVED>.com/board/vaispy-secret.php?do=xml"

-- Load flags
set naughtylist to {}
set naughtylist to paragraphs of (read POSIX file "/Users/aaon/Desktop/naughty.txt")
--set Shows to paragraphs of (read POSIX file "/Users/aaon/Desktop/naughty.txt")
--repeat with nextLine in Shows
--	if length of nextLine is greater than 0 then
--		copy nextLine to the end of naughtylist
--	end if
--end repeat

-- Load Safari
tell application "Safari"
	-- open safari and prepare to load XML
	activate
	make new document at end of documents
	set URL of document 1 to ""
	my waitforload()
	
	repeat
		-- Load XML
		set URL of tab 1 of front window to XMLURL
		delay 10
		-- Get page source
		set currentTab to current tab of front window
		set currentSource to currentTab's source
		
		-- Load source into array
		set event_array to {}
		set tid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "</event>"
		set event_array to text items of currentSource
		
		-- Loop through array
		repeat with myevent in event_array
			try
				
				-- Check if end of array
				set event_test to offset of "</events>" in myevent
				if event_test = 0 then
					
					-- Check alert type
					set alerttype to my gbt(myevent, "<what>", "</what>")
					
					-- Alert is New Thread
					if alerttype = "New Thread" then
						set scan to my gbt(myevent, "<title>", "</title>")
						-- Alert is New Post	
					else
						set scan to my gbt(myevent, "<preview>", "</preview>")
					end if
					
					-- Compare scan to Flagslist
					repeat with naughtyword in naughtylist
						
						-- Check for Flags
						set test to offset of naughtyword's contents in scan
						
						-- Test fails
						if test ≠ 0 then
							
							-- Get postID
							set postid to my gbt(myevent, "<postid>", "</postid>")
							
							-- Load postid list for comparison
							set pid to {}
							set pid to paragraphs of (read POSIX file "/Users/aaon/Desktop/pid.txt")
							--set Shows to paragraphs of (read POSIX file "/Users/aaon/Desktop/pid.txt")
							--repeat with nextLine in Shows
							--	if length of nextLine is greater than 0 then
							--		copy nextLine to the end of pid
							--	end if
							--end repeat
							
							delay 1
							-- Compare postid to postid list
							set found to 0
							repeat with s_pid in pid
								--display dialog "Post ID:" & postid & "   Scan Post ID:" & s_pid
								delay 0.25
								if s_pid's contents = postid then
									set found to 1
								else
									set found to 0
								end if
							end repeat
							
							-- If postID not found
							if found = 0 then
								
								-- Asemble URL
								set FFURL to "http://" & region & ".<URL REMOVED>.com/board/showthread.php?p=" & postid & "&highlight=" & naughtyword
								
								-- Log postID. Open link
								do shell script "echo " & quoted form of postid & ">> ~/Desktop/pid.txt"
								delay 1
								do shell script "open -a Firefox " & quoted form of FFURL
							end if
							
						end if
						
					end repeat
					
				end if
			on error
				delay 10
				--set URL of tab 1 of front window to XMLURL
				tell application "System Events"
					tell process "Safari"
						keystroke "r" using {command down}
					end tell
				end tell
				delay 10
			end try
		end repeat
		delay 10
	end repeat
	
end tell


on gbt(myText, fd, sd)
	try
		set tid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to fd
		set a to text item 2 of myText
		set AppleScript's text item delimiters to sd
		set b to text item 1 of a
		return b
	on error
		tell application "Safari"
			delay 10
			set URL of tab 1 of front window to XMLURL
		end tell
	end try
end gbt

on waitforload()
	--check if page has loaded
	set loadflag to 0
	repeat until loadflag is 1
		delay 0.5
		tell application "Safari"
			set test_html to source of document 1
		end tell
		try
			set zarg to text ((count of characters in test_html) - 10) thru (count of characters in test_html) of test_html
			if "</html>" is in text ((count of characters in test_html) - 10) thru (count of characters in test_html) of test_html then
				set loadflag to 1
			end if
		end try
	end repeat
end waitforload

I left all the old code still in but commented it out. You should be able to see the modifications I made then.

Hi aaon.

Yes. You seem to have it. The critical line is the comparison with ‘postid’ and that’s exactly right. You’ve done away with the other repeats altogether, which is great!

There’s still the problem with accessing ‘XMLURL’ in the ‘gbt’ handler. If there’s ever an error in ‘try’ block and the ‘on error’ section tells Safari to set the URL of the front tab to XMLURL, you’ll get another error saying that the variable isn’t defined. By default, variables in handlers are local and not connected with variables elsewhere in the script. You need either to make XMLURL a global or a property or to pass its value as another parameter to the handler.

Excellent! And the XMLURL in the gbt handler was a mistake. I though I took it out but I just edited the other on error to reflect the new:

delay 10
				--set URL of tab 1 of front window to XMLURL
				tell application "System Events"
					tell process "Safari"
						keystroke "r" using {command down}
					end tell
				end tell
				delay 10

XMLURL has also since been done away with. Now all i ahve to do is play with the delays and add a few more things.

THANKS AGAIN!

Hey NG!

Today the forums I moderate blew up with server issues, so it really tested my script. And turns out I’m having the same issue, my script is ignoring the PID.txt and opening ANY flagged message. The reason the PID.txt exists is sometimes when the XML refreshes enough new posts have not populated the board to push the previously flagged post out of the XML.

Here is my updated script (I added some logging to help me track where things are going wrong)

set region to "euw"
set XML_URL to "http://" & region & ".<URL REMOVED>.com/board/vaispy-secret.php?do=xml"

set postid to ""
set found to ""
set FFURL to ""
set s_pid to ""

-- Load flags
set naughtylist to {}
set nauttxt to my readFile("/Users/aaon/Desktop/naughty.txt")
set naughtylist to paragraphs of nauttxt
--display dialog naughtylist
delay 2

-- LOG
do shell script "echo Region: " & quoted form of region & ">> /Users/aaon/Desktop/Error.txt"
do shell script "echo XML URL: " & quoted form of XML_URL & ">> /Users/aaon/Desktop/Error.txt"
do shell script "echo ------------------------------------------- >> /Users/aaon/Desktop/Error.txt"


-- Load Safari
tell application "Safari"
	-- open safari and prepare to load XML
	activate
	make new document at end of documents
	set URL of document 1 to ""
	my waitforload()
	
	repeat
		
		-- Load XML
		set URL of tab 1 of front window to XML_URL
		delay 5
		-- Get page source
		set currentTab to current tab of front window
		set currentSource to currentTab's source
		
		-- Load source into array
		set event_array to {}
		set tid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "</event>"
		set event_array to text items of currentSource
		-- Loop through array
		set event_array_pop to event_array
		set event_array to items 1 thru -2 of event_array_pop
		
		-- LOG
		do shell script "echo New Page >> /Users/aaon/Desktop/Error.txt"
		do shell script "echo ------------------------------------------- >> /Users/aaon/Desktop/Error.txt"
		do shell script "echo ------------------------------------------- >> /Users/aaon/Desktop/Error.txt"
		
		repeat with myevent in event_array
			try
				
				-- Check alert type
				set alerttype to my gbt(myevent, "<what>", "</what>")
				
				-- Alert is New Thread
				if alerttype = "New Thread" then
					set scan to my gbt(myevent, "<title>", "</title>")
					-- Alert is New Post	
				else
					set scan to my gbt(myevent, "<preview>", "</preview>")
				end if
				
				-- LOG
				do shell script "echo Alert Type: " & quoted form of alerttype & ">> /Users/aaon/Desktop/Error.txt"
				do shell script "echo Scan: " & quoted form of scan & ">> /Users/aaon/Desktop/Error.txt"
				
				-- Compare scan to Flagslist
				repeat with naughtyword in naughtylist
					
					-- Check for Flags
					set test to offset of naughtyword's contents in scan
					
					-- Test fails
					if test ≠ 0 then
						
						-- Get postID
						set postid to my gbt(myevent, "<postid>", "</postid>")
						set pcomp to region & " " & postid
						-- Load postid list for comparison
						set pid to {}
						set pidtxt to my readFile("/Users/aaon/Desktop/pid.txt")
						set pid to paragraphs of pidtxt
						
						-- Compare postid to PID
						set found to 0
						repeat with s_pid in pid
							
							-- Check for Flags
							set test to offset of s_pid's contents in pcomp
							--display dialog test
							delay 0.5
							-- Test fails
							if test ≠ 0 then
								--if s_pid's contents = pcomp then
								set found to 1
							else
								set found to 0
							end if
						end repeat
						
						-- If postID not found
						
						if found = 0 then
							
							-- Asemble URL
							set FFURL to "http://" & region & ".<URL REMOVED>.com/board/showthread.php?p=" & postid & "&highlight=" & naughtyword
							
							--  Open link
							do shell script "echo " & quoted form of pcomp & ">> /Users/aaon/Desktop/pid.txt"
							--delay 1
							do shell script "open -a Firefox " & quoted form of FFURL
						end if
						
						
					end if
					
					--display dialog "SPID: " & s_pid & " PCOMP: " & pcomp & " Found: " & found
					-- LOG
					do shell script "echo Naughty Word: " & quoted form of naughtyword & ">> /Users/aaon/Desktop/Error.txt"
					do shell script "echo Test: " & test & quoted form of ">> /Users/aaon/Desktop/Error.txt"
					do shell script "echo Post ID: " & quoted form of postid & ">> /Users/aaon/Desktop/Error.txt"
					do shell script "echo Found: " & found & ">> /Users/aaon/Desktop/Error.txt"
					do shell script "echo FFURL: " & quoted form of FFURL & ">> /Users/aaon/Desktop/Error.txt"
					do shell script "echo ------------------------------------------- >> /Users/aaon/Desktop/Error.txt"
					set postid to ""
					set found to ""
					set FFURL to ""
				end repeat
				
				
			on error err
				
				delay 10
				--set URL of tab 1 of front window to XML_URL
				tell application "Safari"
					do JavaScript "location.reload(true)" in document 1
				end tell
				delay 10
				
				-- LOG
				do shell script "echo ERROR: " & quoted form of err & ">> /Users/aaon/Desktop/Error.txt"
				
				
			end try
		end repeat
	end repeat
	
end tell


on gbt(myText, fd, sd)
	try
		set tid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to fd
		set a to text item 2 of myText
		set AppleScript's text item delimiters to sd
		set b to text item 1 of a
		return b
	on error
		
		delay 10
		tell application "Safari"
			
			do JavaScript "location.reload(true)" in document 1
			
		end tell
		delay 10
		
	end try
end gbt

on waitforload()
	--check if page has loaded
	set loadflag to 0
	repeat until loadflag is 1
		delay 0.5
		tell application "Safari"
			set test_html to source of document 1
		end tell
		try
			set zarg to text ((count of characters in test_html) - 10) thru (count of characters in test_html) of test_html
			if "</html>" is in text ((count of characters in test_html) - 10) thru (count of characters in test_html) of test_html then
				set loadflag to 1
			end if
		end try
	end repeat
end waitforload

on readFile(unixPath)
	set foo to (open for access (POSIX file unixPath))
	set txt to (read foo for (get eof foo))
	close access foo
	return txt
end readFile

Changes since last time we spoke
Added region + postID to the PID scan, this way the script can differentiate between postIDs of different regions.
Added logging
Changed on error to do do JavaScript “location.reload(true)” in document 1
Changed file paths to full path names EG ~/Desktop/pid.txt is now /Users/aaon/Desktop/pid.txt
Added handler for opening the naughty.txt and pid.txt.

on readFile(unixPath)
	set foo to (open for access (POSIX file unixPath))
	set txt to (read foo for (get eof foo))
	close access foo
	return txt
end readFile

Hi.

When you test ‘found’ after the repeat in this snippet, its value is the 1 or 0 from the last iteration of the repeatie. from testing the last item in ‘pid’. Either the ‘if found’ block needs to be inside the repeat, so that it’s there for every item, or the repeat needs to exit when an item of interest is discovered. Or possibly the ‘else’ section needs to be cut from the ‘if test’ statement. I haven’t had time so far to work out which.

Ah thanks! Added exit repeat and all seems to be groovey. That else statement needed to come out too. Thanks!

Edit#1:
One last thing, and you might not be able to help me with this. I notice after about an hour of running the memory usage for applescript is pretty closed to 2gb. Is there any way to occasionally purge the memory usage of the script. I have a feeling it’s because I keep loading the PID.txt over and over in the loop. THoughts?

Edit#2:
Setting the following seemed to curb the memory usage:

local postid, pcomp, pid, pidtxt, found, s_pid

Edit#3:
It seemed to work fine on EUW and EUNE but the moment I change the region to NA the memory skyrockets.

#lost

There are a few coding inefficiencies in the script, but I don’t know how they affect memory use. I did produce my own version, which you’re welcome to try. It’s simply your script with a few optimisations and hopeful optimisations:

¢ The implicit run handler code’s now in an ordinary handler so that all the variables are local.
¢ Safari is no longer used, the XML source being fetched by shell script. However, I’ve left the modified Safari code commented in.
¢ Fewer shell scripts are used to write the log.
¢ Strings are compared using the language command ‘contains’ rather than the OSAX command ‘offset’. Consequently the ‘test’ variable is no longer used and is omitted from the log.
¢ The “pid.txt” file is read, and the ‘pid’ lit created, just once at the top of the script. ‘pid’ is then updated at the same time as “pid.txt” instead of the file being reread and the list created anew each time.
¢ The ‘readFile’ handler reads files with implicit rather than explicit opening for access.

Other modifications:
¢ The paths used are set near the top of the script for editing convenience.
¢ The Safari window (if used) is referenced by id instead of by index.
¢ The ‘waitforload’ handler (if enabled for use with Safari) waits for an XML page instead of an HTML one. The comparison code has been optimised.
¢ The text item delimiters are now reset from ‘tid’ after use!
¢ The five-second delay in the main repeat has been moved to the end.

I used the URL you gave in post #1 to test it. I don’t know how it works with your address.

main()

on main()
	set region to "euw"
	set XML_URL to "http://" & region & ".<URL REMOVED>.com/board/vaispy-secret.php?do=xml"
	
	set postid to ""
	set found to ""
	set FFURL to ""
	set s_pid to ""
	
	-- Load flags
	set dtPath to (path to desktop as text)
	set naughtylist to paragraphs of readFile(dtPath & "naughty.txt")
	set pid to paragraphs of readFile(dtPath & "pid.txt")
	set pidPOSIX to quoted form of POSIX path of (dtPath & "pid.txt")
	set appendToLog to " >>" & quoted form of POSIX path of (dtPath & "Error.txt")
	
	-- LOG
	do shell script ("echo " & quoted form of ("Region: " & region & ¬
		"\\nXML URL: " & XML_URL & ¬
		"\\n-------------------------------------------") & appendToLog)
	
	(*	-- Load Safari
	tell application "Safari"
		-- open safari and prepare to load XML
		activate
		make new document at end of documents
		set XML_window to front window -- Reference to the window by ID.
	end tell *)
	
	repeat
		-- This shell script used instead of Safari.
		set currentSource to (do shell script ("curl " & quoted form of XML_URL))
		
		(* tell application "Safari"
			-- Load XML
			set URL of tab 1 of XML_window to XML_URL
			my waitforload()
			
			-- Get page source
			set currentTab to current tab of front window
			set currentSource to currentTab's source
		end tell *)
		
		-- Load source into array
		set tid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to "</event>"
		set event_array to text items 1 thru -2 of currentSource
		set AppleScript's text item delimiters to tid
		
		-- LOG
		do shell script ("echo " & quoted form of ("New Page\\n-------------------------------------------\\n-------------------------------------------") & appendToLog)
		
		repeat with myevent in event_array
			try
				
				-- Check alert type
				set alerttype to my gbt(myevent, "<what>", "</what>")
				
				-- Alert is New Thread
				if (alerttype = "New Thread") then
					set scan to my gbt(myevent, "<title>", "</title>")
					-- Alert is New Post	
				else
					set scan to my gbt(myevent, "<preview>", "</preview>")
				end if
				
				-- LOG
				do shell script ("echo " & quoted form of ("Alert Type: " & alerttype & ¬
					"\\nScan: " & scan) & appendToLog)
				
				-- Compare scan to Flagslist
				repeat with naughtyword in naughtylist
					
					if (scan contains naughtyword) then
						
						-- Get postID
						set postid to my gbt(myevent, "<postid>", "</postid>")
						set pcomp to region & " " & postid
						
						-- Compare postid to PID
						set found to false
						repeat with s_pid in pid
							if (pcomp contains s_pid) then
								set found to true
								exit repeat
							end if
						end repeat
						
						-- If postID not found
						
						if (not found) then
							-- Asemble URL
							set FFURL to "http://" & region & ".<URL REMOVED>.com/board/showthread.php?p=" & postid & "&highlight=" & naughtyword
							
							-- Append pcomp to both pid.txt and the current pid list.
							do shell script ("echo " & quoted form of pcomp & " >> " & pidPOSIX)
							set end of pid to pcomp
							--  Open link						
							do shell script ("open -a Firefox " & quoted form of FFURL)
						end if
					end if
					
					-- LOG
					do shell script ("echo " & quoted form of ("Naughty Word: " & naughtyword & ¬
						"\\nPost ID: " & postid & ¬
						"\\nFound: " & found & ¬
						"\\nFFURL: " & FFURL & ¬
						"\\n-------------------------------------------") & appendToLog)
					set postid to ""
					set found to ""
					set FFURL to ""
				end repeat
				
			on error err
				
				delay 10
				tell application "Safari"
					do JavaScript "location.reload(true)" in document 1
				end tell
				delay 10
				
				-- LOG
				do shell script ("echo " & quoted form of ("ERROR: " & err) & appendToLog)
				
			end try
		end repeat

		delay 5 -- Pause between iterations.
	end repeat
end main

on gbt(myText, fd, sd)
	try
		set tid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to fd
		set a to text item 2 of myText
		set AppleScript's text item delimiters to sd
		set b to text item 1 of a
		set AppleScript's text item delimiters to tid
		
		return b
	on error
		
		delay 10
		tell application "Safari"
			
			do JavaScript "location.reload(true)" in document 1
			
		end tell
		delay 10
		
	end try
end gbt

(* on waitforload()
	--check if page has loaded
	set loaded to false
	repeat until (loaded)
		delay 0.5
		tell application "Safari" to set test_xml to source of document 1
		try
			set loaded to (test_xml ends with "</events>")
		end try
	end repeat
end waitforload *)

on readFile(HFSPath)
	try
		set txt to (read file HFSPath from 1)
	on error
		-- File doesn't exist. Create it.
		close access (open for access file HFSPath)
		set txt to ""
	end try
	
	return txt
end readFile

Another idea would be to use an idle handler, but again, I’ve no idea if that would keep memory use under control. I may try it later today if I get time.

Thanks NG! But in your absence I created a bash script that does everything, and a bit quicker.

I still use a little applescript to get the page source but here is my bash script:

Ah! I thought you must have had experience in some other computer language(s)! Glad it’s all working now. :slight_smile:

Ha I never want to claim I have experience in any language. The world is a lot friendlier to “noobs.” Everything I do know though is from years of self struggle.

I’ve actually fine tuned my bash script since I last posted, pretty fast and posts flagged URLs to my company chat room. Woo cURL works somewhere!