Monday, October 26, 2020

#1 2020-09-26 12:49:46 am

Neophyte
Member
From:: Canberra, ACT, Australia
Registered: 2017-08-20
Posts: 68
Website

Problem in Big Sur with Finder object ?

I have a simple code block which works when run with Script Editor in Mojave:

Applescript:

tell application "Finder"
   set window_bounds to bounds of window of desktop
   set screen_width to item 3 of window_bounds as string
   set screen_height to item 4 of window_bounds as string
end tell
display dialog screen_width & return & screen_height

A user of my applet has reported to me that they get an error when they run that code in Big Sur:

error "Finder got an error: Can’t get bounds of window of desktop." number -1728 from bounds of window of desktop

I have much the same code in my applet which gives them the same error.  They are running macOS version 11.0 Beta (20A5374i).  My iMac can't run Big Sur so I can't verify the error.

Does anyone know of a change in Big Sur or another reason why that user would be getting the error ?

Thanks.

Last edited by Neophyte (2020-09-26 12:51:49 am)


Public servants are people too

iMac (late 2009), 27", 2.66GHz i5, 12GB, 1TB SSD, HD4850
macOS Mojave, 10.14.6

Offline

 

#2 2020-10-01 03:54:50 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5299

Re: Problem in Big Sur with Finder object ?

Hi Neophyte.

I currently have no intention of installing anything beyond Mojave, so I can't test your code in context.

I think 'window of desktop' is a relic from the past which has just happened to work up till now as a way of addressing the desktop screen. Looking at the Finder's dictionary, neither the desktop object nor the container class from which it inherits has a 'window' property. It does have a 'container window' property which produces the same result as your code in Mojave. It may be worth trying that as a first step.

For the past three years, I've been using an ASObjC method to get the main screen's dimensions:

Applescript:

use AppleScript version "2.4" -- Mac OS 10.10 (Yosemite) or later.
use framework "Foundation"
use framework "AppKit"
use scripting additions

set screenFrame to current application's class "NSScreen"'s mainScreen()'s frame()

But even this is problematic. Before Mojave, it returned an AppleScript record which looked like this:
{origin:{x:0.0, y:0.0}, |size|:{width:2560.0, height:1440.0}}.

In Mojave (and presumably later), it returns a list which looks like this:
{{0.0, 0.0}, {2560.0, 1440.0}}

I've only needed the screen height and I've been able to get this on both my Mojave and El Capitan machines by adding the following line to the code above:

Applescript:

set screenHeight to current application's NSHeight(screenFrame) as integer

This works whether screenFrame is a list or a record — or did until last week! Since the latest Mojave security update, invoking the NSHeight() function has caused the script to quit silently — but only when it's run directly from the system script menu. It still works perfectly when run in a script editor or as an application in its own right and another script using a library containing exactly the same code has no problem when run from the script menu. Don't ask me if this is a bug or another security precaution! Rather than turn the problem script into an application, I've replaced the NSHeight() line with good old vanilla AS:

Applescript:

if (screenFrame's class is record) then
   set screenHeight to screenFrame's |size|'s height as integer
else if (screenFrame's class is list) then
   set screenHeight (item 2 of item 2 of screenFrame) as integer
end if


NG

Offline

 

#3 2020-10-01 05:15:31 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6471

Re: Problem in Big Sur with Finder object ?

Nigel Garvey wrote:

Since the latest Mojave security update, invoking the NSHeight() function has caused the script to quit silently — but only when it's run directly from the system script menu.



FWIW, it doesn't seem to happen in Catalina, but it does in Big Sur beta. And it happens there with all the functions related to structs I've tested (eg, NSMakeRect()), and in editors as well.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#4 2020-10-02 02:40:15 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5299

Re: Problem in Big Sur with Finder object ?

That said, there's been a "supplemental" 10.14.6 update this evening which appears to have fixed the issue in Mojave. frame() still returns a list, but NSHeight() doesn't crash the script reading it when the script's run from the script menu. However I think I'll stick with the vanilla alternative for now.  wink


NG

Offline

 

#5 2020-10-02 05:27:01 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6471

Re: Problem in Big Sur with Finder object ?

And it looks like it's fixed in the latest Big Sur beta, too.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#6 2020-10-03 10:03:08 am

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

Re: Problem in Big Sur with Finder object ?

Nigel Garvey wrote:

I think 'window of desktop' is a relic from the past which has just happened to work up till now as a way of addressing the desktop screen.


For some time now I've been using "bounds of desktop's window" to get screen dimensions and I hope they continue this in Big Sur or replace it with something similar.. The Finder dictionary defines various window classes including desktop window but it returns no properties. The alternatives appear to be ASObjC or the system_profiler shell command but both of these are less desirable IMO in a simple AppleScript.

Last edited by peavine (2020-10-03 01:20:20 pm)


2018 Mac mini - macOS Catalina

Offline

 

#7 2020-10-03 02:15:17 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5299

Re: Problem in Big Sur with Finder object ?

peavine wrote:

The Finder dictionary defines various window classes including desktop window but it returns no properties.


Interestingly, though, in Mojave, if you get the properties of 'window of desktop', the class shown is 'desktop window'. However not all the other properties match those shown for a 'desktop window' in the Finder's dictionary and the 'target' property is a only property of the 'properties' list, not of the window itself! Both the Finder's scripting implementation and its dictionary are long overdue for an overhaul — that is if it's being kept in Big Sur.  hmm

Applescript:

tell application "Finder"
   properties of window of desktop
   --> {class:desktop window, id:45, name:"", position:{0, 0}, bounds:{0, 0, 2560, 1440}, index:missing value, zoomed:false, closeable:false, titled:false, floating:false, modal:false, resizable:false, zoomable:false, visible:true, collapsed:false, target:desktop of application "Finder", current view:icon view, icon view options:icon view options of window of desktop of application "Finder"}
   target of (get properties of window of desktop)
   --> desktop of application "Finder"
   target of (get window of desktop)
   --> error "Finder got an error: Can’t get target of window of desktop." number -1728 from target of window of desktop
end tell


NG

Offline

 

#8 2020-10-03 08:54:02 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6471

Re: Problem in Big Sur with Finder object ?

peavine wrote:

For some time now I've been using "bounds of desktop's window" to get screen dimensions



The fact that it works is arguably a bug. You're asking for the bounds of whatever is returned by the desktop-object's window property, but the desktop-object has no such property. Using "desktop's window" is not the same as "desktop window" -- the latter class seems completely broken (at least it does here).

According to the dictionary, you should be able to use "bounds of desktop's container window", and that does seem to work.

But both versions are useless if you have more than one screen.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#9 2020-10-04 07:02:57 am

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

Re: Problem in Big Sur with Finder object ?

Thanks Shane and Nigel. I tested my scripts with "bounds of desktop's container window" and with "bounds of container window of desktop" under Catalina. Both worked fine. I'm going to upgrade to Big Sur when released and will retest then.

Last edited by peavine (2020-10-06 06:50:47 am)


2018 Mac mini - macOS Catalina

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)