Weird things in Finder's "explore"

foundation
finder
asobjc

(Jean Christophe Helary) #1

I’m wondering what are the “containers”, “documents files”, “files”, “folders”, “items”, that appear when I explore Finder.

I seem to get things that are not windows, and even after killing and restarting Finder (and SD) I get the same things.

Also, probably a beginner’s trivial question, but I’m seeing “desktop window” in Finder’s dictionary defined as “the desktop window”. But I can’t get any information about it.

tell application "Finder"
get properties of desktop window
end tell

returns the error:

error "Can’t get properties of desktop window." number -1728 from properties of «class dktw»

What is wrong with me?

(Mind you, I am reading Neuburg’s book right now so I will know in the future :slight_smile: )


(Shane Stanley) #2

Containers are folders, disks, the desktop-object and the trash-object, although I see these days disks are also (wrongly) being included as folders. Files consist of application files, document files, alias files, clippings and internet location files. Items is the ultimate superclass, consisting of the computer-object, containers, files, and packages.

This is all covered in the Dictionary view under the Subclasses heading. Or you can see it visually by clicking on Object Model view, then selecting Item and Inherits.

Looks like a bug in Finder —desktop window doesn’t seem to do anything. I have a vague recollection that this might be fall-out from the change in how the desktop is defined in terms of multiple screens.


(Jean Christophe Helary) #3

Shane, I know all that. But what I’m seeing under those categories seem to be “ghosts” because they are not selected itms, and don’t appear in any specific window etc. The only things that appear as real Finder items are the windows.


(Jean Christophe Helary) #4

For ex, in “containers” I have 2 items of which one is on the Desktop, and was created a while ago, and one is a directory I created yesterday in Terminal to build a piece of software. 2 totally unrelated folders, and I have no idea why only those 2 appear there. The same are in “folders”. Also, I see 5 document files, identical to the files category and again, I have no idea why only those 5 are there.


(Jean Christophe Helary) #5

Ok, that makes me feel better. At least it’s not me who doesn’t know what I’m doing :slight_smile:


(Shane Stanley) #6

I’m afraid I’m not sure what you mean. Those categories generally list items on the desktop.


(Jean Christophe Helary) #7

Wow. Yes. indeed. Everything is on the desktop… Why isn’t that documented in the dictionary?!?


(Shane Stanley) #8

I’ll take that as a rhetorical question :grinning:

My ancient printed AppleScript Finder Guide says, in part: “Any items contained by the desktop […] can be considered elements of either the desktop or of the Finder application object…”

FWIW, my general advice is that the Finder is best avoided unless you’re actually doing something with windows and arrangements, or you simply want the selection. For actual file management you’re better to use either System Events (which has its own quirks) or AppleScriptObjC. The Finder is slow and quirky.

Indeed, I believe the reason System Events has the Disk-Folder-File Suite was to encourage a move away from using the Finder for this stuff.


(Jean Christophe Helary) #9

Yeah, well, sorry for that :wink:

I was actually trying to find the size of the screen and I had found a hint where “bounds of desktop window” was the solution. So I started to investigate and found all those weird things.

Ok, well one good reason to actually check System Events…

And what about that Finder Guide you mention? Is it still relevant?


(Shane Stanley) #10

That certainly used to be the case, but it’s not working here. And specifying the value directly is also returning nonsense (I have two screens, which might make a difference).

For a single screen:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
set theRect to current application's NSScreen's mainScreen()'s frame()
set theSize to current application's {NSWidth(theRect), NSHeight(theRect)}

Or multiple screens:

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

set theSizes to {}
set theRects to current application's NSScreen's screens()'s valueForKey:"frame"
repeat with aRect in theRects
	set end of theSizes to current application's {NSWidth(aRect), NSHeight(aRect)}
end repeat

Not particularly.


(Jean Christophe Helary) #11

Excellent ! Thank you very much !


(Nigel Garvey) #12

These work for me in Leopard, El Capitan, and Sierra with just one screen attached to each computer:

tell application "Finder"
	-- Screen dimensions.
	bounds of desktop's container window
	
	-- Bounds of desktop folder's Finder window. Result depends on whether or not it's open.
	bounds of (desktop as alias)'s container window
end tell

(Shane Stanley) #13

If you have more than one screen it gives you the bounds of a bounding rectangle around all screens, with the horizontal zero-point being the left side of the window with the dock. So here, with the dock on my iMac screen and another screen to its left, I get: {-2560, 0, 2560, 1440}. Correct, but it doesn’t really tell you any screen’s actual size.


(Jean Christophe Helary) #14

On my HS machine, only the later works. And that’s certainly not what I’d try to do from just looking at the dictionary.


(Jim Underwood) #15

@ShaneStanley,

Thanks for a great script to get the screen frame of multiple screens.

I want to return a text variable like this:

0,0,2048,1152
2048,0,2048,1152

So, here is how I modified your very useful script.

Are there NS functions to get x & y coordinates?

I think it could be greatly simplified if there are. I searched and could not find any.

I did find NSStringFromRect, which was helpful. Unfortunately it puts the braces “{}” in its results.

#  AUTHOR: @ShaneStanley
#          with minor mods by @JMichaelTX

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

set theSizes to {}
set theRects to current application's NSScreen's screens()'s valueForKey:"frame"

set recList to {}

repeat with aRect in theRects
  set end of theSizes to current application's {NSWidth(aRect), NSHeight(aRect)}
  
  ## Is there a NS function to get x & y coord? ##
  
  set end of recList to current application's NSStringFromRect(aRect) as text
  
end repeat

set AppleScript's text item delimiters to linefeed
set screenList to recList as text

--- Requires Satimage.osax ---
set screenList to change {"{", "}", " "} into "" in screenList

(Shane Stanley) #16

Yes: NSMinX() and NSMinY().


(Jim Underwood) #17

Thanks. That works well. I saw those functions, but the min/max threw me off. :wink:

The script returns the screen values with a decimal point (0.0).
I want an integer value.
Is there a better way to convert?

  set oScreen to current application's {NSMinX(aRect), NSMinY(aRect), NSWidth(aRect), NSHeight(aRect)}
  
  --- Convert NSFloat to Integer ---
  repeat with aValue in oScreen
    set contents of aValue to (aValue as integer)
  end repeat

(Shane Stanley) #18

That’s because screen values are real values.

That’s probably as good as any.


(Jim Underwood) #19

Shane, OK, thanks for all your help.

Just one more question for now. (:wink)

From the array of screens returned with
set theRects to current application's NSScreen's screens()'s valueForKey:"frame"

is there a way I can identify which screen is the:

  • Main, active screen
  • Primary (internal) screen?

I know how to get the screen info for these from this thread:
How Do I Get the Bounds (frame) of the Main Display (Screen)?

What I’d like to do is list the screens with frame coord, and indicate which of each is “Main” and “Primary” (or both).

I’ve done a lot of searching on Apples’ dev site for NS stuff, and even opened Xcode and searched there, and I can’t find anything.

I did find this, but it turned out not to be of any help:

This is a dictionary containing the attributes of the receiver’s screen. For the list of keys you can use to retrieve values from the returned dictionary, see Display Device—Descriptions.

I had one hellofa time finding that, and when I did no keys to indicate Main or Primary. Or at least I didn’t see any.

Thanks again for all your help.


(Shane Stanley) #20

The first screen returned is the one whose origin is at {0.0, 0.0}, which is referred to as the primary screen. The documentation is a bit confusing because much of it dates from the time when only one screen could display a menu bar at a time.

To get the active screen, you can use the mainScreen method:

current application's NSScreen's mainScreen()'s frame()