Dalam upaya meningkatkan produktivitas dan mengurangi gangguan (setelah mendengarkan 'Pekerjaan Dalam' oleh Cal Newport di Audible), saya memutuskan untuk secara otomatis meluncurkan dan menutup aplikasi email dan IM di Mac saya pada waktu-waktu tertentu, memungkinkan saya untuk fokus pada pekerjaan produktif sisa waktu. Saya membuat 2 skrip apel yang berfungsi dengan baik dari Terminal:

Script 1: /Users/$USER/Documents/dev/apple_scripts/open_comms_apps.scpt

# Open messaging & comms apps.
tell application "Microsoft Outlook.app"
    if not (exists window 1) then activate
end tell
tell application "Slack.app"
    if not (exists window 2) then launch
end tell
tell application "Skype.app"
    launch
end tell

Script 2: /Users/$USER/Documents/dev/apple_scripts/close_comms_apps.scpt

# Close messaging & comms apps.
tell application "Microsoft Outlook.app"
    if (exists window 1) then quit
end tell
tell application "Slack.app"
    quit
end tell
tell application "Skype.app"
    quit
end tell

NB: Saya sudah menetapkan Outlook, Slack dan Skype ke desktop mereka sendiri.


Yang ingin saya lakukan adalah membukanya 3 kali sehari hanya pada hari kerja, jadi saya membuat file plist berikut untuk launchd.

Tugas terjadwal 1: /Users/$USER/Library/LaunchAgents/com.open.comms.apps.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Disabled</key>
    <false/>
    <key>Label</key>
    <string>com.open.comms.apps</string>
    <key>KeepAlive</key>
    <false/>
    <key>RunAtLoad</key>
    <true/>
    <key>Program</key>
    <string>/usr/bin/osascript</string>
    <key>ProgramArguments</key>
    <array>
        <string>osascript</string>
        <string>/Users/$USER/Documents/dev/apple_scripts/open_comms_apps.scpt</string>
    </array>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Weekday</key>
            <integer>1</integer>
            <key>Hour</key>
            <integer>9</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>1</integer>
            <key>Hour</key>
            <integer>13</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>1</integer>
            <key>Hour</key>
            <integer>16</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>2</integer>
            <key>Hour</key>
            <integer>9</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>2</integer>
            <key>Hour</key>
            <integer>13</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>2</integer>
            <key>Hour</key>
            <integer>16</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>3</integer>
            <key>Hour</key>
            <integer>9</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>3</integer>
            <key>Hour</key>
            <integer>13</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>3</integer>
            <key>Hour</key>
            <integer>16</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>4</integer>
            <key>Hour</key>
            <integer>9</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>4</integer>
            <key>Hour</key>
            <integer>13</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>4</integer>
            <key>Hour</key>
            <integer>16</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>5</integer>
            <key>Hour</key>
            <integer>9</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>5</integer>
            <key>Hour</key>
            <integer>13</integer>
            <key>Minute</key>
            <integer>0</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>5</integer>
            <key>Hour</key>
            <integer>16</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
    </array>
</dict>
</plist>

Tugas yang dijadwalkan 2: /Users/$USER/Library/LaunchAgents/com.close.comms.apps.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Disabled</key>
    <false/>
    <key>Label</key>
    <string>com.close.comms.apps</string>
    <key>KeepAlive</key>
    <false/>
    <key>RunAtLoad</key>
    <true/>
    <key>Program</key>
    <string>/usr/bin/osascript</string>
    <key>ProgramArguments</key>
    <array>
        <string>osascript</string>
        <string>/Users/$USER/Documents/dev/apple_scripts/close_comms_apps.scpt</string>
    </array>
    <key>StartCalendarInterval</key>
    <array>
        <dict>
            <key>Weekday</key>
            <integer>1</integer>
            <key>Hour</key>
            <integer>9</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>1</integer>
            <key>Hour</key>
            <integer>13</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>1</integer>
            <key>Hour</key>
            <integer>16</integer>
            <key>Minute</key>
            <integer>45</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>2</integer>
            <key>Hour</key>
            <integer>9</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>2</integer>
            <key>Hour</key>
            <integer>13</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>2</integer>
            <key>Hour</key>
            <integer>16</integer>
            <key>Minute</key>
            <integer>45</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>3</integer>
            <key>Hour</key>
            <integer>9</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>3</integer>
            <key>Hour</key>
            <integer>13</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>3</integer>
            <key>Hour</key>
            <integer>16</integer>
            <key>Minute</key>
            <integer>45</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>4</integer>
            <key>Hour</key>
            <integer>9</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>4</integer>
            <key>Hour</key>
            <integer>13</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>4</integer>
            <key>Hour</key>
            <integer>16</integer>
            <key>Minute</key>
            <integer>45</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>5</integer>
            <key>Hour</key>
            <integer>9</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>5</integer>
            <key>Hour</key>
            <integer>13</integer>
            <key>Minute</key>
            <integer>30</integer>
        </dict>
        <dict>
            <key>Weekday</key>
            <integer>5</integer>
            <key>Hour</key>
            <integer>16</integer>
            <key>Minute</key>
            <integer>45</integer>
        </dict>
    </array>
</dict>
</plist>

NB: Saya mencoba ini dengan nama pengguna yang dikodekan dalam path file dan variabel sistem $ USER.


Saya mengubah izin pada skrip dan memuat file plist melalui terminal sebagai berikut:

chmod 755 /Users/$USER/Documents/dev/apple_scripts/open_comms_apps.scpt
chmod 755 /Users/$USER/Documents/dev/apple_scripts/close_comms_apps.scpt
launchctl load -w /Users/$USER/Library/LaunchAgents/com.open.comms.apps.plist
launchctl load -w /Users/$USER/Library/LaunchAgents/com.close.comms.apps.plist

Seperti yang mungkin diharapkan banyak orang, ini tidak berhasil dan ketika saya mencoba memulai tugas yang dijadwalkan secara manual:

launchctl start /Users/$USER/Library/LaunchAgents/com.open.comms.apps.plist
launchctl start /Users/$USER/Library/LaunchAgents/com.close.comms.apps.plist

Tidak ada yang terjadi.

Adakah yang bisa di luar sana (dengan lebih dari 1 hari pengalaman saya dengan skrip apel dan launchd), mohon saran bagaimana cara kerjanya?

Collisio-Adolebitque
sumber