NSLog in console or other way to log

I’ve finally an app release (replacement of ASS app) which runs fine and fast on my machine.
I sent out to a few users to test and it doesn’t work. No crash.

Is there a way they can see something in console or another way to track what happens?

Yes, you can use NSLog, and probably even AS’s log. The catch is that they need to have Console.app running while it happens.

Thanks Shane for your continuos help!

Be careful with NSLog. You might be tempted to use something like:

set x to 3
current application's NSLog("x is %d", 3)

But that may well crash your app. The problem is although x is an AppleScript integer, in the absence of guidance from a method signature it will be bridged to an NSNumber. That means the placeholder needs to be %@, not %d.

As a rule of thumb, stick to %@, and don’t try to log objects of classes that aren’t bridged to Objective-C classes.

Thanks for the hint!

I just learned %@ is not enough. This works

set x to 3
set x_as_object to current application's NSNumber's numberWithInt:x
current application's NSLog("x is %@", x_as_object)

It used to be enough. Looking at crash reports here, I’m wondering whether moving NSLog() to be a wrapper around the newer os_log functions is the cause.


I just tried the old crashing version again:

current application's NSLog("x is %d", 3)

And now it works fine! This is on 10.14.4. So it may even be the case that someone has intentionally fixed the original “bug”. I wonder when the change was made.

I guess sticking with objects is safest, in that it should work with all versions of the OS.

If anyone running older versions of the OS wants to try, it would be interesting to track down when the change happened.

On both El Capitan 10.11.6 and Mojave 10.14.4 %d works, and %@ crashes.

At work I have High Sierra, but I’m not in the office now, and before I left I started the Mojave install… I could try Yosemite on monday…

Interesting. And yet stringWithString_ behaves quite differently:

current application's NSString's stringWithFormat_("x is %@", 3) --> (NSString) "x is 3"
current application's NSString's stringWithFormat_("x is %d", 3) --> (NSString) "x is 1584690273"

Interesting indeed. When I run “%d” on macOS I get “x is -1173781599” (on Mojave 10.14.2) and “x is -1166379287” (on Mojave 10.14.4)…

I think you’ll find the value changes each time you quit and relaunch your editor.

D’oh! Even low positive numbers.

Anyways, better not to use ObjC formatting then…

In this case, using %@ and assuming the bridge will do the conversion seems reliable enough.