How do I fix this error message?

Hi, all,

I’ve been successfully using the AppleScript listed below for quite a while, but all of a sudden it has broken with the attached error message ()

I’m running Big Sur 11.4 on a 2020 iMac 27.

I have searched the forums for “SPDislaysDataType” and “shell script retina” with no success, so I’m hoping it’s a recognizable error which someone can explain a correction for.

If this is a common issue that has been addressed many times, a suggestion about what to search for and/or how to find it would be appreciated.

Or, if anyone has suggestions as to how to improve on the script, I’m all ears. The purpose of the script is to create one Finder window of a specific size and in a specific location each time the computer reboots.

Thanks for your time,

Scott

P.S. I have a 27" monitor attached to the iMac.

set monitorWidth to (word 2 of (do shell script “system_profiler SPDisplaysDataType | grep Resolution”)) * 0.9
set monitorHeight to (word 4 of (do shell script “system_profiler SPDisplaysDataType | grep Resolution”))
delay 5
tell application “Finder”
get the number of Finder windows
set winNum to the result
if winNum is greater than 1 then
repeat until winNum is equal to 1
close Finder window winNum
set winNum to (winNum - 1)
end repeat
end if
if winNum is equal to 0 then make new Finder window
–>set bounds of Finder window 1 to {15, monitorHeight * 0.6, monitorWidth, monitorHeight}
set bounds of Finder window 1 to {730, 860, 2516, 1424}
set the target of the front Finder window to folder “V” of folder “W” of folder “X” of folder “Y” of folder “Z” of startup disk of application “Finder”
end tell

Scott. Your script works fine for me but I’m on Catalina. I suspect the issue arises from the output of system_profiler and the use of word 2 and word 4. To troubleshoot, run the following line in script editor and look at the results.

do shell script "system_profiler SPDisplaysDataType | grep Resolution"

I get the following, so word 2 and word 4 work OK. My guess is that you’ll see something different.

If the above doesn’t resolve the issue, you may want to try the following, which also works on Catalina:

delay 5 -- test different values

tell application "Finder"
	set theBounds to bounds of desktop's container window
	set monitorWidth to item 3 of theBounds
	set monitorHeight to item 4 of theBounds
	
	set winNum to the number of Finder windows
	if winNum is greater than 1 then
		repeat until winNum is equal to 1
			close Finder window winNum
			set winNum to (winNum - 1)
		end repeat
	end if
	if winNum is equal to 0 then make new Finder window
	-- set bounds of Finder window 1 to {15, monitorHeight * 0.6, monitorWidth, monitorHeight}
	set bounds of Finder window 1 to {730, 860, 2516, 1424}
	-- set the target of the front Finder window to folder "V" of folder "W" of folder "X" of folder "Y" of folder "Z" of startup disk of application "Finder"
end tell

BTW, it doesn’t appear that you use the monitor width and height in the script, because the line that would use these values is commented out. The following is an informative thread on getting screen resolution:

https://macscripter.net/viewtopic.php?pid=203998

Thanks, peavine, for the quick response! That worked perfectly.

Yes, I commented it out while trying to figure out the problem and then forgot to uncomment it. And thank you for the alternate method of coding this… I haven’t had a problem yet, but will try this out to see if it improves the operation.

Again, thank you for the assistance!

Scott

Scott. You’re welcome. I’m glad that got things working.

Just in case anyone else happens upon this thread with a similar issue, I thought I would add a quick note concerning display resolution. The system_profiler utility returns two values, which on my computer with a 4K monitor are quoted below. In contrast, desktop’s container window property returns what is effectively the UI-looks-like value. There’s no absolute right or wrong about this but, if the values differ, I would use the looks-like value when setting the bounds of a window.

It’s also worthy of note that the system_profiler utility is relatively slow. In testing with Script Geek, the system_profiler utility with grep took 180 milliseconds to run, while getting desktop’s container window property took 22 milliseconds. ASObjC’s NSScreen’s frame property took only 3 milliseconds, although this assumes that the AppKit and Foundation frameworks are in memory.

 -- requires macOS 10.0 +
use framework "AppKit"
use framework "Foundation"
use scripting additions

set screenFrame to current application's class "NSScreen"'s mainScreen()'s frame()
--> {{0.0, 0.0}, {1920.0, 1080.0}}