From 15f469d95a6a318a108cd426291113d3a499aaee Mon Sep 17 00:00:00 2001 From: Yudi Date: Tue, 17 Sep 2019 23:00:00 -0300 Subject: [PATCH] Updated Scheduling exports on macOS (markdown) --- Scheduling-exports-on-macOS.md | 229 ++++++++++++++++++++++++++------- 1 file changed, 184 insertions(+), 45 deletions(-) diff --git a/Scheduling-exports-on-macOS.md b/Scheduling-exports-on-macOS.md index d155796..ff99add 100644 --- a/Scheduling-exports-on-macOS.md +++ b/Scheduling-exports-on-macOS.md @@ -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= @@ -30,8 +32,9 @@ FILENAME= EXPORTDIRECTORY= EXPORTFORMAT= # 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: `` with your [Token](https://github.com/Tyrrrz/DiscordChatExporter/wiki/Obtaining-Token-and-Channel-IDs) @@ -72,52 +79,184 @@ exit `` with one of the available export formats +
-**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:`.
+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) +
-## 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 + + + + + Label + local.discordchatexporter + Program + /path/to/filename.sh + REPLACEME + + +``` +* The `Label` string is the name of the export job, it must be something unique. Replace `local.discordchatexporter` between the `` 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 `` 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 when you want to export.
-## 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.
+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 +RunAtLoad + +``` -**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 +StartInterval +3600 +``` -**5.** Don't forget to save +### Exporting at a specific time and date -## Exporting on User Login +Template: +```xml +StartCalendarInterval + + Weekday + 0 + Month + 0 + Day + 0 + Hour + 0 + Minute + 0 + +``` -**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.
+You can delete the ``s you don't want, don't forget to remove the `` 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 +StartCalendarInterval + + Hour + 17 + Minute + 15 + + +``` + +Every 15 minutes of an hour (xx:15): + +```xml +StartCalendarInterval + + Minute + 15 + + +``` + +Every Sunday at midnight and every Wednesday full hour (xx:00). Notice the inclusion of `` and `` for multiple times: + +```xml +StartCalendarInterval + + + Weekday + 0 + Hour + 00 + Minute + 00 + + + Weekday + 3 + Minute + 0 + + +``` + +## 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:**
+* If macOS has a single user: + * If you want to export when the user is logged in, choose the first one.
+ * If you want the script to always run, choose the seconds one.
+* 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.
+ * If you want the script to always run, choose the second one.
+ +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**
+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).
Special thanks to [@RenanYudi](https://github.com/RenanYudi) \ No newline at end of file