0

I don't know if the issue lies with me or with OS X. I have this AppleScript:

tell application "Caffeine"
    if active then
        turn off
    else
        turn on
    end if
end tell

I translated this to this JavaScript

caffeine = Application("Caffeine");
if (caffeine.active)
{
    caffeine.turnOff();
}
else
{
    caffeine.turnOn();
}

However caffeine.turnOn(); is never executed, no matter how often I run it. If Caffeine is active, it is turned off, otherwise nothing. The AppleScript equivalent runs. caffeine.turnOn(); and caffeine.turnOff(); by itself also run fine. I can't imagine, that JavaScript for OSA is really this broken, that even this doesn't work.

hgiesel
  • 5,430
  • 2
  • 29
  • 56

1 Answers1

1

caffeine.active might be a function, which when not called will always be truly:

var my_fn = function() {};
if (my_fn) console.log('my_fn is truly');

Call the function:

var caffeine = Application("Caffeine");
if (caffeine.active()) {
    caffeine.turnOff();
}
else {
    caffeine.turnOn();
}

A way to check it, is to simply log the value:

console.log(caffeine.active); // function() { .... }
// or using typeof
console.log(typeof caffeine.active); // "function"
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • When I say `console.log(caffeine.active)` it says `[object objectSpecifier]`, which displays the Obj-C underworkings of Apple's JS implementation. – hgiesel Jun 01 '16 at 22:32
  • @hgiesel What about `typeof caffeine.active` – Andreas Louv Jun 01 '16 at 22:42
  • Ok, this one says `function`. I guess even though Apple distinguishes between *properties* and *methods*, there seems to be no such differentiation for the JS implementation – hgiesel Jun 01 '16 at 23:13