Updated Scheduling exports on macOS (markdown)

Yudi
2019-09-17 23:00:00 -03:00
parent cf5e2abb78
commit 15f469d95a

@@ -1,25 +1,27 @@
Scheduling on macOS is a bit tricky, but it should work if you follow the instructions accordingly.
Make sure you already have DiscordChatExporter and Mono properly installed ([instructions here](https://github.com/RenanYudi/DiscordChatExporter/wiki/macOS-usage-instructions)).
* [Creating a Workflow](https://github.com/Tyrrrz/DiscordChatExporter/wiki/Scheduling-exports-on-macOS#creating-a-workflow-with-automator)
* [Scheduling with Calendar](https://github.com/Tyrrrz/DiscordChatExporter/wiki/Scheduling-exports-on-macOS#scheduling-with-calendar)
* [Scheduling with Lingon](https://github.com/Tyrrrz/DiscordChatExporter/wiki/Scheduling-exports-on-macOS#scheduling-with-lingon)
* [**1.** Creating the script](https://github.com/Tyrrrz/DiscordChatExporter/wiki/Scheduling-exports-on-macOS#1-creating-the-script)
* [**2.** Creating the .plist file](https://github.com/Tyrrrz/DiscordChatExporter/wiki/Scheduling-exports-on-macOS#2-creating-the-plist-file)
* [Exporting on System Boot/User Login](https://github.com/Tyrrrz/DiscordChatExporter/wiki/Scheduling-exports-on-macOS#exporting-on-system-bootuser-login)
* [Exporting every _n_ seconds](https://github.com/Tyrrrz/DiscordChatExporter/wiki/Scheduling-exports-on-macOS#exporting-every-n-seconds)
* [Exporting at a specific time and date](https://github.com/Tyrrrz/DiscordChatExporter/wiki/Scheduling-exports-on-macOS#exporting-at-a-specific-time-and-date)
* [**3.** Loading the .plist into launchctl](https://github.com/Tyrrrz/DiscordChatExporter/wiki/Scheduling-exports-on-macOS#3-loading-the-plist-into-launchctl)
* [Exporting on User login](https://github.com/Tyrrrz/DiscordChatExporter/wiki/Scheduling-exports-on-macOS#exporting-on-user-login)
## Creating a Workflow with Automator
## 1. Creating the script
**1.** Search for `Automator.app` on Spotlight (⌘+SPACE) and open it
**1.** Open TextEdit.app and create a new file
**2.** Create a new `Workflow` file
**2.** Convert the file to plain text in 'Format > Make Plain Text' (⇧⌘T)
![](https://i.imgur.com/DpsQkAR.png)
![](https://i.imgur.com/WXrTtXM.png)
**3.** Search for `Shell` and Drag it to the empty space on the right
![](https://i.imgur.com/AIv3iS6.png)
**4.** Paste the following into the empty text box:
**3.** Paste the following into the editor:
```bash
#!/bin/bash
# Info: https://github.com/Tyrrrz/DiscordChatExporter/wiki
TOKEN=<token>
@@ -30,8 +32,9 @@ FILENAME=<name>
EXPORTDIRECTORY=<dir>
EXPORTFORMAT=<format>
# Available export formats: PlainText, HtmlDark, HtmlLight, Csv
# /\ CaSe-SeNsItIvE /\
cd $EXEPATH || exit
cd $EXEPATH || exit 1
if [[ "$TOKENTYPE" == "BOT" ]]; then
ISBOTYES=-b
@@ -39,24 +42,28 @@ fi
PATH=/Library/Frameworks/Mono.framework/Versions/Current/bin/:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
mono DiscordChatExporter.Cli.exe export -t ${TOKEN//\"} $ISBOTYES -c $CHANNEL -f $EXPORTFORMAT -o exporttmp
CURRENTTIME=`date +"%Y-%m-%d-%H-%M-%S"`
mono DiscordChatExporter.Cli.exe export -t $TOKEN $ISBOTYES -c $CHANNEL -f $EXPORTFORMAT -o export.tmp
CURRENTTIME=$(date +"%Y-%m-%d-%H-%M-%S")
if [[ "$EXPORTFORMAT" == "PlainText" ]]; then
mv "exporttmp" ${EXPORTDIRECTORY//\"}/$FILENAME-$CURRENTTIME.txt
mv "export.tmp" "${EXPORTDIRECTORY//\"}/$FILENAME-$CURRENTTIME.txt"
elif [[ "$EXPORTFORMAT" == "Html"* ]]; then
mv "exporttmp" ${EXPORTDIRECTORY//\"}/$FILENAME-$CURRENTTIME.html
mv "export.tmp" "${EXPORTDIRECTORY//\"}/$FILENAME-$CURRENTTIME.html"
elif [[ "$EXPORTFORMAT" == "Csv" ]]; then
mv "exporttmp" ${EXPORTDIRECTORY//\"}/$FILENAME-$CURRENTTIME.csv
mv "export.tmp" "${EXPORTDIRECTORY//\"}/$FILENAME-$CURRENTTIME.csv"
else
exit
echo "$EXPORTFORMAT - Unknown export format"
if ! rm -Rf "export.tmp" ; then
echo "Unable to delete $PWD/export.tmp"
fi
exit
exit 1
fi
exit 0
```
**5.** Replace:
**4.** Replace:
`<token>` with your [Token](https://github.com/Tyrrrz/DiscordChatExporter/wiki/Obtaining-Token-and-Channel-IDs)
@@ -72,52 +79,184 @@ exit
`<format>` with one of the available export formats
<br/>
**6.** Run the Workflow (this may take a while to complete). If the export file was created, you're ready to save the file.
To quickly get file or folder paths, select the file/folder, hit Command+I (⌘I) and copy what's after `Where:`. <br/>
After copying and pasting, make sure the file/folder name is not missing. And if any of the folders has a name with a space, add `\` before the space:
* `/Users/user/Documents` - Wrong ✗
* `/Users/user/Documents/Discord Exports` - Wrong ✗
* `/Users/user/Documents/Discord\ Exports` - Correct ✓
![](https://i.imgur.com/ogPrhNp.png)
![](https://i.imgur.com/29u6Nyx.png)
<br/>
## Scheduling with Calendar
**5.** Save the file as `filename.sh` not `.txt`
**1.** Search for `Calendar.app` on Spotlight (⌘+SPACE) and open it
**6.** Open Terminal.app, type `sudo chmod +x`, press the SPACE key, then drag & drop the folder into the Terminal window and hit RETURN. You may be prompted for your password, and you won't be able to see it while typing.
**2.** Create a New Event (⌘N).
## 2. Creating the .plist file
![](https://i.imgur.com/BtmOxc3.png)
```xml
<?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>local.discordchatexporter</string>
<key>Program</key>
<string>/path/to/filename.sh</string>
REPLACEME
</dict>
</plist>
```
* The `Label` string is the name of the export job, it must be something unique. Replace `local.discordchatexporter` between the `<string>` if you want to run more than one script.
* Set `starts` hour to the time you want the Workflow to be executed
* Set `repeat` frequency
* Set `alert` as `Custom…`, `Open File`, `Other…` and select your Workflow file, and `At time of event`
* The `Program` string is the path to the script. Replace `/path/to/filename.sh` between the `<string>` to the path of the previously created script.
![](https://i.imgur.com/GbUOPmt.png)
* Replace the `REPLACEME` with the content presented in the following sections, according to <u>when</u> you want to export. <br/>
## Scheduling with Lingon
[Lingon](https://www.peterborgapps.com/lingon/) ($14.99) is an easy tool to manage `launchd`, which allows you to set schedules for system tasks.
Save the file with the same name as the `Label`, like `local.discordchatexporter.plist` (not `.txt`) when you're finished. <br/>
Please notice that if the system is asleep, the job will be started the next time the computer wakes up.
![](https://i.imgur.com/5wl4AXb.png)
### Exporting on System Boot/User Login
**1.** Create a new scheduled task (⌘N)
```xml
<key>RunAtLoad</key>
<true/>
```
**2.** Name it
### Exporting every _n_ seconds
**3.** Choose the Workflow file
The following example is to export every 3600 seconds (1 hour), replace the integer value with your desired time:
**4.** Select the "Scheduled" box and set the time
```xml
<key>StartInterval</key>
<integer>3600</integer>
```
**5.** Don't forget to save
### Exporting at a specific time and date
## Exporting on User Login
Template:
```xml
<key>StartCalendarInterval</key>
<dict>
<key>Weekday</key>
<integer>0</integer>
<key>Month</key>
<integer>0</integer>
<key>Day</key>
<integer>0</integer>
<key>Hour</key>
<integer>0</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
```
**1.** Open System Preferences on the Apple () menu
Key | Integer
------------ | -------------
**Month** | 1-12
**Day** | 1-31
**Weekday** | 0-6 (0 is Sunday)
**Hour** | 0-23
**Minute** | 0-59
**2.** Open Users & Groups
**Sunday** - 0; **Monday** - 1; **Tuesday** - 2; **Wednesday** - 3; **Thursday** - 4; **Friday** - 5; **Saturday** - 6.
**3.** Click on User and then on Login Items
Replace the template's `0`s according to the desired times.<br/>
You can delete the `<key>`s you don't want, don't forget to remove the `<integer>` under it. Omitted keys are interpreted as wildcards, for example, if you delete the Minute key, the script will run at every minute, delete the Weekday key and it'll run at every weekday, and so on.
**4.** Drag your Workflow file into the list
**Examples ([or skip to step 3](https://github.com/Tyrrrz/DiscordChatExporter/wiki/Scheduling-exports-on-macOS#3-loading-the-plist-into-launchctl))**
![](https://i.imgur.com/xacYWCA.png)
Export everyday at 5:15 PM:
```xml
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>17</integer>
<key>Minute</key>
<integer>15</integer>
</dict>
```
Every 15 minutes of an hour (xx:15):
```xml
<key>StartCalendarInterval</key>
<dict>
<key>Minute</key>
<integer>15</integer>
</dict>
```
Every Sunday at midnight and every Wednesday full hour (xx:00). Notice the inclusion of `<array>` and `</array>` for multiple times:
```xml
<key>StartCalendarInterval</key>
<array>
<dict>
<key>Weekday</key>
<integer>0</integer>
<key>Hour</key>
<integer>00</integer>
<key>Minute</key>
<integer>00</integer>
</dict>
<dict>
<key>Weekday</key>
<integer>3</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
</array>
```
## 3. Loading the .plist into launchctl
**1.** Copy your `something.plist` file according to how you want it to run:
* `~/Library/LaunchAgents` runs as the current logged in user. Choose this if you want it to run when this user logs-in.
* `/Library/LaunchDaemons` runs as the system _"administrator"_ (root). Choose this if you want it to run on System Startup.
**If you don't wan't to run at System Startup/User Login:**<br/>
* If macOS has a single user:
* If you want to export when the user is logged in, choose the first one.<br/>
* If you want the script to always run, choose the seconds one.<br/>
* If macOS has a multiple users:
* If you want the script to run only when a certain user is logged in, choose the first one.<br/>
* If you want the script to always run, choose the second one.<br/>
To quickly go to these directories, open Finder and press Command+Shift+G (⌘⇧G), then paste the path into the text box.
**2.** To load the job into launchctl, in Terminal, type `launchctl load`, press SPACE, drag and drop the `.plist` into the Terminal window, hit RETURN. It won't output anything if it was successfully loaded.
### Extra launchctl commands
**Starting a job**<br/>
To run a job regardless of its conditions (time), being `local.discordchatexporter` its `Label`:
```
launchctl start local.discordchatexporter
```
**Unloading a job**
```
launchctl unload /path/to/Library/LaunchAgents/local.discordchatexporter.plist
```
**List every loaded job**
```
launchctl list
```
**Test if job is enabled**
```
launchctl list | grep local.discordchatexporter
```
#
Further reading: [Script management with launchd in Terminal on Mac](https://support.apple.com/guide/terminal/script-management-with-launchd-apdc6c1077b-5d5d-4d35-9c19-60f2397b2369/mac).<br/>
Special thanks to [@RenanYudi](https://github.com/RenanYudi)