IP Abuse data submittal (revisited)

In the original thread (but I can’t find it) sits an abuse submittal script which is as follows:


-- script:  Check IP addresses list for DD_DOS and other net attacks
--            Leave only 5 fields from the report, then sort the table by the 2-nd field
-- written by: Daron Brewood (today)
-- updated and fixed by: KniazidisR
-- note: visit https://api.abuseipdb.com to learn more details

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

property NSString : a reference to current application's NSString
property NSJSONSerialization : a reference to current application's NSJSONSerialization
property NSUTF8StringEncoding : a reference to current application's NSUTF8StringEncoding

property serviceAddress : "https://api.abuseipdb.com/api/v2/check"
property privateKey : "247cdf97b62552676b6f56a4e56611a7fcb2ceaced16cd1e27f7f987be861c1f42b96a7f8be45fb9"
property maxAgeInDays : 30 -- for last 30 days 

-- set ipList to {"13.32.145.30", "34.247.206.80", "36.156.66.62", "45.233.113.9", "46.51.207.89"}
-- or:
set ipListFile to (choose file of type "txt") -- provide ip addresses inside this file
set ipList to paragraphs of (read ipListFile)


-- set report to "ipAddress;isPublic;ipVersion;isWhitelisted;abuseConfidenceScore;countryCode;usageType;isp;domain;hostnames:;totalReports;numDistinctUsers;lastReportedAt"
set report to "IP Address;Abuse %;Country;ISP;Domain"
repeat with nextIP in ipList
	-- get JSON Report (as record) using site's API
	set jsonData to do shell script "curl -G " & serviceAddress & ¬
		" --data-urlencode \"ipAddress=" & nextIP & ¬
		"\" -d maxAgeInDays=" & maxAgeInDays & ¬
		" -H \"Key: " & privateKey & ¬
		"\" -H \"Accept: application/json\""
	set jsonString to (NSString's stringWithString:jsonData)
	set jsonData to (jsonString's dataUsingEncoding:NSUTF8StringEncoding)
	set aRecord to (NSJSONSerialization's JSONObjectWithData:jsonData options:0 |error|:(missing value)) as record
	
	-- BUILD THE REPORT as TEXT 
	tell (|data| of aRecord)
		if its abuseConfidenceScore ≥ 10 then -- Filters out low confidence abuse confidence
			set report to report & linefeed & its ipAddress & ";"
			set report to report & its abuseConfidenceScore & ";"
			set report to report & its countryCode & ";"
			set report to report & its isp & ";"
			set report to report & its domain & ";"
		end if
	end tell
	
end repeat

-- make temporary text file
set tempFolder to (path to temporary items folder from user domain)
tell application "Finder"
	try
		set reportTextFile to (make new file at tempFolder with properties {name:"Report.txt"}) as alias
	on error
		set reportTextFile to (file "Report.txt" of folder (tempFolder as text)) as alias
	end try
end tell

-- Calculate date stamp
set dateObj to (current date)
set theMonth to text -1 thru -2 of ("0" & (month of dateObj as number))
set theDay to text -1 thru -2 of ("0" & day of dateObj)
set theYear to year of dateObj
set dateStamp to "" & theYear & "-" & theMonth & "-" & theDay


-- write JSON report to temporary text file
set file_ID to open for access reportTextFile with write permission
set eof file_ID to 0
write report to file_ID as «class utf8»
close access file_ID

-- convert temporary text file to CSV file
-- set csvReportFile to "" & (path to downloads folder) & "Report " & dateStamp & ".csv"
-- set csvReportFile to "" & (path to startup disk) & "Volumes:NAS Store:IP Abuse reports:" & "Report " & dateStamp & ".csv"
set csvReportFile to "" & (path to desktop folder) & "Report " & dateStamp & ".csv"
tell application "Numbers"
	activate
	set theDoc to open reportTextFile
	tell theDoc to tell sheet 1 to tell table 1 to if (count rows) > 2 then sort by column 2 direction descending
	export theDoc to file csvReportFile as CSV
	close documents saving no
	-- quit it
end tell

This works wonderfully well with one exception … after the CSV file has been saved to the desktop it leaves a ‘ReportX’ numbers file live inside the Numbers app and next time I open Numbers I’m prompted to save it. Is there a way of closing Numbers and either saving that file automatically or closing it without saving? The ‘close documents saving no’ seems not to do anything.

It’s probably here:

Thanks muchly, you did better than I did in finding it. :slight_smile: I’ve now replied to the original thread re-stating my queries.