I scan in a large amount of documents and save them in a folder. Each document needs a unique name but may do so as a repetition number. As an example, if I have 3 documents to scan and I want the name to be ‘Big Store’ I want the file names to be:
Big Store.pdf
Big Store #2.pdf
Big Store #3.pdf
Upon scanning I have Keyboard Maestro open a dialog and ask me the name of the file (vendor) and I set the path within Keyboard Maestro (kmPath). I want to enter in ‘Big Store’ and then have the macro continue to save the document and if there is already a file named ‘Big Store’ it would add the #[repetition number].
I currently have an Applescript that Keyboard Maestro runs during this process to count the number of ‘Big Store’ in the folder. This works great if the name of the file is one word like ’BigStore’ however, if there is a space in the name such as ‘Big Store’, Applescript returns the wrong file name count.
Here is the Applescript that interacts with my Keyboard Maestro macro:
tell application "Keyboard Maestro Engine"
set myVar to make variable with properties {name:“Vendor”}
set vendor to value of myVar
set myVar to make variable with properties {name:“kmPath”}
set folderPath to value of myVar
end tell
set theFolder to quoted form of folderPath
try
set vendorList to every paragraph of (do shell script "cd " & theFolder & “;” & "ls " & vendor & “*”)
on error
return {}
end try
set vendorCount to (count of vendorList) + 1
– adding 1 so Keyboard Maestro uses this next value
tell application "Keyboard Maestro Engine"
make variable with properties {name:“vendorCount”, value:vendorCount}
end tell
I am not an Applescript expert so the code I have above was the culmination of many hours trying to find Applescript examples online and making a hybrid of the items.
Is there a way to have Keyboard Maestro count the occurrences of a file in a path, rather than have Applescript do it? If not, does anyone know how to perfect the Applescript so it works with file names containing a space?