Get Sub-Folder Name without opening finder window?

I’d like to get the name of a sub-folder.
I can do it currently, but must open a finder window in order to do so.
Hoping there might be a way to do it ‘under the hood’.

  • the sub-folder name always changes (but the parent folder is always the same name)
  • there is always only 1 sub-folder at ‘that level’.
    thank you in advance -

This should get you started.
Enter the POSIX path of the Parent Folder in this action before you run the Macro:
For example:
~/Documents

Let us know if you have any further questions about this.


###MACRO:   @Files Get List of Folder Names in Chosen Folder @Example

~~~ VER: 1.0    2017-06-04 ~~~

####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/9/91049b93e877f43c189b6f5ab2e2850a7d395013.kmmacros">@Files Get List of Folder Names in Chosen Folder @Example.kmmacros</a> (5.2 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

<img src="/uploads/default/original/2X/c/ca0ea21caabb1c9e39b7769b3349bb9dc0c9156a.png" width="476" height="970">

---

###AppleScript To Get Sub-Folder List
```applescript
--- GET PARENT FOLDER FROM KM VAR ---

tell application "Keyboard Maestro Engine" to set parentFolderPath to getvariable "FGL__ParentFolder"

--- IF KM VAR DOES NOT EXIST, ASK USER ---
if (parentFolderPath = "") then
  tell application "SystemUIServer" 
    set parentFolderAlias to (choose folder with prompt "Choose PARENT Folder")
  end tell
else
  set parentFolderAlias to my makeAlias(parentFolderPath)
end if

--- GET LIST OF SUB-FOLDERS ---

tell application "Finder"
  set folderList to name of every folder of parentFolderAlias ## as alias list  
end tell

--- RETURN TO KM AS TEXT LIST ---

set AppleScript's text item delimiters to linefeed
return folderList as text

--~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~

on makeAlias(pAnyPath) -- @Path @Alias
  
  if class of pAnyPath is alias then
    set itemAlias to pAnyPath
    
  else if class of pAnyPath is text then
    
    if pAnyPath contains ":" then
      set pAnyPath to POSIX path of pAnyPath
    else if pAnyPath is "~" or pAnyPath is "~/" then
      set pAnyPath to POSIX path of (path to home folder)
    else if pAnyPath starts with "~/" then
      set pAnyPath to (POSIX path of (path to home folder)) & text 3 thru -1 of pAnyPath
    else if pAnyPath starts with "/" then
      # Nothing
    end if
    
    set itemAlias to alias POSIX file pAnyPath
    
  else if class of pAnyPath is «class furl» then
    set itemAlias to pAnyPath as alias
    
  end if
  
  return itemAlias
  
end makeAlias
-------------------------------------------------------------------------------------------

```
1 Like

Hey Guys,

In general it’s better to avoid Finder-references completely.

If you know you’ll always have ONLY 1 subfolder then this is a bit more efficient:

set folderAlias to alias ((path to downloads folder as text) & "temp:")

tell application "Finder"
   set subFolderPath to folders of folderAlias as text
end tell

I recommend never using the SystemUIServer for this purpose.

tell application "SystemUIServer" 
    set parentFolderAlias to (choose folder with prompt "Choose PARENT Folder")
  end tell

You leave the user HANGING in it, and they have to click out or or switch apps to get back to where they started.

Since there’s no indication to the user that they’ve switched apps (to the SUIS) this can be very disconcerting.

The best way to handle this is to work with the frontmost application like so:

set frontApp to path to frontmost application as text

tell application frontApp
   set myFolder to choose folder
end tell

This method will drop the user right back where they started and avoid confusion.

-Chris

1 Like

Chris, I just tested this (again), and I am not seeing the behavior you describe. Using SystemUIServer is working fine for me in a KM macro in every app I've tested running macOS 10.11.6.

For example, in Evernote, I started typing a few words, triggered the macro, and the immediately hit CMD-V to paste the results. Worked fine.

At no point was I left "hanging". After choosing the folder, I was immediately returned to the app.

Could this be a difference between El Capitan and Sierra?

BTW, I only use SystemUIServer when I'm not in a tell block of another app.

However, on further testing, I did notice this difference:

  • Using SystemUIServer did NOT show my Default Folder X add-on
  • Using frontmost application did show it.

So that is definitely one advantage of using frontmost application.

set folderAlias to alias ((path to downloads folder as text) & "temp:")

tell application "Finder"
   set subFolderPath to folders of folderAlias as text
end tell

Chris, I'm a bit confused here. How are you avoiding "Finder-references completely"?

I don't see any material difference between your script and mine (from above). I don't see how your script is more efficient.

--- GET LIST OF SUB-FOLDERS ---

tell application "Finder"
  set folderList to name of every folder of parentFolderAlias ## as alias list  
end tell

except that I am returning just the sub-folder name, as requested by the OP.

Hey JM,

You're apparently getting away with this, because you're not activating the SIUS.

Yes.

Using the SUIS also hangs a big honking dialog in the entires user-space.

When using the front-application you can hide the dialog along with the app if needed.

-Chris

Hey JM,

I missed the fact that you're returning the NAME of the folder.

tell application "Finder"
  set folderList to name of every folder of parentFolderAlias ## as alias list  
end tell

That's what Troy says he wants in post #1, although I'm not sure that's what he needs.

Troy?

Do you need the name of the sub-folder or a reference TO it?

-Chris

Looking to get the name in order to build a variable which includes the name
To be honest, I don’t know the difference between the name and the ‘reference’ to it.

File or Folder "Name" -- the last item in the path as in:
~/Documents/MyFolder
Name: "MyFolder"
~/Documents/MyFile.txt
Name: "MyFile.txt"

File or Folder "Reference"

  • either the full path to the item, or, in a script, an object reference to the item.
  • Is required to operate on the item, like move or rename.
  • KM requires the POSIX path to the file/folder
    • ~/Documents/MyFolder

If you just want the Folder Name to use in a KM Variable that will NOT be used to operate on the Folder, then the Name is all you need.
If you plan to operate on the folder, then you need the POSIX path (a reference to the folder).

Make sense?

ah yes, of course, thought it was deeper than that, =) thank you.
I do already have the POSIX path to the ‘parent’ folder so I have hard wired that into the variable that I make with the sub-folder name that you helped me to get.
So the answer to Chris’ question would be that I was looking for the name of the folder, since I already had the path to it’s parent.