How Do I Create an Applescript that executes curl commands w delays?

then the address iteration loop must be moved to the delayAndCurl handler


property logFileName : "myLog.txt"
property logFile : ""

set logFile to ((path to library folder from user domain as text) & "Logs:" & logFileName)

repeat
	set ipAddresses to paragraphs of (read file "MacHD:path:to:file.txt") -- change to proper path
	delayAndCurl(300, "keypress/home", ipAddresses)
	delayAndCurl(10, "launch/42088", ipAddresses)
	delayAndCurl(15, "keypress/right", ipAddresses)
	delayAndCurl(5, "keypress/select", ipAddresses)
end repeat

on delayAndCurl(theDelay, thePath, addressList)
	delay theDelay
	repeat with anAddress in addressList
		try
			do shell script "curl -d http://" & anAddress & ":8060/" & thePath
		on error errorMessage
			writeToLog(thePath & " failed: " & errorMessage)
		end try
	end repeat
end delayAndCurl

on writeToLog(theMessage)
	tell (current date) to set timestamp to short date string & space & time string & ": "
	try
		set fileReference to open for access file logFile with write permission
		set logFileEof to get eof of the fileReference
		write timestamp & theMessage & return to fileReference starting at eof as «class utf8»
		close access fileReference
	on error
		try
			close access file logFile
		end try
	end try
end writeToLog


Thank you Stefan! Brilliant! I am still making an effort to wrap my head around how you are able to come up with these solutions so quickly! Your brain works in amazing ways indeed. :slight_smile:

Total newbie question, but on specifying the file path to the ip list:

  1. must the path be included in quotes?

AND

  1. is proper path specification with : or / ? - worried about the space in the Volume name “Macintosh HD”…

i.e, “Macintosh HD:Users:admin:Desktop:iplist.txt” or “/Users/admin/Desktop/iplist.txt”?

It’s quite like a real language. If you’re able to speak it, it’s quite easy to build the phrases :wink:

No, quotation is only needed in the shell environment

The native AppleScript path representation is HFS path (starting with a disk name and colon separated).
However nowadays some applications and Scripting Additions commands accept also POSIX paths (starting with a slash representing the startup volume and slash separated).

To point to the desktop I recommend to use the relative path

(path to desktop as text) & "iplist.txt"

which works regardless of the name of the startup volume and the name of the current user

Wow! Genius!

And the solutions you are able to come up with are simple, elegant and clean.

Absolutely Brilliant! Thank you for all of your guidance and support!

PS: if any address do contain space or other special characters quote the do shell script line


do shell script "curl -d " & quoted form of ("http://" & anAddress & ":8060/" & thePath)

Ah, OK. I see. Is there any downside to going ahead and using the Quoted version, just for “insurance?”

I’m using it always when passing a path or URL but in your case you’re passing only IPv4 addresses (111.222.333.444) which never contain spaces

Hello Stefan!

When I try to implement the relative path to Desktop as:

repeat
set ipAddresses to paragraphs of (read file (path to desktop as text) & “iplist.txt”)
delayAndCurl(5, “keypress/home”, ipAddresses)
delayAndCurl(8, “launch/42088”, ipAddresses)
delayAndCurl(12, “keypress/right”, ipAddresses)
delayAndCurl(3, “keypress/select”, ipAddresses)
delay 300
end repeat

I get an error in Applescript Editor:

error "Can’t get file /“Macintosh HD: USers:admin:Desktop:".” number -1728 from file “Macintosh HD:Users:admin:Desktop:”

Also, I placed a delay of 600 seconds at the end of the commands within this repeat loop - for two reasons - first to be able to run the script and see results without having to wait 300 seconds - and second, to see if this will still work here such that there is about 5 minutes delay between the entire CURL command sequence running (all IPs inclusive)

Can you advise on what I am doing wrong above?

Apologies, I have a typo - I should have copy pasted the error - there are inadvertant spaces and a wrong cap:

correct error message below:

error "Can’t get file /“Macintosh HD:Users:admin:Desktop:".” number -1728 from file “Macintosh HD:Users:admin:Desktop:”

add another pair of parentheses


set ipAddresses to paragraphs of (read file ((path to desktop as text) & "iplist.txt"))

or in two lines


set txtFilePath to (path to desktop as text) & "iplist.txt"
set ipAddresses to paragraphs of (read file txtFilePath)

OK, so I still have syntax errors when recomposing the error message - so sorry: Slash going the wrong way!

error “Can’t get file "Macintosh HD:Users:admin:Desktop:".” number -1728 from file “Macintosh HD:Users:admin:Desktop:”

Ah! OK, got it! Adding additional parathensis worked.

However, now I am getting the error on CURL command - “no URL specified”

Events window shows:

do shell script “curl -d ‘http://192.168.2.235:8060/leypress/home’”
→ error "curl: no URL specified!

both versions in post #30 should work

Hi,
So far script is working like a charm. One question - on the LOG output, how can I make the AppleScript write the actual curl line/command that caused the error - ideally I need to see the IP address to know which device is having the issue. Right now, when I check the logs I get:

curl: (7) couldn’t connect to host
7/15/14 12:12:17 PM: launch/42088" failed:

I am not seeing the full “path” with an IP address - just the command part - which is useful too.

Any insight you can offer would be sincerely appreciated.

Kind Regards,

Ethan

just change

writeToLog(thePath & " failed: " & errorMessage)

to

writeToLog(anAddress & "/" & thePath & " failed: " & errorMessage)