Need JavaScript help

Yeah, knowing what to search for is key. I still miss my cubicle neighbor at Disney who had a gift for Google searches. Even if he didn't know the subject, he just "spoke" Google.

Bookmarked. Thanks!

Dan, et al, I have just updated my JXA list:

JXA Resources

1 Like

@JMichaelTX, @ComplexPoint, @peternlewis -

I might be on to something. Iā€™m gonna take a break for a while, maybe the rest of the evening. But I thought I should report this, because one of you might have a lightbulb moment after I share this.

I was hoping I could get something to work as long as I didnā€™t have more than one copy of a file open. Nothing seemed to work. Until I tried UI Automation properties/functions/whatever.

This appears to work every time, but as I said, Iā€™m stopping for now.

(function Run() {
	'use strict';
	
var _xcode = Application("Xcode");
_xcode.activate();
delay(1);

var _systemEvents = Application("System Events");

var _xcode = _systemEvents.processes["Xcode"];
var _window = _xcode.windows[0];
return _window.title();

})()

Now, it may only work because Iā€™m using ā€œactivateā€. Havenā€™t checked that yet.

Iā€™m want to see if I can get some of the attributes shown in UI Browser. Iā€™m able to get Role, although itā€™s called ā€œAXRoleā€ in UI Browser, so donā€™t know if itā€™s the same or not. If anyone can figure out how to get other attributes, that might help also.

'Night.

.properties() gives some idea of what's accessible

(function () {
    'use strict';
    
    Application('Xcode').activate();

    var se = Application('System Events'),
        procs = se.applicationProcesses.whose({
            name: 'XCode'
        }),
        proc = procs.length ? procs[0] : undefined;

    if (proc) {
        var ws = proc.windows,
            w = ws.length ? ws[0] : undefined;
                
        return w && w.properties();    
    }
})();

Awesome!

Did you see my question about Atom? I canā€™t seem to get it to build JavaScript. I think I just configured something wrong. Any ideas?

Once you have installed the packages:

  • Script
  • languages-javascript-jxa

you can save your source with an .scpt extension

and then run it in the editor with āŒ˜I

I also use the atom-beautify package for automatic code formatting,
running my .scpt JavaScript for Automation code through the JS beautifier with:

Auto-format JXA JavaScript in Atom.kmmacros (20.3 KB)

1 Like

Awesome! Does this support debugging via Safari?

From the Script Package in Atom, you can certainly launch a JSContexts debugging session in the Safari debugger. I do this all the time with the TaskPaper 3 JSContext.

The level of access is determined, however, by the particular JSContext, rather than by Atom. Safari only seems to allow access to through Script Editor for the Automation JSContext, so at the moment when I need to debug I duplicate the source file and access the debugging session from a copy in SE.

SE is not alas, an unalloyed delight ā€“ no comment-toggling, no block indent or block outdent, no automatic JavaScript formatting, but it does crash :slight_smile:

LOL! For sure.

Do you use a macro in Atom to setup SD and run it through the debugger?

SD as in the AppleScript Script Debugger ?

Perhaps SD -> SE ?

No, I should probably take a moment to automate the flipping out to Script Editor for debugging. Havenā€™t done that.

Yeah.

Script Editor, when youā€™re debugging. The process you just explained.

As above - not yet ā€¦

OK, cool. I donā€™t use the debugger a lot, especially since it almost never helps. :slight_smile:

I find the Safari debugger very good :slight_smile:

A good perspective from which to understand the object interfaces and probe them a bit.

Oh, yeah. Itā€™s just that the things Iā€™m trying to debug, as I learn this stuff, are rarely helped by the debugger. Once Iā€™m up to snuff, then it will be helpful. I hope.

Atom question: When I comment out lines, I like to use ā€œ//ā€, and I like the slashes to stay in columns 1-2, not indented. Any way to get them to stay there? Beautify, and for that matter Pasting, wants to indent them.

The fine-tuning of Beautify (not sure if there is a setting for that) is at:

Atom > Preferences > Packages > Atom Beautify > JavaScript > Settings

Incidentally under Preferences > Packages for Script, you will find the keybindings:

1 Like

Any way to get Atom to warn me on exit if I have unsaved files?

Iā€™m such an inveterate saver that I havenā€™t looked into that.

As youā€™ve probably found, it automatically saves sessions, and reopens the same tabs, in the same state after a simple quit.