Choose Folder(s)

UPDATED TO VER 0.2

Section on Custom Plugins in KM Help File:

(note that to update a plug in action you must manually remove it from the ~/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions folder before re-installing it)

Choose Folder(s).zip (10.9 KB)

Custom Keyboard Maestro Plug-in

NAME

  • Choose Folder(s)

VERSION

  • 0.2
  • Adds custom prompt text

SYNOPSIS

  • Launches an OS X Folder chooser
  • Returns a list of Posix file paths

REQUIREMENTS

  • Yosemite
    • The core script chooseFolders.sh is mainly written in Javascript for Applications

OPTIONS

  • Dialog prompt
  • Initial Folder displayed
    • Popup presets
      • Folder in Active Finder Window
      • Desktop
      • Home
      • Downloads
      • Music
      • Pictures
    • Custom path
      • (posix string or bash $Variable)
  • Multiple selections allowed (checkbox)
  • Output format
    • Unquoted lines
    • Single-quoted lines
    • JSON array string

INSTALLATION

  • Drag the .zip file onto the Keyboard Maestro icon in the OS X toolbar.
  • (if updating a previous version of the action, first manually remove the previous copy from the custom actions folder)
    • ~/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions

CONTACT

4 Likes

Is it possible to combine this plugin with the “choose file” plugin so that one can copy files and/or folders using the same window?

It would take a bit of work – wrapping NSOpenPanel.

Tyler Gaw’s example at https://github.com/tylergaw/js-osx-app-examples would be a starting point, but probably not something I will get to soon myself.

(The existing actions wrap the built-in chooseFolder and chooseFile of osascript, which don’t, I think, have the flexibility that you are after)

Thanks. For now, I will stick to the user prompt to choose between copying files and copying folders.

Updated versions of this (ver 0.2 above) and the Choose File(s) custom plugin

(enabling, in each case, a custom prompt text in the Choose dialog)

This is very cool, thanks!

A couple issues:

  1. It doesn’t seem to auto-focus the “Choose Folder” window. That means you have to reach for the mouse (and click on the “Choose Folder” window to focus it) before you can get back to the keyboard. Which we all love :slight_smile:. Even pressing the esc key, to exit the window, won’t work until it’s first focused.

  2. It doesn’t work with Default Folder 5.x. IOW: The type of window that’s opened is not recognized by DF, and the DF window chrome and its other features are unavailable. The best of which is “Enable Finder Click” which allows you to change the current folder (in the Open/Save dialog) to Finder window you click on. That feature would match very well with this plugin.

Before finding this plugin (and all of its clever features), I had been using the following:

tell application "Keyboard Maestro Engine"
	activate
	set dir to POSIX path of (choose folder with prompt "Choose Folder")
	--return the POSIX path of dir
end tell

I still use this though, because of the keyboard and DF issues above.

I hope this helps.

Thanks.

Hey @ar-km,

Using your method leaves the user hanging in the Keyboard Maestro Engine and does not return them to the app they were working in.

Try this:

tell application (path to frontmost application as text)
   set dir to POSIX path of (choose folder with prompt "Choose Folder")
end tell

-Chris

1 Like

Very sorry for this late reply @ccstone

Using your method leaves the user hanging in the Keyboard Maestro Engine and does not return them to the app they were working in.

Ahh!
That's a good point + thanks for the fix.

One problem though: DF 5.x has a problem when the Finder is the frontmost application. It locks up the entire Open/Save window and you have to esc from it.

It has something to do with AppleScript and AppleEvents running on the main event loop.

From Jon (DF author):

I think the issue is that the Finder can't respond to Default Folder X's queries and run the file dialog at the same time.

AppleScript is implemented with AppleEvents, and AppleEvents are processed by an application's main event loop. There's only one main event loop in an app, and the AppleScript handling isn't reentrant, so it'll just lock if AppleScript is called externally while an AppleScript is already executing (which is essentially what's happening).

It was his suggestion to use tell application "Keyboard Maestro Engine". But he couldn't have foreseen what you pointed out.

Given all of this, I'm kinda chasing my tail.
Here are the outcomes via each method:

  1. If you use tell application (path to frontmost application as text) within:

    • Finder = Broken (because of DF and/or AppleScript/AppleEvents)
    • Any other app = OK
  2. If you use tell application "Keyboard Maestro Engine" within:

    • Finder = OK
    • Any other app = Focus is stuck inside Keyboard Maestro Engine.

In my case, I've only been calling this from the Finder. So I could live with #2. But that wouldn't be good for this plugin or manually in a custom macro.

My first thought, as a workaround, would be to:

  • Use Method #2 but replicate the "path to frontmost application" logic directly in the plugin or macro (not via AS).
  • Then use that to switch focus back to the original app at the end of the plugin/macro.

Hey @ar-km,

Ah, I hadn’t noticed the issue with the Finder.

Try this:

set frontApp to (path to frontmost application as text)

if frontApp does not end with "Finder.app:" then

   tell application frontApp
      set dir to POSIX path of (choose folder with prompt "Choose Folder")
   end tell

else

   tell application "System Events"
      activate
      set dir to POSIX path of (choose folder with prompt "Choose Folder")
      set frontmost of application process "Finder" to true
   end tell

end if

-Chris

3 Likes