Adaption of Existing AppleScript

Hello @ccstone, hi Chris,

Quite some years ago you created this beautiful script for me that I have used thousands of times since then:

try
	
	set dbPath to (path to home folder as text) & "Dropbox:"
	set jobsFldr to dbPath & "CT:Pro:"
	
	try
		alias jobsFldr
	on error
		do shell script "mkdir -p " & quoted form of (POSIX path of jobsFldr)
	end try
	
	set _date to do shell script "date \"+%y-%m-%d\""
	--set _date to do shell script "date \"+%d%m%y\""
	
	tell application "Finder"
		activate
		set _sel to selection as alias list
		if length of _sel > 0 then
			set clientName to text returned of (display dialog "Enter Client Name" default answer "Client Name")
			--set fldrName to clientName & _date
			set fldrName to clientName & "20" & _date
			
			try
				set similarJobs to name of folders of folder jobsFldr whose name starts with fldrName
				if length of similarJobs > 0 then
					set lastJob to last item of similarJobs
					set AppleScript's text item delimiters to "-"
					set lastDigit to last text item of lastJob
					set newFldr to make new folder at folder jobsFldr with properties {name:fldrName & "-" & lastDigit + 1}
				end if
			on error
				set newFldr to make new folder at folder jobsFldr with properties {name:fldrName & "-1"}
			end try
			move _sel to newFldr
			reveal newFldr
		end if
	end tell
	
on error e number n
	set e to e & return & return & "Num: " & n
	tell application "Finder" to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
	if button returned of dDlg = "Copy" then set the clipboard to e
end try

The original script contained the line set jobsFldr to dbPath & "CT:Jobs:" but since I want to use a second script to actually store my unzipped XLIFF files etc. in my Proj folders structure, I changed this line to: set jobsFldr to dbPath & "CT:Pro:".

This slightly modified script does exactly what it is supposed to do: move all selected files on the Desktop to a folder like: /Users/hl/Dropbox/CT/Pro/miller2022-12-29-1.

However, I'd like to move files for client Miller to /Users/hl/Dropbox/CT/Pro/miller/miller2022-12-29-1:

If /Users/hl/Dropbox/CT/Pro/miller/ doesn't exist, it should be created.

Would you be willing to make the necessary modifications to script? Many thanks in advance!

BTW: Since you originally created this script, Keyboard Maestro has evolved tremendously. I was wondering: could this task also be managed with KM-only actions?

I'm sure @ccstone can make the AppleScript do what you want and it will run very fast. But since you asked whether this can be done with just Keyboard Maestro actions...

The answer is yes. The fiddly bit is the last digit at the end of the folder name which makes a unique folder each time you run the Macro.

miller2022-12-29-1

If it was just a date at the end and you were happy to have all files moved on the same day end up in the same dated folder that would make the Macro a bit simpler.

A kind of quirk of Keyboard Maestro is that when you tell it to create a folder at a location it will not overwrite if a folder of that name already exists at the location. You would think you would need a condition to say "only if this folder doesn't exist create it" but in fact Keyboard Maestro handles this nicely - making a new folder if it doesn't exist and leaving the folder and its contents alone if it does exist.

In the first Magenta Action you would replace ~/Desktop with the path to where you want to store these folders on Dropbox. But first test that it does what you want by trying out on dummy files.

To use, you first select the files you want to move in the Finder and run the Macro.

EXAMPLE Move Files and Create Folders if do not exist.kmmacros (7.9 KB)

Click to Show Image of Macro

1 Like

Most (all?) scripting languages fail gracefully upon encountering that condition, because to do otherwise would very frequently destroy data users don't want destroyed.

You might want to turn on ‘Create Intermediates’ in the create folder action.

Hey Hans,

Required info ⇢ How are you determining who the client is for the selected file(s)?

This task could always be done via Keyboard Maestro native actions. I just wrote it in AppleScript, because it was easier for me – and because the AppleScripted move is undoable in the Finder.

Keyboard Maestro's move is similar to the shell's in that the actions cannot be undone, therefore I always prefer to do this kind of thing with AppleScript.

-Chris

Hello Chris,

There is just one input box, for the client's name. This name shall be used both for the subfolder /Users/hl/Dropbox/CT/Pro/miller/ and the sub-subfolder /Users/hl/Dropbox/CT/Pro/miller/miller2022-12-30-1.

Thank you @Zabobon!

This macro works perfectly!

And I learned a new thing: assigning hot keys to buttons:

To promote Keyboard Maestro, I created this post.

@ALYB

Please try this:

try
   
   set dbPath to (path to home folder as text) & "Dropbox:"
   set jobsFldr to dbPath & "CT:Pro:"
   
   try
      alias jobsFldr
   on error
      do shell script "mkdir -p " & quoted form of (POSIX path of jobsFldr)
   end try
   
   set _date to do shell script "date \"+%y-%m-%d\""
   --set _date to do shell script "date \"+%d%m%y\""
   
   tell application "Finder"
      activate
      set _sel to selection as alias list
      if length of _sel > 0 then
         set clientName to text returned of (display dialog "Enter Client Name" default answer "Client Name")
         --set fldrName to clientName & _date
         set fldrName to clientName & "20" & _date
         set clientFolder to make new folder at folder jobsFldr with properties {name:clientName}
         try
            set similarJobs to name of folders of clientFolder whose name starts with fldrName
            if length of similarJobs > 0 then
               set lastJob to last item of similarJobs
               set AppleScript's text item delimiters to "-"
               set lastDigit to last text item of lastJob
               set newFldr to make new folder at clientFolder with properties {name:fldrName & "-" & lastDigit + 1}
            end if
         on error
            set newFldr to make new folder at clientFolder with properties {name:fldrName & "-1"}
         end try
         move _sel to newFldr
         reveal newFldr
      end if
   end tell
   
on error e number n
   set e to e & return & return & "Num: " & n
   tell application "Finder" to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
   if button returned of dDlg = "Copy" then set the clipboard to e
end try

Whups – I missed that when reading through the script...

If you're satisfied with @Zabobon's macro then I won't bother monkeying with the script.

I'd want to rewrite it from scratch, and I have other things to do...  :sunglasses:

1 Like

Yes, I’m perfectly happy with @Zabobon's macro :smiley:. Thank you for all your help this year and have a good 2023, Chris!

1 Like