Adrian Nier Code

Back to overview

Implementation

-- The command to run in the Terminal window
property pCOMMAND : "top -ocpu -P '    PID     %CPU    COMMAND            USER' -p '$aaaaaa  $cccccc    ^bbbbbbbbbbbbbbbb  ^nnnnnnnnnnnnnn' -n 20"

-- Exit after command finished
property pAUTOEXIT : true

-- Custom title for the Terminal window
property pTITLE : "Processes by CPU usage"

-- Window size in rows and columns
property pSIZE : {29, 52}


on run
   prepareTerminalWindow()
   runTerminalCommandWithWindowSizeAndTitle(pCOMMAND, pSIZE, pTITLE)
   bringTerminalToFront()
   return
end run

on prepareTerminalWindow()
   tell application "Terminal"
      try
         if (exists first window) is false then
            -- Open a new window, if none exists
            do script ""
         else if (busy of selected tab of first window) is true then
            -- Open a new window, if the current one is busy
            do script ""
         end if
      on error
         do script ""
      end try
   end tell
end prepareTerminalWindow

on runTerminalCommandWithWindowSizeAndTitle(_command, _size, _title)
   tell application "Terminal"
      set number of rows of first window to item 1 of _size
      set number of columns of first window to item 2 of _size
      set custom title of first window to _title
      if pAUTOEXIT then
         do script (_command & " ; exit") in first window
      else
         do script _command in first window
      end if
   end tell
end runTerminalCommandWithWindowSizeAndTitle

on bringTerminalToFront()
   tell application "System Events"
      set frontmost of process "Terminal" to true
   end tell
end bringTerminalToFront