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.
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.
Ok, that makes me feel better. At least it’s not me who doesn’t know what I’m doing
I’m afraid I’m not sure what you mean. Those categories generally list items on the desktop.
Wow. Yes. indeed. Everything is on the desktop… Why isn’t that documented in the dictionary?!?
I’ll take that as a rhetorical question
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.
Yeah, well, sorry for that
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?
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.
Excellent ! Thank you very much !
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
bounds of desktop’s container window
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.
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.
Or multiple screens:
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
Are there NS functions to get x & y coordinates?
Yes: NSMinX()
and NSMinY()
.
Yes: NSMinX() and NSMinY().
Thanks. That works well. I saw those functions, but the min/max threw me off.
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
The script returns the screen values with a decimal point
That’s because screen values are real values.
Is there a better way to convert?
That’s probably as good as any.
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.
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()
To get the active screen, you can use the mainScreen method:
Yep, I knew that from the other thread.
So, my question is, from the list returned, how can I identify the active screen?
I don’t know that you can — you probably need to call mainScreen
separately. Perhaps:
set theRects to current application's NSScreen's screens()'s valueForKey:"frame"
set mainRect to current application's NSScreen's mainScreen()'s frame()
set rectsLessMain to theRects's mutableCopy()
rectsLessMain's removeObject:(current application's NSValue's valueWithRect:mainRect)