Extending the UI of an existing application.

I would appreciate if the response was in JXA, but Applescript would at least give me an idea of what calls I need to make.

I have already seen some examples of creating buttons with windows:

So I would assume my code would be something like this:

ObjC.import("Cocoa");

//If no superclass is provided, NSObject is subclassed.
ObjC.registerSubclass({
	name:"AppDelegate",
	methods:{
		"btnClickHandler:":{
			types:["void",["id"]],
			implementation:function(sender){
				//My button implementation
			}
		}
	}
});

var window=Application('Some Application').window[0]

var appDelegate=$.AppDelegate.alloc.init;

var btn=$.NSButton.alloc.initWithFrame($.NSMakeRect(25,(height-100),200,25));
btn.title="Run JXA";
btn.bezelStyle=$.NSRoundedBezelStyle;
btn.buttonType=$.NSMomentaryLightButton;
//NOTE:See NSButton docs for info on target/action
btn.target=appDelegate;
btn.action="btnClickHandler:";
btn.keyEquivalent="\r"; //Enterkey

window.contentView.addSubview(btn);

However I have very little experience with Applescript or Objective C, so am wondering whether I am on the right track (or even if what I’m trying to do is possible!)

Thanks,
~Sancarn

I’m rusty on JXA, and I’m not sure where you’re getting your window from. But unless you own the window, it’s not going to work. I doubt that you need the AppDelegate stuff – the main thing is to make the script the target of the button.

Gosh darn it! I was hoping you could get the window from the processes class:

Processes('ScreenFlow').windows[0]

If I need to own the window… I guess the only option would be to have an invisible window made in AS with visible UI elements which tracks the size of the window (and controls) of the other application?

Hmm… I also remember that F-Script, after being injected into applications, created it’s own menu item, so perhaps you can inject menu items into other applications with ASObjC/JXA?

That’s fine, If I can get my hands on some AS code I can hopefully port it :slight_smile:

I hope not. It would be a massive security hole.

I’m not entirely sure why it would be a security issue? As far as I am aware, software developers shouldn’t have to concern themselves with security… An applescript by itself is a security risk after all…

Surely it would be nothing but a good thing for scripters? One of the most important things, in my mind, when developing scripts is to make them easy for users to use… Adding menu items for instance is a great way to achieve this - equivalently adding window elements.

It’s one of the reasons why I love HTML/JS. Because it’s easy to take someones website and just add features to it… (With JS script injection I mean)

Anyway:

Extending the UI of finder can be seen here:
https://pmougin.wordpress.com/2009/10/28/ultimate-proof-that-the-finder-is-now-a-cocoa-application/

F-Script Anywhere:
https://github.com/adamnemecek/FScriptAnywhere

F-Script framework:
https://github.com/Kentzo/F-Script/releases