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.
Total newbie question, but on specifying the file path to the ip list:
must the path be included in quotes?
AND
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
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
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)
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: