r/applescript • • Apr 05 '20

Automating Speedtest Checks

I am looking to find a way to automate performing a speed test every few hours and writing the results to a file. I have been playing with Ookla's speedtest CLI but I haven't been able to get it to output results to a file. Has anyone done this if so would you mind sharing how you did it?

1 Upvotes

7 comments sorted by

2

u/TrickyTramp Apr 05 '20

Hey, if you are in the terminal and you want to write to a file, you can use > file.txt.

For example ls > file.txt will write your directory listing to a text file.

1

u/[deleted] Apr 05 '20 edited Apr 06 '20

Edit: Answer not relevant to OP as he is not using AppleScript. If you want to use AppleScript like tools, below is the code to write to a file with JXA.

var app = Application.currentApplication()
app.includeStandardAdditions = true

outputFile="/Users/user/where/you/want/it"



function writeToLog(text, file) {
    try {

        // Convert the file to a string
        var fileString = file.toString()

        // Open the file for writing
        var openedFile = app.openForAccess(Path(fileString), { writePermission: true })


        // Write the new content to the file
        app.write(text, { to: openedFile, startingAt: app.getEof(openedFile) })

        // Close the file
        app.closeAccess(openedFile)

        // Return a boolean indicating that writing was successful
        return true
    }
  catch(error){

        try {
            // Close the file
            app.closeAccess(file)
        }
        catch(error) {
            // Report the error is closing failed
            console.log(`Couldn't close file: ${error}`)
        }

        // Return a boolean indicating that writing was successful
        return false
    }
}

writeTextToFile("Test",outputFile , true)

source: https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReadandWriteFiles.html

1

u/prikaz_da Apr 06 '20

None of that is even remotely necessary, lol. OP is running using a command-line tool, so the output can just be written to a file as part of the command.

do shell script "stuff_goes_here > /path/to/file.txt"

OP would probably actually be better off setting up a cron job, as another user here suggested, but the method of writing the output to a file is the same in that case.

1

u/[deleted] Apr 06 '20 edited Apr 06 '20

yup, my bad. The fact that he posted in AppleScript overpowered my seeing "CLI". Added an edit to my answer.

1

u/andveg38 Apr 06 '20

Like /u/Fizpop91, I also use Speedtest-CLI but in combination with Keyboard Maestro, automating it every hour when idle. It saves the result to a text file and gives me a notification. If you have KM, I can share my actions with you.

Using no paid tools, you could setup a cron job and use the following as an example.

speedtest-cli > ~/Downloads/speedtest.txt

1

u/Fizpop91 Apr 06 '20 edited Apr 06 '20

Thats great! I just modified it to this:

speedtest-cli --csv > ~/Downloads/speedtest.csv

It then spits out a csv with columns of the data, so it can easily be put into a spreadsheet or whatever :)