1

I'm trying to run a startup AppleScript, and a AppleScript which run at a set time (I hate the automator / calendar approach)

on idle

    set currentTime to current date
    if the weekday of currentTime is (need to do for Tuesday to Saturday) then
        do shell script "kextunload /System/Library/Extensions/AppleHDA.kext " password "..." with administrator privileges

    end if

    --only check once a day
    return 24 * 60 * 60
end idle

the only issue with this approach is the script it's running all the time, using memory and also I'm not sure how to calculate the correct time yet. (24.60.60 is every 00:00 right?) I would like to run it at 05 AM

if I understand it right, adding the script.scpt to /Library/LaunchDaemons will make the script run all the time even if the user is logged out, or logging with the screensaver on right?

I read about a another approach with seems even better, basically using a Plist to launch a script at a set time.

PS : would all this work if the disk is encrypted ? and can I use AppleScript to log an user at boot with the screen off (or discreetly)so I can start open some apps without have security issues.

Can I have your help and advises about which approach is the best one? Many thanks in advance.

  • Is there a specific need to use AppleScript here? Running kextunload on specific dates/times can be done via launchd (or cron) directly. – nohillside Mar 11 '17 at 7:53
  • thank you, I will read about this, basically I want to run some terminal command with the screensaver on (turn off the internal speaker, purge the memory, open some app (which I don't think it's can work with the screensaver on) – Kevin Mar 11 '17 at 8:03
  • It makes a huge difference regarding the approach whether you want to run some commands you usually run in Terminal (e.g. kextunload) or whether you want to run some applications. You'll probably get better answers if you edit your question to clearly state your intention, without already making assumptions regarding possible solutions and their drawbacks. – nohillside Mar 11 '17 at 8:17
1

You can run a command at start-up with a launchd plist looking something like this, in /Library/LaunchDaemons

<?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>NameYourTask</string>
    <key>ProgramArguments</key>
    <array>
    <string>CommandToRun</string>
    </array>
</dict>
</plist>

To run at a defined time, use something like this (this runs Monday, 1am):

<?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>JobName</string>
        <key>ProgramArguments</key>
        <array>
            <string>CommandToRun</string>
        </array>
        <key>StartCalendarInterval</key>
        <dict>
            <key>Hour</key>
            <integer>1</integer>
            <key>Minute</key>
            <integer>0</integer>
            <key>Weekday</key>
            <integer>1</integer>
        </dict>
        <key>StandardOutPath</key>
            <string>LogFilePath</string>
        <key>StandardErrorPath</key>
            <string>LogFilePath</string>
    </dict>
</plist>

LaunchD has a lot of options regarding how the jobs can run - it can watch and keep processes running, run when networks or files are present, etc.

It looks like you need to define your requirements, then work out how to implement the jobs.

Not below, the command string is a single string. If you want to pass arguments, you can, with multiple string tags.

<array>
    <string>Command</string>
    <string>Arg1</string>
    <string>Arg2</string>
    <string>Arg3</string>
    <string>Arg4</string>
</array>
  • Thank you, I will try this that seems to be way better that my current script. – Kevin Mar 12 '17 at 9:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .