Open 3 Finder windows with KM Macro or AppleScript

Open 3 Finder windows for autoconverting images Macro (v10.2)

The problem: although I set a Pause of 2.5 sec between the 3 tasks of opening 3 Finder windows, the macro ends up opening the last one.

I understand that this could done with KM pure, -
but now I got this AppleScript, and wanted to use it.

/
with best regards, Omar KN, Sweden

Open 3 Finder windows for autoconverting images.kmmacros (6.2 KB)

Probably because you aren't opening more Finder windows, instead you are changing the target of -- what is shown in -- a single window.

Have a look at the Finder's AppleScript dictionary, particularly the make verb (command) and the Finder window class and see if you can open three Finder windows, each showing the appropriate directory.

Hey Omar,

Since you're wanting to use AppleScript anyway, you might as well consolidate everything.

  • Note that I've made the convertPosixPathToAlias() handler use the expandTildePath() handler, so you only have to call the former to do both jobs.
  • Now you don't have to fool around with pauses.
  • You'll have to change the path strings and bounds as needed for your system.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2023/01/23 20:19
# dMod: 2023/01/23 20:19 
# Appl: Finder, System Events
# Task: Open the designated Finder windows and resize them.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Finder, @System_Events, @Open, @Windows, @Resize
--------------------------------------------------------

set savedSearchRef to convertPosixPathToAlias("~/Library/Saved Searches/Tag ⇱ Working.savedSearch")
set autoconvertedPicsRef to convertPosixPathToAlias("~/LC3b-wout-Movies/_Pictures-LC3/_autoconverted pics")
set autoconvertRef to convertPosixPathToAlias("~/Desktop/_Autoconvert")

tell application "Finder"
   
   activate
   
   open savedSearchRef
   set savedSearchWindowName to savedSearchRef's name
   set bounds of window savedSearchWindowName to {0, 23, 534, 900}
   
   open autoconvertedPicsRef
   set autoconvertedPicsName to autoconvertedPicsRef's name
   set bounds of window autoconvertedPicsName to {453, 23, 987, 900}
   
   open autoconvertRef
   set autoconvertName to autoconvertRef's name
   set bounds of window autoconvertName to {906, 23, 1440, 900}
   
end tell

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
# Automatically uses expandTildePath() if needed.
on convertPosixPathToAlias(posixPath)
   set posixPath to expandTildePath(posixPath)
   return alias POSIX file posixPath
end convertPosixPathToAlias
--------------------------------------------------------
on expandTildePath(tildePath)
   tell application "System Events"
      return POSIX path of disk item tildePath
   end tell
end expandTildePath
--------------------------------------------------------

Hi Chris,

There is a small problem with most of the bounds:

This row set bounds of window autoconvertName to {906, 23, 1440, 900}
has W = 1440, but on my machine both windows (the 2nd and the 3rd) have the same width 535 (should have been 987 and 1440.)

I assume that the 4 values for bounds are L Top W Height.
When I changed the bounds
like this

set bounds of window savedSearchWindowName to {71, 47, 1209, 1159}
	
set bounds of window autoconvertedPicsName to {1279, 23, 916, 308}

set bounds of window autoconvertName to {1279, 333, 916, 308}

The 1st savedSearchWindow
has correct Left & Top margins, almost correct Width and Height

The 2nd and 3rd windows move down to the bottom - left of the screen.

I leave it to you Chris if you want to delve into/ continue with it, other forum members probably have more pressing issues than this one, and there is always KM pure :slight_smile:

/

with best regards, Omar KN, Stockholm, Sweden

You know how to get window bounds yourself by now – yes?

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2023/01/24 15:56
# dMod: 2023/01/24 15:56 
# Appl: Finder
# Task: Copy the AppleScript Bounds Property of the Front Window to the Clipboard.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder
--------------------------------------------------------

tell application "Finder"
   set winBounds to bounds of front window
end tell

try
   winBounds / 0
on error winBoundsText
   set AppleScript's text item delimiters to {"{", "}"}
   set winBoundsText to "{" & (text item 2 of winBoundsText) & "}"
   set the clipboard to winBoundsText
   return winBoundsText
end try

--------------------------------------------------------
1 Like

If you want to get all window bounds at once you can run this from Script Debugger or Apple's decrepit Script Editor:

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2023/01/24 15:56
# dMod: 2023/01/24 16:06
# Appl: Finder
# Task: Copy the AppleScript Bounds Property of Every Window to the Clipboard.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder
--------------------------------------------------------

tell application "Finder"
   set winNameList to name of windows
   set boundsList to bounds of windows
end tell

set collatorList to {}

set _cntr to 0
repeat with i in winNameList
   set _cntr to _cntr + 1
   set (contents of i) to {(contents of i), (item _cntr of boundsList)}
end repeat

return winNameList

--------------------------------------------------------

But the output will be normal AppleScript output – I'm not going to take the time to convert this one into text right now.

Thank you Chris for the 2 scripts.

The reason why there was some confusion about the Finder bounds on my side is because of the different ways of AppleScript and KM measure the Right Edge and Bottom of a Window.

KM uses Width and Height whereas AS uses Distance from the Left and Distance from the top.

1 Like

Take a look at the macro I emailed you – it's more comprehensive.

This script which results in a nice "copyable list of Finder Window names and bounds in a KM Text Window", is the best!!

It will be of good use for many, I'm sure.

/

with best regards, Omar KN, Stockholm, Sweden

I'm still working on the macro, so please don't post it.

--
Take Care,
Chris

I had not read your email.

/

This script does it all:

# Auth: Christopher Stone
# dCre: 2023/01/24 15:56
# dMod: 2023/01/24 16:06
# Appl: Finder, AppleScriptObjC
# Task: Display the Name and Bounds of Every Finder Window.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @ASObjC
--------------------------------------------------------
use AppleScript version "2.4" --» Yosemite or later
use framework "Foundation"
use scripting additions
--------------------------------------------------------

tell application "Finder"
	set winNameList to name of windows
	set boundsList to bounds of windows
end tell

set _cntr to 0
repeat with i in winNameList
	set _cntr to _cntr + 1
	set (contents of i) to {(contents of i), (item _cntr of boundsList)}
end repeat

set theJSON to its convertASToJSON:winNameList saveTo:(missing value)

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on convertASToJSON:someASThing saveTo:posixPath
	--convert to JSON data
	set {theData, theError} to current application's NSJSONSerialization's dataWithJSONObject:someASThing options:0 |error|:(reference)
	if theData is missing value then error (theError's localizedDescription() as text) number -10000
	if posixPath is missing value then -- return string
		-- convert data to a UTF8 string
		set someString to current application's NSString's alloc()'s initWithData:theData encoding:(current application's NSUTF8StringEncoding)
		return someString as text
	else
		-- write data to file
		theData's writeToFile:posixPath atomically:true
		return result as boolean -- returns false if save failed
	end if
end convertASToJSON:saveTo:
--------------------------------------------------------

Yes, but the output presentation is uncouth, so that's why I reformatted it in the macro – nor did I want you to post it...

--
Take Care,
Chris

Alright. I'll take it off.
Which Macro replaced it?

/bye

I sent it to you in a private email night before last:

From: Christopher Stone <****@****>
Subject: Keyboard Maestro 10.2 “AppleScript – Get Bounds of all Finder Windows v1.00” Macro
   Date: Tue, 24 Jan 2023 20:13:07 -0600
     To: Omar ****** <****@****>

Yes , I had already used the AppleScript in the Debugger, but this latest update is very neat and ‘good-looking’.

  1. Keyboard Maestro 10.2 “AppleScript – Get Bounds of all Finder Windows v1.00

With the results (example):

--------------------------------------------------------
Window_Name                   Window_Bounds
--------------------------------------------------------

Downloads                     {51,29,1204,1171}
Latest-Image_SMF.savedSearch  {520,332,1729,1339}
_Autoconvert                  {1279,23,2195,331}

Then the Window Discovery Tool, would be for someone who needs the window dimensions of the Finder or any other app together with KM Window measurement (height , width).

We have the older macro ( 2023/01/23) : Task: Open the designated Finder windows and resize them.

(My name: Open 3 Finder windows for 
_AS)

Which opens 3 Finder windows and lays them out at their dedicated positions.

/okn

(I’ll wait with uploading to the Macro library.)