Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HOWTO] EventScripter Script to Re-Apply Dark Theme After Updates #32

Open
andrewjmetzger opened this issue Mar 19, 2021 · 19 comments
Open
Labels
enhancement New feature or request

Comments

@andrewjmetzger
Copy link

andrewjmetzger commented Mar 19, 2021

Why this exists

After installing the rather excellent dark theme, I was frustrated that updates temporarily broke the theme until JDownloader 2 was restarted again. So, I looked up the API calls, and created a script for the Event Scripter plugin to work around the bug. This Event Scripter script will check for updates periodically, apply them if JDownloader 2 if it is idle, and then automatically restart JDownloader2 again to fix the dark theme.

To use this, enable Event Scripter from Settings -> Plugins. Then, add a new script with an Interval trigger. Set the trigger to a much longer delay, enable synchronous execution by checking the box, and paste the script from below. Scroll down for detailed instructions.

/*
    Name: Update and Re-Apply Theme
    
    Version: 1.0.1
    
    Author: https://github.com/andrewjmetzger
    
    Description: Check for and apply updates periodically if JDownloader 2 is idle, 
                 and then restart JDownloader 2 one more time to apply the dark theme properly.
                 
    Trigger Required: "Interval" (Recommended: 600000 (10 mins.) or more)
    
    Synchronous execution: Yes (checked)
*/

if (
        callAPI("update", "isUpdateAvailable") &&       // an update is available, and
        !callAPI("linkcrawler", "isCrawling") &&        // LinkCrawler is idle, and
        !callAPI("linkgrabberv2", "isCollecting") &&    // LinkGrabber is idle, and
        !callAPI("extraction", "getQueue").length &&    // nothing is being extracted, then
        isDownloadControllerIdle()                      // if no downloads are running...
) {
    callAPI("update", "restartAndUpdate");              // Check for and apply an update, then
    callAPI("system", "restartJD");                     // restart JDownloader 2 again to fix the theme
}

Headless users only -- here is the same script with no line breaks:

[ {
  "eventTrigger" : "INTERVAL",
  "enabled" : true,
  "name" : "Update: Reapply Theme",
  "script" : "/*\n    Name: Update and Re-Apply Theme\n    \n    Version: 1.0.1\n    \n    Author: https://github.com/andrewjmetzger\n    \n    Description: Check for and apply updates periodically if JDownloader 2 is idle, \n                 and then restart JDownloader 2 one more time to apply the dark theme properly.\n                 \n    Trigger Required: \"Interval\" (Recommended: 600000 (10 mins.) or more)\n    \n    Synchronous execution: Yes (checked)\n*/\n\nif (\n        callAPI(\"update\", \"isUpdateAvailable\") &&       // an update is available, and\n        !callAPI(\"linkcrawler\", \"isCrawling\") &&        // LinkCrawler is idle, and\n        !callAPI(\"linkgrabberv2\", \"isCollecting\") &&    // LinkGrabber is idle, and\n        !callAPI(\"extraction\", \"getQueue\").length &&    // nothing is being extracted, then\n        isDownloadControllerIdle()                      // if no downloads are running...\n) {\n    callAPI(\"update\", \"restartAndUpdate\");              // Check for and apply an update, then\n    callAPI(\"system\", \"restartJD\");                     // restart JDownloader 2 again to fix the theme\n}",
  "eventTriggerSettings" : {
    "lastFire" : 1616161911463,
    "interval" : 600000,
    "isSynchronous" : true
  },
  "id" : 1616156482823
} ]

Detailed Instructions

IMPORTANT: For the first time run, you must allow the script to use the Interval permission. Afterwards, the script will run automatically.

  1. In the Event Scripter settings, click Add to create a blank script
  2. Give the script a name, such as Update and Re-apply theme
  3. Change the Trigger for the script to Interval
  4. Open the blank script by clicking Edit
  5. Change the script interval to something long, like 600000 milliseconds (10 minutes)
  6. Check the box to Enable Synchronous execution
  7. Copy the first block of code above, and paste it in the Editor
  8. Save the script, then in the Event Scripter list click Edit again.
  9. Click Test Compile, and dismiss the "Success" message. (Note: If you do not see "Success", make sure to copy/paste the whole script.)
  10. Click Test Run to display a permission dialog
  11. In the permission dialog, make sure to check Don't show this again, then click Allow
  12. To apply the new permissions and close the script editor, click Save

@Vinylwalk3r if you like this script and want to include it in the repo, the JSON "headless" script can be added to ./cfg/org.jdownloader.extensions.eventscripter.EventScripterExtension.scripts.json under the JDownloader 2 installation directory. Note that this still requires the user to install and enable Event Scripter beforehand. Let me know and I'm happy to create a PR.

Hope this helps!
@andrewjmetzger


Changelog:

Version 1.0.1 - 2021-03-19:

  • Updated informational comments
  • Added top-level heading ("Why this exists")

Version 1.0.0 - 2021-03-19:

  • Initial release
@andrewjmetzger andrewjmetzger changed the title EventScripter Script to Re-Apply Dark Theme After Updates [HOWTO] EventScripter Script to Re-Apply Dark Theme After Updates Mar 19, 2021
@Vinylwalk3r
Copy link
Owner

Wow, @andrewjmetzger I godda say, thats not bad work your done here! Way above my head for sure!
You could most certainly make a PR for it, sounds like a awesome idea!

@Vinylwalk3r Vinylwalk3r added the enhancement New feature or request label Mar 19, 2021
@andrewjmetzger
Copy link
Author

Thanks! Would you mind testing it first, please? I only have one system running JD2, and more proof that everything works is always welcome. 😉

Note: If you do any testing, use a super short interval like 60000 milliseconds (1 minute) or something to avoid waiting so long for a restart.

@leeuniverse
Copy link

So... I'm dunce, but why do I not see anything related to the actual "files" that are needed to "reapply" the Theme?

After every update, I have to drag and drop the syntheticaBlackEye.jar into the folder structure of JDownloader.

@andrewjmetzger
Copy link
Author

Hey @leeuniverse, great question! Normally, to reapply the theme after an update, all you need to do is restart JDownloader 2 (JD2) a second time -- once by clicking the update button, and once more from the File menu. You shouldn't have to reinsert any JARs unless you completely wipe your settings or reinstall the program.

The "trick" discussed in this issue doesn't rely on modifying JD2 files at all. Instead, it uses the built-in Event Scripter plugin to:

  1. Detect an update (automatically restarting once if so)
  2. If an update was applied in the last step, wait a few seconds and restart JD2 again. The second restart (hopefully) "fixes" the theme.

To add this to your JD2 install, you only need to enable the Event Scripter and add the scripts at the top of this issue.

For more information on the Event Scripter plugin, check out the official docs and this list of curated community scrips.

Let me know if there is anything I can do to further explain or assist you. 😃

@leeuniverse
Copy link

Hummm.... Now I think I realize what's happening.

The author has Two install instructions. One in which you change a backup file and I guess it's just one and done? And then another which has an already fully edited .JAR file. Now that I think about it, the first one I guess would work with your method.
I realize I've been using the second install method because the first method doesn't include the "Green Progress Bars" which I like.

So, I guess that's why I was confused. LOL
Thanks for the info....

P.S., since you are on the ball, I figure I'll just ask you. ;)
How the flip do you do his "Terminal" thing to make the Top Bar of JDownloader black like the rest of the program?
I'm guessing Terminal is the Command Prompt right? When I put his code into it nothing happens?
I'm guessing there are "other" steps needed to do this? I don't get why he wasn't clear with this for us non-programers. LOL

@andrewjmetzger
Copy link
Author

andrewjmetzger commented Apr 15, 2021

Both instructions work with this script. You can absolutely use a backup as the author directs, then unzip that and add the green progress bars to the JAR. The steps in this thread should only be followed AFTER the black theme is installed to your satisfaction, including any tweaks such as green progress bars.

Edit: Fixed typos.

@andrewjmetzger
Copy link
Author

Apologies, I did not mean to close this issue. 🙃

@andrewjmetzger
Copy link
Author

As for the system bar command, you are correct, that is intended to go in the Mac terminal. if the command in the wiki doesn't work, are you using Windows?

On Windows 10, change the accent color in Settings -> Personalization -> Colors:

  1. Choose the Dark overall color
  2. Uncheck Automatically pick an accent color
  3. Choose a dark grey or black or something else as desired
  4. Check Show the accent color on ... Title bars and window borders

Otherwise, please post your macOS version (Check this using the Apple menu on the top left of the system bar -> About this Mac).

@leeuniverse
Copy link

leeuniverse commented Apr 15, 2021

Thanks... That sort of works. For some reason, it is also changing the color of the Windows Text Links on the Right of that Setting page. So, unfortunately, can't use too much of a dark color, otherwise can't read the links. LOL

BTW, the script you created above, is there also one you can make that will automatically transfer over the JAR file from one location to replace the one that's replaced on Update?

So, as you might know, his download includes already a fully edited (or near to it) Theme JAR file which we can just use to replace the program's theme JAR (save the Title Bar sadly). So on most updates, we have to replace that JAR file again because it gets overwritten on the update to be the default dark Black_Eye theme again.

Thus, it would be nice for the replacing of that file to be done automatically for us like your script does after updating on restart or whatever. Thanks.

@andrewjmetzger
Copy link
Author

is there also one you can make that will automatically transfer over the JAR file from one location to replace the one that's replaced on Update?

To clarify, the script in this thread does NOT touch any JARs. Assuming the green progress bars are installed correctly, these things happen when JD2 updates:

  1. JD2 looks good, but an update is available. You click the update button.
  2. Temporarily the dark theme is broken. Is this when you would replace the JAR?
  3. Restart JD2 again using the File menu option. The theme fixes itself and you should NOT need to replace any files.

If the advanced settings are configured correctly, you only have to replace files once per system.

@leeuniverse
Copy link

  1. Sure, I knew that... :)
  2. Yep.
  3. It's broke permanently... It's just using the Black_Eye theme which is especially bad due to the big fat WHITE space in the whole middle of JDownloader.
  4. But, as I mentioned, I realized that the "first" method of adding the theme, is "permanent".... (I am doing the JAR replacement method since it includes the Green Download Progress images) "save on update before restart, which I know now your script fixes".
    So, I haven't tested it, but I believe even with your method we have to change the progress bar images still after update since they are included in the JAR file?

BTW, there are also some images in the Dark Theme found at JDownloader 2.0\themes\standard\org\jdownloader\images\exttable\ which are "White" instead of the "black" ones that are default.
I'm forgetting how those are added, if they are in the JAR, or not... Anyway, just something I noticed.

Anyway, either way, can you make a version of the script which copies/pastes the JAR file after the update?
Just curious.... :)

Thanks

@andrewjmetzger
Copy link
Author

EventScripter can't modify core files, unfortunately it's very limited to controlling running downloads and the program itself. The proper way to install the green progress bars is to pack your own theme JAR. if you do that it will not revert after any update.

@leeuniverse
Copy link

So... apparently, you have to use one of the "existing" Themes as the Author did, since I guess he doesn't have access or know how to add his own JAR theme?

So, I apparently can't just "rename" the JAR to something else. Seems it's a more complicated thing otherwise the author would have done it.
I wish the Dev's themselves would just add the theme, since it's really all that's needed, Dark_Theme and the Default theme... pretty much all the others are junk. LOL

@andrewjmetzger
Copy link
Author

As a point of clarification, here is how to propery apply the green progress bars, so that they persist after JD2 is updated. As described above, they will briefly vanish mid-update when JD2 turns white, but a second restart should fix that and restore the customized theme.

  1. Make sure you have 7-Zip, or WinRAR installed. Opening and editing JARs doesn't work as well with the built-in Windows programs. I'll use 7-Zip for this example.
  2. Locate the path to your JD2 installation, such as C:\Program Files\JDownloader\libs\laf. Open it.
  3. At the top of the folder window, go to the View tab. Make sure File name extensions is checked, like this:
    image
  4. Make a copy of syntheticaBlackEye.jar. Paste it in the same place, and rename the copy to syntheticaBlackEye.jar.bak Windows will warn you about changing the file extension, but that's OK.
  5. Download the progress bar images. This is easiest if you download the whole repository as a ZIP and extract it somewhere temporarily, such as your Desktop folder. The actual green bar images are inside that ZIP at .\JDownloader-2-Dark-Theme-master\themeNameHere.jar\de\javasoft\plaf\synthetica\blackeye\images\(progressbar).
    Now open syntheticaBlackEye.jar with 7-Zip, like this:
    image
  6. Using 7-Zip, navigate inside the JAR to /de/javasoft/plaf/synthetica/blackeye/images/, like this: https://streamable.com/a3qm7m
  7. Drag the images from step 4 into 7-Zip. Overwrite the existing ones.
  8. Close 7-Zip and restart JD2

@andrewjmetzger
Copy link
Author

Just saw your last post... yes this is currently a JD2 limitation, so the theme MUST use the same name, at least for now. Issue #13 tracks the progress of making this repo an official theme.

@ChaoticHolon
Copy link

This script appears to have broken and needs some adjustment? Now it no longer restarts the 2nd time, even after adding multiple callAPI system restartJD lines.

@andrewjmetzger
Copy link
Author

👋 Hi @ChaoticHolon! Thank you for letting me know that you're not seeing the dark theme applied as expected.

I'm sad to say that we're limited in exactly how we can debug this, since the partially-applied theme we're trying to fix only occurs after a JDownloader 2 (JD2) update, so we'd have to wait for one, deliberately not apply the update, and run this script to test whether the restarts worked. We could deliberately downgrade JD2, although that seems less than ideal. 😞

Admittedly, I haven't updated the script in a while (I don't use the dark theme on my server). I have a funny feeling that JD2 doesn't "keep track" of script execution across restarts anymore, though. When this script was working as intended, the second restart would occur either right away (if you were lucky) or you'd be waiting around for the next occurrence of that Interval-based trigger. In other words, you'd be staring at a partially-applied JD2 theme for 10 minutes, maximally (if using the suggested Interval). Unless you were staring at JD2 during that brief window, you may have never seen it (which was very much by design).

With some update during the last year, it seems this behavior has changed. Using the script as it exists in v1.1.0 is quite literally a non-starter (heh). There are a couple of possible work-around solutions depending on how far you'd like to take this, but all of them are way beyond the scope of this issue. If you're curious, I've outlined them at the bottom. 🙂

Thanks and make it a great day,
Andrew


So, if the original solution's out, let's get creative... 😉

There is no native JD2 CLI to just do jdownloader --update && jdownloader --restart && jdownloader --restart, for instance. We'd have to automate the JD2 interface some other way. Here's a basic outline of the steps.

  1. Press Alt + H to open the Help menu
  2. In the Help menu, press Alt + F to Find Updates
  3. Press whatever button accepts the update and restarts JD2 (Probably Enter or similar)
  4. Wait for JD2 to update and restart itself; theme is partially applied
  5. In the time before links start downloading again, press Alt + F to open the File menu
  6. Press Alt + R to Restart JD2
  7. Wait for restart; theme is fully applied.

On Windows, we could use something like AutoHotKey to do this for us. On Windows/Linux/macOS, I'm sure we could do something similar and Automate the Boring Stuff with Python.

@leeuniverse
Copy link

leeuniverse commented Mar 9, 2022

So, just a mention to you all.... I've just been replacing the JAR file every update when it's needed, which isn't always, haven't had problems yet.
I'm also using the Green Bars which have already been replaced in the default location which never get overridden, so far anyway.

I don't do anything else fancy like the Terminal stuff or otherwise. So, other than the Top Border Bar being the system default, I always have the nice black theme the creator made.
It would be nice if the JD team would finally make this theme one of the included themes like they said they would what 5 years ago at least? LOL

@andrewjmetzger
Copy link
Author

andrewjmetzger commented Mar 9, 2022

So, just a mention to you all.... I've just been replacing the JAR file every update when it's needed, which isn't always, haven't had problems yet.
I'm also using the Green Bars which have already been replaced in the default location which never get overridden, so far anyway.

Hey @leeuniverse, that's a great point! If I were to do this again today, I'd take a similar approach. Replacing the JAR would also be easy to add into any JD2 launch shortcut, .desktop file, etc. for automatic re-theming.

I've included some untested scripts here if anyone wants to try this:

REM This is a Windows batch script
REM Save as themeJD2.bat or similar. 

REM Setup: Make changes to syntheticaBlackEye.jar such as green progress bars etc
REM and save a copy elsewhere (the first path in the copy). 
REM This script will restore the custom syntheticaBlackEye.jar 
REM to JD2's directory before every launch (the second path in the copy).

@echo off
copy /b /v /y "/path/to/modified/copy/of/syntheticaBlackEye.jar" "/path/to/JDownloader/libs/laf/syntheticaBlackEye.jar"
start jdownloader2.exe
exit
#!/usr/bin/env bash

# This is a Bash script for linux and possibly macOS.
# Save as themeJD2.sh or similar. 

# Setup: Make changes to syntheticaBlackEye.jar such as green progress bars etc. 
# Save a copy elsewhere  (the first path in the copy).
# This script will restore the custom syntheticaBlackEye.jar 
# to JD2's directory before every launch (the second path in the copy).

cp -f "/path/to/modified/copy/of/syntheticaBlackEye.jar" "/path/to/JDownloader/libs/laf/syntheticaBlackEye.jar"

# JD2 should be in your PATH, so this will launch it.
jdownloader &
disown
exit

It would be nice if the JD team would finally make this theme one of the included themes like they said they would what 5 years ago at least? LOL

Ha! Good luck with that. I think the JD2 team have their hands full with other issues. Lots of backlog in their bug tracker. They're doing a good job, though, so I can't really complain.


EDIT: Fixed a syntax error in the Windows batch script (misplaced space)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants