JamieMason / ImageOptim-CLI

Make optimisation of images part of your automated build process
https://foldleft.io/image-tools
MIT License
3.45k stars 126 forks source link

ImageOptim always quits #140

Closed jamesstout closed 6 years ago

jamesstout commented 7 years ago

There is a flag in the ImageOptim code that determines if the app should quit when done:

int main(int argc, char *argv[]) {
    quitWhenDone = hideLogs = isLaunchedWithCliArguments(argc, argv);

This is always set to true due to the call to ImageOptim in ImageOptimBashLib:

"${location}Contents/MacOS/ImageOptim" 2> /dev/null "${TEMP_PATH}"

The "${TEMP_PATH}" is the arg that sets quitWhenDone to 1, so the app quits.

Not sure of the fix, it's not the end of the world, but I quite like to inspect what ImageOptim has done.

Maybe another arg to ImageOptim, but that would need help from @pornel.

kornelski commented 7 years ago

What would be your preferred behavior?

JamieMason commented 7 years ago

*poke* @jamesstout

jamesstout commented 7 years ago

Maybe a flag that gets checked first that says "don't quit when done"?

static int isLaunchedWithCliArguments(int argc, char *argv[]) {
    // Unfortunately NSApplicationLaunchIsDefaultLaunchKey doesn't cover bare CLI launch
    if (argc < 2) return 0;
    for (int i=0; i < argc; i++) {
        if ([[NSString stringWithFormat:@"%s", argv[i]] isEqual:@"--dont-quit-when-done"]) {
            return 0;
        }
        if ('-' == argv[i][0]) { // Normal OS X launch sets -psn
            return 0;
        }
    }
    return 1;
}

Or if you want to stick with C strings:

#include <string.h>

static int isLaunchedWithCliArguments(int argc, char *argv[]) {
    // Unfortunately NSApplicationLaunchIsDefaultLaunchKey doesn't cover bare CLI launch
    if (argc < 2) return 0;
    for (int i=0; i < argc; i++) {
        if(strcmp(argv[i], "--dont-quit-when-done") == 0){
            return 0;
        }
        if ('-' == argv[i][0]) { // Normal OS X launch sets -psn
            return 0;
        }
    }
    return 1;
}
jamesstout commented 7 years ago

~~Option 1: https://github.com/ImageOptim/ImageOptim/compare/master...jamesstout:dont-quit-when-done Option 2: https://github.com/ImageOptim/ImageOptim/compare/master...jamesstout:dont-quit-when-done-cstring~~

Deleted those forks.

kornelski commented 7 years ago

I'm not interested in implementation part. "Not quitting" can be done even easier by deleting all the qutting logic I've deliberately put in there.

I'm more interested in understanding why quitting is a problem, and how else do you want ImageOptim to behave.

Note that when it's launched directly from CLI, it crates multiple copies of the app, and each will open a window, steal focus and try to get user's attention. If you run it 20 times, you'll have 20 ImageOptim windows sitting idle. Surely, that's not a desireable behavior?

There's another alternative that already exists and works, which is to use the open -a ImageOptim command, which will find an existing running instance and add files to it. That doesn't quit automatically.

jamesstout commented 7 years ago

Yeah, thinking about it, if imageOptim doesn’t exit then Jamie’s imageOptimBashLib doesn’t know imageOptim has finished.

Really, I just noticed that the -q option in imageOptim-CLI doesn’t work, or rather, if you don’t provide that arg, then imageOptim still quits, and I wondered why. Not really thinking about how it should behave.

Quitting isn’t a problem, in fact as it stands, it has to quit.

Forget all this, I’ll play around with open -a

Thanks James

On 31 May 2017, at 10:12 PM, Kornel notifications@github.com wrote:

I'm not interested in implementation part. "Not quitting" can be done even easier by deleting all the qutting logic I've deliberately put in there.

I'm more interested in understanding why quitting is a problem, and how do you want ImageOptim to behave.

Note that when it's launched directly from CLI, it crates multiple copies. If you run it 20 times, you'll have 20 ImageOptim windows sitting idle. Surely, that's not a desireable behavior?

There's another alternative that already exists and works, which is to use the open -a ImageOptim command, which will find an existing running instance and add files to it. That doesn't quit automatically.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/JamieMason/ImageOptim-CLI/issues/140#issuecomment-305199184, or mute the thread https://github.com/notifications/unsubscribe-auth/ABgM_qm-wWXXjmmqLkiNaE6f-HNOGbQrks5r_XVSgaJpZM4MSMai.

jamesstout commented 7 years ago

Here's a version that doesn't quit if you don't pass the -q arg. Not sure if you want to change things at this point, but I just had to figure it out :)

https://github.com/JamieMason/ImageOptim-CLI/compare/master...jamesstout:dont-quit-when-done

JamieMason commented 6 years ago

Should be addressed by ImageOptim-CLI 2.0.0 🤞