Trigger macro with new Finder window/tab

I have a minor annoyance with Finder that is cured with a short and sweet macro. Is there a way to trigger the macro every time I open a new tab or a new Finder window?

Hey Evan,

A) What is the annoyance, and how are you working around it?

B) No. Keyboard Maestro cannot detect that sort of action in the Finder.

C) You could intercept the keyboard shortcuts for making a tab or opening a window, although that won’t cover all bases.

D) I’m fairly certain you could cover all bases by using UI Actions from PFiddlesoft, although you’d have to shell out $35.00 U.S. for the privilege. (There is a demo.)

-Chris

Hi Chris,

Thanks for the reply. The annoyance is getting at Finder’s Saved Search “Show Search Criteria” that is hidden in the Action drop down (or right click context menu of the Saved Search). I mapped a Keyboard Shortcut to the command in the Actions drop down it in System Preferences, but for whatever reason it only works after I’ve clicked the drop down with the mouse once. Then the shortcut works until that tab is closed. I actually mapped it also to “Hide Search Criteria” so the same shortcut works as a toggle nicely, but I have to once click that drop down before I can use it. Although it’s hacky, KM can click that menu for me. I’d like to tie that to the opening of a new tab.

Thanks for the suggestions.

Hey Even,

I won’t guarantee this will work on your system due to localizations and the way you have things set up, but you can find out by running it from the Applescript Editor.

It will toggle show/hide search criteria for the front Finder window if the window is showing a saved search.

  • Note that AppleScripts run faster from Keyboard Maestro when run as a script file rather than as a text script.

-Chris

--------------------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2014/08/24 22:30
# dMod: 2014/08/24 23:27 
# Appl: Finder & System Events UI-Scripting
# Task: Toggle Show/Hide Search Criteria for front Finder Window
# Tags: @Applescript, @Script, @Finder @System_Events, @UI-Scripting
# Test: OSX 10.9.4
--------------------------------------------------------------------------

set _button to false
tell application "System Events"
  set quit delay to 0
  tell application process "Finder"
    set frontmost to true
    tell front window
      if static text "Search:" of group 1 of splitter group 1 exists then
        tell toolbar 1
          set buttonList to first menu button of groups whose description is "Action"
          repeat with i in buttonList
            if (contents of i) ≠ missing value then
              set _button to first item of i
              exit repeat
            end if
          end repeat
        end tell
        if _button = false then error "Action Button not Found!"
        tell _button
          click
          tell menu 1
            if menu item "Show Search Criteria" exists then
              tell menu item "Show Search Criteria"
                if enabled = true then
                  click
                end if
              end tell
            else
              if menu item "Hide Search Criteria" exists then
                tell menu item "Hide Search Criteria"
                  if enabled = true then
                    click
                  end if
                end tell
              end if
            end if
          end tell
        end tell
      else
        beep
      end if
    end tell
  end tell
end tell

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

Wow, thanks for going to the trouble. I’m getting the “Action Button not Found!” I’ll post back if I’m able to get it working.

Hey Evan,

Send me a screenshot of a Finder window off-list (listmeister@thestoneforge.com).

I can probably tweak it if I see how you’ve set up your toolbar.

-Chris

Hey Folks,

It turns out that Evan has customized his Finder toolbar quite a bit, and System Events actually loses track of some of the accessibility titles and descriptions with his setup (surely a bug in OSX).

Here’s the script that works for him:

--------------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2014/08/24 22:30
# dMod: 2014/08/26 01:24
# Appl: Finder & System Events UI-Scripting
# Task: Toggle Show/Hide Search Criteria for front Finder Window
# Tags: @Applescript, @Script, @Finder @System_Events, @UI-Scripting
# Test: OSX 10.9.4
--------------------------------------------------------------------

tell application "System Events"
  set quit delay to 0
  tell application process "Finder"
    set frontmost to true
    if name of front window ends with ".savedSearch" then
      tell front window
        tell toolbar 1
          tell group 4
            tell menu button 1
              click
              tell menu 1
                if menu item "Show Search Criteria" exists then
                  tell menu item "Show Search Criteria"
                    if enabled = true then
                      click
                    end if
                  end tell
                else
                  if menu item "Hide Search Criteria" exists then
                    tell menu item "Hide Search Criteria"
                      if enabled = true then
                        click
                      end if
                    end tell
                  end if
                end if
              end tell
            end tell
          end tell
        end tell
      end tell
    end if
  end tell
end tell

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

If you have Xcode installed you can use the Accessibility Inspector app to suss out the UI-structure, although the far superior option is Pfiddlesoft’s commercial product UI Browser.

You can also do some detective work using AppleScript itself, although it is often tedious and frustrating work.

---------------------------------------
tell application "System Events"
  tell application process "Finder"
    UI elements
  end tell
end tell
---------------------------------------

Using this skeleton you can start working your way along the UI.

---------------------------------------
tell application "System Events"
  tell application process "Finder"
    tell window "Downloads"
      UI elements
    end tell
  end tell
end tell
---------------------------------------

In general most folks won’t want to wrestle with UI-Scripting, but as Evan can attest it provided a much smoother solution for this particular job than his previous attempt using KM alone. So it’s worth knowing a little bit about it.

-Chris

2 Likes