Having a Regex issue I’d appreciate some help with.
Short version: where a text line contains more than one delimiter (eg. :), I want to return the text between each delimiter using Regex. However, I can’t seem to get any search(es) working!
Longer version:
I often have to create folder trees for new projects, and have a macro for this. However, it currently uses a Switch action to set variables for the project’s root folder and the required folder tree. I’m trying to rewrite it with all the relevant data included in a text dictionary - the idea being the project type is chosen from a list prompt, and this populates the other values.
Take, for example, the line Blackpink:/Volumes/Woot:/boombayah. I’d like a Regex to initially return Blackpink (the type of project) as %Local__Choice%; and, from there:
Return /Volumes/Woot (the project type's root folder) as instance_ProjectFormatPath
Separately, return /boombayah (the link to the file containing the desired folder tree structure) as instance_ProjectFolderListPath
^([^:]+): returns Blackpink to %Local__Choice%. However, from there I’m stumped:
%Local__Choice%:([^:]+) returns /Volumes/Woot//boombayah (note: it’s replacing : with /)
%Local__Choice%:(.*):.* returns what would be /Volumes/Woot, except... from an entirely different line
And no form of any Regex I write will return /boombayah on its own
Am I asking too much of Regex here?? If not (I don’t *think* I am, but I stand to be corrected) - what expressions can I use that will return what I'm after?
^ → find from the start (.*?): → capture group #1, all characters from start to first colon (.\*?): → capture group #2, all characters between first and second colons (.*)$ → capture group #3, all characters after the second colon to the end of the line
Then just name each variable in the output section of the Regex Search action, and you're done. There are definitely other ways to do this, but this is how I'd do it.
Thanks for this. How would these work with list prompts? My existing dictionary-based list prompts have only one delimiter (I can’t find it anymore, but they’re adapted from a Favorite Finder Folders macro that was posted here a while back). So, say the dictionary is
Blackpink:/Volumes/Woot
ITZY:/Volumes/ICY
Aespa:/Volumes/Kwangya
etc., the macro will:
take the first part of each line in the dictionary file (Blackpink, ITZY, Aespa etc) and format these into a list, using the Regex query (.*):.*
present the resulting list in a Prompt With List action
set the value of %Local__Choice% to the chosen item from the list prompt (eg. Blackpink)
search for the line starting with %Local__Choice%
return the value after the delimiter (`/Volumes/Woot`) using a second Regex (% Local__Choice%:(.*))
so, choosing Blackpink from the list returns /Volumes/Woot as the file path.
I’d like to replicate that behaviour here - just with adding a second delimiter and string after it, which is returned as its own value (to specify the folder tree).
Also, it doesn’t need to be a single Regex - nor should it, I expect!
I'm not sure I fully understand what you're describing, but because you can use __ to separate a return value from the display value in prompt with list, couldn't you just format your list items like this:
Then when the user selects "Blackpink," your variable (%local_theResult% or whatever) holds the full related string. Then you can use either of the methods above to split it like you want it split.
...using the regex "Search and Replace" to swap the : for __ and, importantly, to reverse the order so the "value" is first and the "friendly label" second.
To include further results for each choice you include a splittable string as the "value" -- the trick is getting the Prompt's list in the correct format:
That is considerably easier than what I've been using for these list prompts! Which for context is this, cribbed from the aforementioned Favorite Finder Folders macro (which I wish I could find now):
Thank you all - I've now got this mostly working (hurrah!) and without Switch actions (double hurrah!). I've gone with @Nige_S 's method in the end.
Only issue I'm now facing is, where a to-be-created folder contains an ICU date token (eg. [1] Scripts/[1] Latest Draft/%ICUDateTime%yy-MM-dd%/[1] FULL*), it's creating folders with the unprocessed token rather than converting it to the desired date format. It's doing this even after adding a(nother) Regex to replace the token (as a test to ensure there's at least some active processing going on).
This is only when getting the folder tree structure from a text file - when it was previously set directly in KM under the relevant Switch action, it would output the correct names. Not sure how to get around this?
Difficult to answer without a sample of the text file, but my guess is that you have the Token in the actual file:
/some/path/stuff/%ICUDateTime%yyyy-MM-dd%/[1] FULL
/some/other/path/%ICUDateTime%yyyy-MM-dd%/[2] PART
..or similar.
Tokens aren't processed when you read a text file to a variable -- they are literal strings. Simplest solution -- after reading the file into your variable, pass that variable through a "Filter" Action set to "Process Tokens":