Tuesday, May 11, 2021

#1 2021-02-06 10:05:46 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 872

Get window ID

I wondered if there is an ASObjC equivalent to the following:

Applescript:

tell application "Safari" to set windowID to id of window 1

The reason I ask is because the following fails (FSNotes is not scriptable):

Applescript:

tell application "FSNotes" to set windowID to id of window 1

The name of the app will be obtained with:

Applescript:

set activeApp to current application's NSWorkspace's sharedWorkspace()'s frontmostApplication()
set activeApp to activeApp's localizedName as text

The window ID will be used with the -l option (capture the window with windowid) of the screencapture utility.

I spent the better part of an hour on this with Google but couldn't find anything. Thanks.

Last edited by peavine (2021-02-06 10:11:25 am)


2018 Mac mini - macOS Catalina

Offline

 

#2 2021-02-06 11:23:53 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 707

Re: Get window ID

Maybe this is what you are looking for...

If I understand this correct a keyWindow property is frontmost window

Apple:
The value of this property is nil when there is no window receiving keyboard events. The property might be nil because the app’s storyboard file has not yet finished loading or when the receiver is not active.

Applescript:

use framework "AppKit"

set theTarget to current application's NSApplication's sharedApplication()'s keyWindow()
set theNumber to theTarget's windowNumber()

Reference: https://developer.apple.com/documentati … guage=objc

To get numbers of all applications

Applescript:

set theList to (current application's NSWindow's windowNumbersWithOptions:(current application's NSWindowNumberListAllApplications)) as list

Or..

Applescript:

set theList2 to (current application's NSWindow's windowNumbersWithOptions:(current application's NSWindowNumberListAllSpaces)) as list

Reference: https://developer.apple.com/documentati … guage=objc

Here is example and not fast approach to automate screencapture

Applescript:

use framework "Foundation"
use framework "AppKit"
use scripting additions

set thePath to POSIX path of (path to desktop)
set theWinList to (current application's NSWindow's windowNumbersWithOptions:(current application's NSWindowNumberListAllSpaces)) as list

repeat with i from 1 to count theWinList
   set thePic to do shell script "/usr/sbin/screencapture -l" & item i of theWinList & space & quoted form of thePath & "cap/image_" & i & ".png"
end repeat

Here is vanilla AppleScript version of doing screencapture of every Finder window

Applescript:

set thePath to POSIX path of (path to desktop)
tell application "Finder" to set winIDList to id of every window

repeat with i from 1 to count winIDList
   set thePic to do shell script "/usr/sbin/screencapture -l" & item i of winIDList & space & quoted form of thePath & "cap/image_" & i & ".png"
end repeat

Last edited by Fredrik71 (2021-02-06 02:53:46 pm)


if you are the expert, who will you call if its not your imagination.

Offline

 

#3 2021-02-06 03:27:50 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 872

Re: Get window ID

Thanks Fredrik71 but I don't think any of those will do what I want.


2018 Mac mini - macOS Catalina

Offline

 

#4 2021-02-06 03:53:42 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 707

Re: Get window ID

Your respond made me to find this for you...

https://github.com/smokris/GetWindowID

download the git... and run make it will compile a command-line tool for you.

You could do something like this, the command is GetWindowID, the first argument is Finder
and second is --list. It will output a list of every windowID. Now you could run the script
bellow to filter the output to only return the last word of every paragraph.

It should work with application that is not scriptable.

Applescript:

set thePath to POSIX path of (path to desktop) & "AppleScriptFolder/GetWindowID/"

set theData to do shell script quoted form of thePath & "GetWindowID Finder --list"

set theList to {}
repeat with i from 1 to count paragraph of theData
   set theItem to last word of paragraph i of theData
   set beginning of theList to theItem
end repeat
set result to items 3 thru -1 of theList

Last edited by Fredrik71 (2021-03-02 03:39:32 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#5 2021-02-07 11:29:44 am

Mark FX
Member
From:: UK
Registered: 2011-08-12
Posts: 118

Re: Get window ID

As you have probably figured out, you can't get the window ID of non scriptable apps from vanilla AppleScript or AppleScriptObjC code.
But you can from the CoreGraphics and CoreFoundation frameworks, using C or ObjectiveC or Swift.
You can even do it from a Python script, but unfortunately you can't use these frameworks with AppleScript.

So when the tools you're using can't get the job done, you need to make yourself a new tool.
The "GetWindowID" command line tool that "Fredrik71" has pointed you at could be adapted.

And I also have a simpler command line tool that I made a couple of years ago using Swift.
Which could be called from a do shell script command with an application pid as a parameter.

You would need to be familiar with Xcode, in order to compile and build either of the above command line tools, but let me know if you interested, and I'll post the simple Swift code which could be adapted.
As long as the forum moderators are happy for non AppleScript code postings.

Regards Mark

Last edited by Mark FX (2021-02-07 11:45:03 am)

Offline

 

#6 2021-02-07 02:02:22 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 872

Re: Get window ID

Mark. Thanks for responding to my post. It's good to know for certain that I can't get the window ID of non-scriptable apps exactly as I want.

My existing script calls screencapture in window-select mode when it encounters a non-scriptable app. This works reasonably well and I'll stick with that. Thanks again.

Last edited by peavine (2021-02-07 03:02:09 pm)


2018 Mac mini - macOS Catalina

Offline

 

#7 2021-03-01 12:20:49 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 152

Re: Get window ID

ASObjC can't do this, but JSObjC can.  Like any AppleScript, it can be run in Script Editor or on the command line with osascript -l JavaScript …

ObjC.import('CoreGraphics');
ObjC.import('Quartz');

nil      = $();
$.unwrap = ObjC.deepUnwrap.bind(ObjC),
$.bind   = ObjC.bindFunction.bind($);

$.bind('CFMakeCollectable', [ 'id', [ 'void *' ] ]);
Ref.prototype._nsObject = function () {
	return $.unwrap($.CFMakeCollectable(this));
}

const kCGWindows = $.CGWindowListCopyWindowInfo(
                   $.kCGWindowListOptionAll,
                   $.kCGWindowNull)
		    ._nsObject();

const Windows = { 
	visible      : kCGWindows.filter( w => 
	               w.kCGWindowIsOnscreen ),
		
	applications : kCGWindows.filter( w => 
	              !w.kCGWindowLayer )
};

Windows.applications;

Tested in macOS Catalina

Offline

 

#8 2021-03-01 03:29:02 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 872

Re: Get window ID

Thanks CK and KniazidisR for responding to my post.

@KniazidisR. I tested your script with 3 apps--Script Geek, FSNotes, and Soulver--and received an error message on all 3 occasions. I tested different window index numbers without success.

error "Script Geek got an error: window 1 of application \"Script Geek\" of «script» doesn’t understand the “exists” message." number -1708 from window 1 of application "Script Geek"



@CK. I pasted your script into Script Editor and received the following error message when I ran the script. This error occurred in the first line of the script, so I assume I'm doing something wrong.

Expected end of line, etc. but found unknown token.


2018 Mac mini - macOS Catalina

Offline

 

#9 2021-03-02 12:00:59 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1797

Re: Get window ID

I am deleting my post for now as it was useless. It turns out that Handbrake is scriptable, which is why my code worked with it. I was confused by the Script Debugger. It defines this application as non-scriptable.

I tested JsObjC solution of CK on my Mac. It works. Switch ScriptEditor to JavaScript language.

kCGWindowNumber is ID you look for

Last edited by KniazidisR (2021-03-02 12:32:01 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#10 2021-03-02 03:03:46 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 707

Re: Get window ID

Have anyone try my suggestion in post #4 to compile objective-c code from the link.

Clone the git and run make in home directory of the repo.
PS. YOU DO NOT NEED XCODE TO RUN MAKE

I also made a test script in post #4

Last edited by Fredrik71 (2021-03-02 03:44:24 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#11 2021-03-02 07:43:05 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 872

Re: Get window ID

@KniazidisR. I switched Script Editor to JavaScript language and CK's script worked fine. Thanks.

@Fredrik71. I appreciate your response to my post. I'd prefer not to install that utility if it can be avoided but if that's not possible I'll give it a try. Thanks again.


2018 Mac mini - macOS Catalina

Offline

 

#12 2021-03-03 01:11:44 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 707

Re: Get window ID

peavine wrote:

I'd prefer not to install that utility if it can be avoided but if that's not possible I'll give it a try. Thanks again.



Lets say you know how to build a framework, the link in post #4 could be a guide for that.
And that will answer your question in paragraph 1 of post #1

Its possible to build a framework to use ASObjC


if you are the expert, who will you call if its not your imagination.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)