2

I have an app which is written in AppleScripting language which checks if the S.M.A.R.T. status is 'Verified' if it is not then it will automatically show me a dialog alert. But I need it to run every 30 minutes. I used cron before but it never works with .app extension. So how can I accomplish running the app at regular intervals using launchd?

  • 2
    launchd neither works with app bundles (which is just a folder). You always have to choose a/the binary (e.g. /usr/local/sbin/smartmontool or *.app/Contents/MacOS/app_binary) – klanomath Oct 31 '17 at 10:30
  • Not that I'm against AppleScript, but wouldn't this be much, much easier in bash? – Allan Oct 31 '17 at 10:49
  • @Allan That in part might depend on how comfortable one is scripting in bash vs, AppleScript. Could use a combination of both. bash to do the S.M.A.R.T. status checking and AppleScript via osascript to display a dialog box or notification in Notification Center. But that's also what do shell script in AppleScript is for. – user3439894 Oct 31 '17 at 10:54
  • @user3439894 To get that status, AS would have to (at minimum) issue a shell command in bash anyway (diskutil info diskX | grep -i smart). That would be the meat and potatoes of the script right there. – Allan Oct 31 '17 at 10:58
  • In AppleScript, e.g.: display dialog "SMART Status: " & (do shell script "diskutil info disk0 | awk '/SMART Status:/{print $3}'") buttons {"OK"} default button 1 – user3439894 Oct 31 '17 at 11:08
1

Use oascript to call your ApplesScript.

$ oascript foobar.applescript

In your plist your ProgramArguments definition to launch the app would be as follows:

<key>ProgramArguments</key>
    <array>
        <string>osascript</string>
        <string>foobar.applescript</string>
    </array>


To get your item to run, you need a StartInterval definition which takes an integer for the number of seconds (1800 for 30 mins) between runs.

<key>StartInterval</key>
<integer>1800</integer>


A simple but functional plist would look as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.user.foobar.app</string>
   <key>ProgramArguments</key>
    <array>
        <string>osascript</string>
        <string>foobar.applescript</string>
    </array>
    <key>StartInterval</key>
    <integer>1800</integer>

</dict>
</plist>
  • It doesn't seem to work I am using .scpt file. Can you help me out with a complete .plist file? – user244378 Oct 31 '17 at 11:17
  • See this answer for details on writing the plist – Allan Oct 31 '17 at 11:36
  • Yeah thanks but I want it to run hourly not on a specific time or day. So I need help on this. – user244378 Oct 31 '17 at 11:45
  • I'm trying to to completely rewrite an answer that already exists, but I have put the most essential parts here. – Allan Oct 31 '17 at 11:59
  • If the OP truly has an app - why not write a shell script that's basically - while (true) do open /path/to/foo.app; sleep 1800 done? Might be easier than the path down turning it into a proper launch daemon or agent type script or installing / activating cron – bmike Oct 31 '17 at 12:05
0

Apple documents everything you need to write a hand crafted XML file to save to the filesystem and than load using launchtl load /path/to/xml but the documentation is very dense, assumes you have several days or weeks of experience and time to learn.

I recommend starting with a tool like LaunchControl or Lingon to ease your learning curve and add a nice interface to pick times like 30 minutes for the repeat interval instead of needing to code 1800 seconds into the xml file.

Here is what you'd enter for your 30 minutes and run only when the user is logged in constraints in the LaunchControl app

enter image description here

Again, you can do this by hand, working through every hurdle you will face (debugging errors to standard out and standard error when things don't work, debugging permissions and folder / file locations) but that's typically too broad a question here for getting someone from an idea to working implementation.

I'd go for the easy tools unless you have a desire to master several scripting tools and the launch daemon and control system under the hood.

You must log in to answer this question.