Having a KM macro ask which folders to use whenever you run it

Hi. I am new to KM and wanting to accomplish something rather advanced. KM support said what I want to do is possible with an Applescript and suggested I ask the question on the forum here.

I have written a macro in KM where there is a source folder that contains a bunch of misc files and a destination folder where the macro creates new folders — one new folder is created for each of files in the source folder with the exact same name as its corresponding file in the source.

I would like KM, each time I run the macro, to ask me using a dialogue box to define the source and destination folders.
I have no idea how to do this.
I have tinkered a very little bit with Automator before – not really Applescripts.
But I’m savvy enough that, with the proper instructions, I’m sure I could manage to get this to work if I’m pointed in the right direction.

Thanks for any help anyone can provide!

1 Like

This should do it:

The AppleScript is the same for both, except for the prompt string:

### RETURNS a POSIX path like "/Users/Shared/Dropbox/" ###

set promptStr to "Choose a FOLDER for SOURCE"
set folderPath to POSIX path of (choose folder with prompt promptStr)
return folderPath
1 Like

I can't thank you enough for helping. This is definitely what I'm looking for BUT, how do I then use those new Variables of Folder_Source and Folder_Destination in my macro?
I have attached a picture of at least what the beginning of my macro looks like with saying to Open my Source Test (that I have to manually input right now) then copying the name of the first file, and then saying to Open my Destination Test folder and continue.
I tried the "Use Variable" command but I don't see the options for Use Variable to open that folder.
I hope I am making sense.
You can not see my entire macro here but I hope it gives you a sense of what I'm trying to accomplish.
Thank you so very much for your time.

I figured out a way! I'm curious to know if this is the best way, but I used the screenshot below to set the variable(s) to text and then told the Open command to open that text

Take a look at this macro. It should do the job, and help you understand how to use the above two Actions I posted.

The key Action here is the For Each Action, which processes each file in the Source folder. Please feel free to ask any follow-up questions about the macro or the logic used.

Be sure to first test using a small number of files in the Source folder.

###MACRO: Create Folders from Files

####Download:
Create Folders from Files.kmmacros (30 KB)

1 Like

Well…my method works, but yours has blown my mind. As soon as I started demoing KM I thought something like this MUST be possible, but this is just amazing.
Thank you. Truly thank you for your help.
I’m going to digest it piece by piece, but it’s immediately making sense and works nearly instantaneously.
I like how you clear out the variables at the end of the script.
If I have any follow-up questions I surely will let you know.
You are a saint, friend.

1 Like

This action:

does nothing - it sets the variable Folder_Source to the value of the variable Folder_Source, which it already is.

The second action (Open "%Variable%Variable_Source%") is a correct way of using the variable in another action.

Hey @KMsoundguy,

JM did a good job of showing you how to mix AppleScript with Keyboard Maestro's functions.

Here's how to do it with pure AppleScript (run from Keyboard Maestro of course).

Create Folder Set from File Names.kmmacros (3.6 KB)

Since you're going to AppleScript for the choose folder function anyway, it makes sense to stay in AppleScript.

Now. Just for fun let's try that in the shell.

#!/usr/bin/env bash
sourceDIR=~/"test_directory/KM_TEST/";
destinatinDIR=~/"Downloads/Dest/";
cd "$destinatinDIR";
fileList=$('ls' -1 $sourceDIR | sed -E 's!\.[^.]+$!!; s!^!'"'"'!; s!$!'"'"'!' | paste -sd "," -);
eval mkdir {$fileList};

It's very fast.

Note: The shell script uses hard-coded paths for the source and destination folders.

-Chris

2 Likes

Chris, for AppleScript experts like you, I’m sure it does. :sunglasses:

For those of us less skilled in AppleScript, using the native KM Actions makes perfect sense, and are less prone to errors/typos. It’s also easier to make changes in KM than in a AppleScript, for most of us.

As always, many thanks for sharing another great script. Lots of good stuff in there. :+1:

1 Like

Well you’re all obviously more expert than I am in all of this so thank you guys for the awesome lessons!
And Peter, thank you for letting me know about that action that was not accomplishing anything.
Makes total sense.
I’d need a lifetime to catch up to the knowledge that all of you guys have.
But hoping with a little more practice I can use all of this, KM and Applescript, to make some previously time-consuming tasks nearly instantaneous and decrease my carpal tunnel syndrome! :stuck_out_tongue:

2 Likes

Hi fellas! I hope you will still get notifications about this thread because I need some more help.
A few things:

  1. I’m sometimes using this applescript for creating folders in a destination location using names of files/folders in source location on source files that have multiple periods (.) in the file names. The applescript seems to think everything after the first period in any file name is all that it needs and that the rest is the extension and it ignores it.
    Example:
    A FOLDER named “OneTwoThree.555.000” in the source creates a folder called only “OneTwoThree” in the destination.
    My workaround has been to create a couple of Automator workflows that first change all periods (.) in the files in the source folder to something unique and rare like “d_o_t” so it can create the full file names in the destination. Then the macro runs a reverse Automator workflow to turn all “d_o_t” instances back into periods (.) at the end of the action.
    But it would be nice to not need to do this and find a way for the applescript to use the entire file name while recognizing if there is actually an extension to be ignore like .wav or .pdf (I do NOT want true extensions to become part of the names of folders created in the destination. My workaround totally works. Would just be nice to not need it.

  2. I would love a different way to run this script so that I could make a selection in the Finder of a subset (NOT all) of files in the source folder and then run a macro/script so that only the files I have highlighted/selected get new folders with those exact names created in the desired destination folder.

  3. I’ve seen this Applescript run with errors on Mountain Lion while it seems to work fine on Mavericks and later (but this is the last and least important question as I rarely use Mountain Lion anymore.)
    Thanks for any help you might be able to provide!

Hey @KMsoundguy,

What AppleScript? Mine from post #8?

The trouble with .file-extension is that the system cannot discriminate between a file extension, and something that looks like a file extension:

 System_Profiler Notes copy.000
 System_Profiler Notes.123
 System_Profiler Notes.ext
 System_Profiler Notes.txt
 System_Profiler Notes.webarchive

Discovered “file-extensions”:

{
  "000", 
  "123", 
  "ext", 
  "txt", 
  "webarchive"
}

Since you’re using naming conventions for files that include dots (“.”), the only way you can ensure that real file-extensions are correctly removed is to filter them with a list of possible extensions.

Your item #2 does not track with the first script, since it strips file-extensions before making folders with the base-names of the files.

It’s easy enough to work with a selection in the Finder, but please be very clear about what you want to do.

In #3 you don’t identify the errors, and it’s difficult to speculate about what they might be.

The current script attempts to strip the file-extension from any given file, but it allows for the possibility that there is no extension.

It is very basic and should not fail on Mountain Lion without a real cause.

The script does not account for the possibility of duplicate folders.

For instance – in a folder with contents of:

ferrari_enzo.jpg
ferrari_enzo.txt

The script will attempt to make two folders named “ferrari_enzo”, and it will throw an error.

In sum:

Can you do what you want? Yes.

Can you do it with 1 script? Yes.

-Chris

Chris, is there some reason this would not always return the root file name?

set oFile to (choose file)


tell application "System Events" to tell disk item (oFile as text) ¬
  to set {fullfileNameStr, extStr} to {name, name extension}

if extStr is not "" then
  set rootFileNameStr to text 1 thru -((count extStr) + 2) of fullfileNameStr
else
  set rootFileNameStr to fullfileNameStr
end if

log ("File Name: " & fullfileNameStr)
log ("Root File Name: " & rootFileNameStr)

return rootFileNameStr

RESULTS:

File Name:      File.Name.with.periods.txt
Root File Name: File.Name.with.periods

I'm not sure that I correctly understand your issue, but the following KM Action will properly get the root file name, even when it contains periods:

I use this in the macro I posted above. It is not working for you?

Thanks for the reponses.

@ccstone
Attached is a zipped folder containing the current KM macro I am using for this along with the Automator workflows that change . to something else and back again. And inside of that folder is also a screenshot of the entire macro because you'll need to relink to the proper Automator workflows once you import the macro.

Also attached are two separately zipped folders as source examples using files that have lots of periods. One contains only folders that have no extensions. The other contains PDF files.

I appreciate the heads up about two files with the same name but different extensions in the source that can throw an error. However the scenarios I'm using this macro for will not ever have that be the situation so I'm not concerned about that happening but it's very good to know.

Maybe I'll boot up a 10.8.5 machine sometime soon to report the exact error I was seeing -- not a big deal but I was just curious if there was anything that could prevent it from working on an older OS X version. Low priority!

I'll try to be ver clear about how I would like to create a different macro that only creates new folders in a destination from a selection.

I want to be able to:

  1. Open "Source example 2 just folders" in the Finder and manually select ONLY some of the items inside -- like the first 4 folders.
  2. Trigger a macro that will recognize that I only want to create new folders from that selection I've made in the Finder, asks me for the location of the Destination folder, and then creates folders for only that specific selection inside of the Destination folder.

@JMichaelTX
Your macro sure does work -- BUT it doesn't work when the source IS folders and not files with extensions. So if you use my "Source example 2 just folders" with it, it will leave out the last two digits after the last period. Give it a try.

I'm not sure if this means I'd need two macros -- one to use when the source files DO have extensions and one to use when they do NOT because they are just folders (or another reason) but it would be great if it could all work in just one self-contained script.

Source example 2 just folders.zip (1.8 KB)
Source example 1 pdfs.zip (36.2 KB)
Create Folders from Files and rename dot.zip (764.4 KB)

It’s not clear what you want to happen when the Source is a folder.
Please provide details.

The source is sometimes a folder full of folders (instead of files) inside of it. Look at the attachments I uploaded.
No matter whether the source is a folder of folders or a folder of files, I still want folders to get created from the names of the files/folders in the source.

So, if the Source folder is “Source A”, and it contains sub-folders of “Source A1”, “Source A2”, “Source A3”, do you want to create new folders in the Destination Folder of just that: “Source A1”, “Source A2”, “Source A3” ?

IOW, do you want to create folders in the Destination folder for every sub-folder and file in the Source Folder?

  • For files, use the root name
  • For sub-folders, use the same name

What happens if the folder already exists in the Destination folder?

I do not care at all about the contents/sub-folders inside of the top-most folders in the “Source A” folder, no. I only want to, within the Destination folder, re-create the top-level folders within the “Source A” folder with the exact names they already have no matter how many periods there are in the name and have them thus be empty there in the Destination folder.
It would be nice, in the same script if at all possible, if the contents of the “Source A” folder happen to be actual files with extensions like .pdf that still have multiple periods in their names before the extension - be able to create folders in the Destination that do NOT use the .pdf (or other) extension.

Hey JM,

Yes. That’s why I provided a variety of “file-extensions” above (note the quotes).

.000 and .123 and .ext

Are not valid file-extensions, but the system thinks they are.

Try your script on a file named link this: Text_Template.000

-Chris