0

I have an applescript which extracts the name, associated process and and other information about a target window. As of now, the script selects the target window via a "choose from list" dialog that contains all open windows from all processes. It would be much better if I could select the target window by highlighting it in the Finder. For example, this can be done for files and folders:

tell application "Finder" to set this_file to the selection

Question: Is there an equivalent method to do select windows from the Finder (using Applescript or Automator)?

2

Well you can ask Finder for its windows. The order returned is topmost to bottommost.

If you have a specific window you want, and know its name then just ask it to activate...

tell application "Finder"
    set w to (name of every window)
    set i to (id of every window)

    tell window named "Desktop" to activate

    get front window
end tell

And the results...

tell application "Finder"
    get name of every window
        --> {"Desktop", "Development", "Downloads"}
    get id of every window
        --> {16267, 16290, 16265}
    activate window "Desktop"
        --> Finder window id 16267
    get window 1
        --> Finder window id 16267
end tell

Hope that helped...

  • Thanks. This is good to know, and might be useful, but not quite what I was hoping for. As I understand it, your solution targets Finder to identify every window and allows the user to select from that list. What I current have targets System Events to accomplish the same. What I was hoping was to avoid any list…Instead, simply have the user select the window of interest via a mouse click, and input the selected/highlighted window directly in Applescript. – paamachat Aug 19 '15 at 14:57
  • So... you want a way to have your script know which window you are working on? Then it would be the top most. The first one in the list since that would be the last active window. See code edit... – Vic Aug 19 '15 at 15:08
  • Ah, of course, so just by clicking it you set it frontmost and can pick it up that way. Duh! I was too focused on a way to import the "highlighted" window similar to the way you import or pass files through Automator. Thanks! – paamachat Aug 19 '15 at 15:13
  • No prob. I would code it by showing a dialog asking to select/click on the window of your choice and then click OK to dismiss dialog. Then a get front window and you are done! :) – Vic Aug 19 '15 at 15:14
0

So here is some final code that works for all windows, Finder and otherwise. Note the 4 second delay to switch to your window of interest is required. Without it the AppleScript Editor will always be frontmost.

tell application "System Events"
display dialog "Click OK, then immediately click on the target window.  Whichever window is highlighted at  4 seconds will be captured."
delay 4
set frontApp to name of first application process whose frontmost is true
set frontWin to name of first window of (first application process whose frontmost is true)
display dialog "You selected:" & linefeed & linefeed & "Window: " & frontWin & linefeed & linefeed & "Process: " & frontApp
end tell
-- now do what you need to with this window

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .