Sunday, June 13, 2021

#1 2021-05-16 05:49:20 am

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

Little debugging in Script Editor for count windows

Here is other debugging of how Script Editor count the windows.

1, I have compare window names vs document names vs windows.plist
2, I have manually count with mission control to get correct number. (if that is the correct)

Ps. I need to put the repeat loop in try block becouse it return error without.

The strange thing the count of document is not correct, but the count I did with windows.plist is.

Funny side note, maybe I should count the object in mission control instead smile

On other hand force quit and run the script again make the count of document correct but not
the others. smile

Applescript:

use framework "Foundation"
use scripting additions

set thePath to POSIX path of (path to home folder) & "Library/Saved Application State/com.apple.ScriptEditor2.savedState/windows.plist"
set theURL to current application's |NSURL|'s fileURLWithPath:thePath

set theRecord to my convertPlistToAS:theURL isPath:true
set thePlistList to {}
repeat with anItem in theRecord
   try
       set theWindowID to (contents of anItem)'s NSWindowID
       set theWindowNumber to (contents of anItem)'s NSWindowNumber
       set end of thePlistList to {theWindowID, theWindowNumber}
   end try
end repeat

tell application "Script Editor" to set {theWinCount, theDocCount} to {count every window, count every document}
return {theWinCount, theDocCount, count thePlistList}

on convertPlistToAS:plistStringOrPath isPath:isPath
   if isPath then -- read file as data
       set theData to current application's NSData's dataWithContentsOfFile:plistStringOrPath
   else -- it's a string, convert to data
       set aString to current application's NSString's stringWithString:plistStringOrPath
       set theData to aString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
   end if
   -- convert to Cocoa object
   set {theThing, theError} to current application's NSPropertyListSerialization's propertyListWithData:theData options:0 format:(missing value) |error|:(reference)
   if theThing is missing value then error (theError's localizedDescription() as text) number -10000
   -- we don't know the class of theThing for coercion, so...
   if (theThing's isKindOfClass:(current application's NSArray)) as integer = 1 then
       return theThing as list
   else
       return item 1 of (theThing as list)
   end if
end convertPlistToAS:isPath:

Last edited by Fredrik71 (2021-05-16 06:14:08 am)


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

Offline

 

#2 2021-05-16 06:37:44 am

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

Re: Little debugging in Script Editor for count windows

Fredrik71 wrote:

The strange thing the count of document is not correct, but the count I did with windows.plist is.


Using try block, you successfully filter from windows.plist things that is not an application window in the full sense of the term window of the Script Editor application. You filter everything except the Main Menu Bar element in the plist file. And it would be necessary to filter it too.

2) When you want to get some object of the list's item, and not item itself, you can omit contents of:

Applescript:


repeat with anItem in theRecord
   try
       anItem's NSIsMainMenuBar
   on error
       try
           set theWindowID to anItem's NSWindowID
           set theWindowNumber to anItem's NSWindowNumber
           set end of thePlistList to {theWindowID, theWindowNumber}
       end try
   end try
end repeat

Last edited by KniazidisR (2021-05-16 07:40:05 am)


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

Offline

 

#3 2021-05-16 07:34:18 am

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

Re: Little debugging in Script Editor for count windows

I only like to know why Script Editor have difficulty to count the windows. Specially when a window is the core terminology of script suite. Any visibly window that a user could see on the desktop become index number. If that doesn't work some people think its a bug... and maybe its.

I do know if I do count the NSTitle in windows.plist it take almost 10 seconds before the update of
the file windows.plist. After update it give me the correct number of windows.

The code itself is not a solution for a problem its to understand it more why Script Editor behave
the way it does. And maybe some Apple employees see this and like to correct it.


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

Offline

 

#4 2021-05-16 07:45:16 am

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

Re: Little debugging in Script Editor for count windows

Fredrik71 wrote:

Any visibly window that a user could see on the desktop become index number. If that doesn't work some people think its a bug... and maybe its.


Why, it isn't visible to you Log window? It is at bottom of document window when you open it. And Script Editor indexes it when it is open (that is, exists). Don't understand what do you mean.

Last edited by KniazidisR (2021-05-16 07:46:37 am)


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

Offline

 

#5 2021-05-16 11:35:59 am

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

Re: Little debugging in Script Editor for count windows

My point is... if you open Log History and run the script to read windows.plist...
You could see after 10 seconds when the file has been updated its has NSTitle:"Log History"

Now we close Log history and run the script and wait 10 seconds to windows.plist is updated.

Now you will not find any NSTitle:"Log History"

But if you ask inside a tell block of Script Editor and ask for every window, it say you do.

So in other words thats why if you use force quit of Script Editor it will read that folders data
next time you start up... and if you now ask for every window it will be correct.

Thats why I did this research becouse I did know Script Editor behave correct after a force quit.

Last edited by Fredrik71 (2021-05-16 11:37:56 am)


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)