A couple of questions about scripting automation on a Mac

I have been writing scripts in AutoIT for windows based systems and now I need to duplicate those scripts for use on Macs (M1 and M2). AutoIT is windows only so that will not work. I haven’t written scripts for Macs before.

I need to be able to retrieve basic system information, create a simple UI for the script, open apps, manipulate the UI by sending keystrokes and/or mouse clicks, get start and end times for tasks performed (I was able to detect the end of a task on Windows system by checking pixel changes on the screen), and output times and system info to a CSV file.

Is Applescript a good tool for this? Should I look into Python or something else? Any recommendations would be helpful.

It really depends on how complex your tasks are going to be. I love AppleScript but I use them sparingly. I get the information I need from Mail or Outlook and then do the heavy lifting in Xojo.

There are a couple of automation apps like Alfred or Hazel which allow you to do more complex workflows combining AppleScript, shell scripts and other things.

I on the other hand use AppleScript ALOT. Programs on the Mac are supposed to be Scriptable so AppleScript can control them.
For those apps that aren’t scriptable, AppleScript has GUI scripting, where it can control the graphical interface.
Then there is AppleScriptObjective-C which can call most of the operating systems frameworks directly to do really complex stuff.

If you give more specific examples of what you are wanting to do, many people here are very helpful and will get you going in the right direction

Here are the basic specific things I need to do:

  • Create a simple GUI for the user to make some selections and to show progress as script runs.
  • Verify that certain software is installed and get the version number of each.
  • Create a .csv file and write to it.
  • Lock out keyboard and mouse until script is complete (enabled Esc key to quit script in case user wants to quit script early)
  • Get computer name, OS edition and version/build number
  • Launch an app (Excel, Power BI, Visual Studio, HWInfo or something similar)
  • Either send direct commands to the app to perform a function (Rebuild a solution, export a report, run a macro, etc.)
  • OR manipulate the GUI to perform those same functions if there isn’t a direct command.
  • Get the time (to the tenth of a second) when a function/action is initiated.
  • Wait for the function/action to complete (in most cases I had to check the pixels on the screen and wait for them to stop changing) but some of the direct commands I was able to use with Excel for example would pause the script until they completed.
  • Get the time when the function/action completed and calculate the difference. I ultimately want to output the times as hh:mm:ss:. AutoIT gave me the times in milliseconds and I had to convert that to the format above.

All of those are doable with appleScript.

  • Create a simple GUI for the user to make some selections and to show progress as script runs.

I would use DialogToolkit Plus for the user selection, and AppleScript has a built in progress bar display.

  • Verify that certain software is installed and get the version number of each.

This can be done using this handler:

on GetApps(appNames)
   set infoForApps to {}
   tell application "System Events" to set allProcesses to processes whose background only is false
   repeat with thisProcess in allProcesses
      tell application "System Events"
         tell thisProcess
            
            properties
            set appBundleID to bundle identifier
            --   set appFile to path of application file
            set appScriptable to has scripting terminology
         end tell
      end tell
      set {fullAppName, appVer, appBundleVer} to GetappsInfo(appBundleID)
      if fullAppName is in appNames then
         set the end of infoForApps to {¬
            fullAppName, ¬
            appVer, ¬
            appBundleVer, ¬
            appBundleID, ¬
            appScriptable ¬
               }
      end if
   end repeat
   return infoForApps
end GetApps

  • Create a .csv file and write to it.

Very simple to do using basic plain vanilla appleScript. It becomes more complex if the data is not already in csv format.

  • Lock out keyboard and mouse until script is complete (enabled Esc key to quit script in case user wants to quit script early)

No, I don’t think there’s an easy way to do this on a mac.

  • Get computer name, OS edition and version/build number

Computer name is available from System events. Here’s commands to get OS version/build number.

set sysVer to (current application's NSProcessInfo's processInfo()'s operatingSystemVersionString()) as text
set sysVer to text items of (sysVer as text)
set {sysVer, buildNum} to ({item 2 of sysVer, item 4 of sysVer})
set sysVer to {"-->  Mac OS Version " & sysVer & " (" & buildNum & ")" as text}
  • Launch an app (Excel, Power BI, Visual Studio, HWInfo or something similar)

Yes, use the appleScript

tell application "app name" to activate/launch

or

tell application "Finder" to open "path to application"
  • Either send direct commands to the app to perform a function (Rebuild a solution, export a report, run a macro, etc.)

Yes, that is what AppleScript does.

  • OR manipulate the GUI to perform those same functions if there isn’t a direct command.

Yes, that’s also what AppleScript does.

  • Get the time (to the tenth of a second) when a function/action is initiated.

This gets you the date and time to the second.

set staretTime to current date 

I know there are ways to get more precision via AppleScript, but I’ve never needed that and so I can’t help you there.

  • Wait for the function/action to complete (in most cases I had to check the pixels on the screen and wait for them to stop changing) but some of the direct commands I was able to use with Excel for example would pause the script until they completed.

In most cases that’s the default procedure. Your script sends an apple event to an application, then waits for a response.

  • Get the time when the function/action completed and calculate the difference. I ultimately want to output the times as hh:mm:ss:. AutoIT gave me the times in milliseconds and I had to convert that to the format above.
set startTime to current date

--your script goes here

set endTime to current date

set endTime to startTime + (20 * hours) + (10 * minutes) + 30 -- you may use this command to test

set elapsedTimeInSeconds to endTime - startTime

set theMinutes to elapsedTimeInSeconds div 60
set theHours to elapsedTimeInSeconds div hours
set theSeconds to elapsedTimeInSeconds - (theMinutes * 60)
set theMinutes to theMinutes - (theHours * 60)

set AppleScript's text item delimiters to {":"}

return {theHours, theMinutes, theSeconds} as text

Note, the format here is “h:m:s” for single digit numbers, and would require a tiny be of formatting for “hh:mm:ss”

The only one I find difficult is the lockout of the keyboard.
Has someone else done that before?

Let’s get started on this. We’ll go over each step and how to do it!

Also AppleScript’s can be run in the Editor, from the menu-bar, or as a standalone-app.

Also for serious scriptures there is a 3rd party Debugger that is indispensable called “Script Debugger” from LateNight Software

1 Like

We’re off to a start!

You can get the OS version using plain AppleScript like this

tell (system attribute "sysv") to set osVersion to ("1" & it mod 4096 div 256 & "." & it mod 256 div 16 & "." & it mod 16)

What are the selections the User would select from? Is it a list?
Then we could query the User using the ‘choose from list’ command

Wow, thanks guys. I really appreciate your willingness to help out. I don’t yet have the Mac nor have I had a chance to do any setup I need to do to it yet. I hope to get it this week. Then I can get my feet wet and try out your suggestions. I feel I should also go through any tutorials to get some basic applescript understanding. I assume my knowledge of AutoIT and Python will translate to some degree but there are always differences to learn.

I’m a little busy at the moment but I will answer your questions later today.

Again, thanks for your help so far. Much friendlier than some forums for the other languages I have used.

1 Like