Filename, wildcards and Get Attribute

I would like to do the following:

  1. In a folder there will be one file which I wish to rename by adding some characters to the end of the existing filename of that file.

  2. The filename of that file will always start with the same wording:

eg "Filename - No. "

and then finish with four numbers:

e.g ‘Filename - No. 1234’

  1. I would use to use the Get Attribute to get the attribute for the Base Name to a variable

  2. Use the variable and then add to the file name some specific wording.

  3. Then move the file to another location.

The difficulty I have is with the Get Attribute function to select the file as the numbers part of the existing filename (e.g. 1234 in the above example) will change each time that the filename is created. Is there any way of using a wildcard with the Get Attribute function?

Hey Victor,

This is one of those cases where AppleScript is probably the best option.

set baseFolder to alias "Athena:Victor_Test_Folder:"
set destFolder to alias "Athena:Dest_Folder:"

tell application "Finder"
  set fileList to (files of baseFolder whose name starts with "My_File_Name") as alias list

  if length of fileList = 1 then
    set theFile to item 1 of fileList
    set fileSuffix to name extension of theFile
    set AppleScript's text item delimiters to "." & fileSuffix
    set newFileName to text item 1 of (get name of theFile)
    set newFileName to newFileName & "_Some_More_Text" & "." & fileSuffix
    set name of theFile to newFileName
    
    move theFile to destFolder
    
  end if
end tell

If you can be more specific about the file name and changes to same, I can help you flesh this out.

Best Regards,
Chris

Chris,

Thank you for your response and the code. It is a pity that Keyboard Maestro cannot support wildcards for filenames.

But to answer your query:

I am scanning a set of reports. Each report is named “NLJ - No. 1234”. The NLJ - No. is the same for each scanned report. the 1234 is the number of the report and changes for each report. After the scanning and the OCR of the text, I wish to be prompted and then add a date for the report.

Does the Applescript prompt for the date?

Victor,

I too do a lot of scanning and had a similar need to work with scanned files. You might find that thread interesting to read. Though there were several suggestions which included AppleScript, Peter offered one that worked great without Applescript.

I also shared a macro I created that deals with file names.

Perhaps this will give you some insight into the kinds of things Keyboard Maestro can do with naming files.

As for moving (and renaming files) someone pointed me to a program call Hazel. Noodlesoft – Noodlesoft – Simply Useful Software
At first glance I thought Hazel was cool but could not see a need for it.... I was wrong! I download about 20 bank statements a month and Hazel watches my download folder, looks for set rules which include scanning the content of the downloaded pdfs, renames the files based on content it finds IN THE DOCUMENTS and moves them to locations based on CONTENT IN THE DOCUMENTS. You could do the same kind of thing by scanning all documents to a given folder and then have Hazel do its magic.

. . . Stephen

[quote=“Victor_Warner, post:3, topic:629”]
Thank you for your response and the code. It is a pity that Keyboard Maestro cannot support wildcards for filenames.[/quote]

Hey Victor,

Keyboard Maestro has full-blown regular expression support, so it can do all sorts of things. It also can run shell scripts, so wildcard driven file listings are easy.

In your case it just seems simpler to use AppleScript.

Why would you want to manually enter a date instead of automate it?

Please describe your actual workflow step by step.

Best Regards,
Chris

This will get the first (alphabetically) path in the Desktop containing the words Screen Shot. You can adjust the match to use a regular expression if necessary, or select the file by any other means.

Warning: if there is no matching file, then Path will be the path of the last file in the directory, so you may wish to repeat your test at the bottom to ensure it has found a match.

Thank you for all the replies.

Reply to Chris about why I wish to add the date manually: I already use Hazel and it have can pick up the contents of the file such as the number of the report and the date etc which will be added to the filename. However, the reports are printed on poor quality paper and none of the OCR programs I have used can pick up the date of the report (and thus make it impossible for Hazel to extract the date).

Workflow:

  1. Scan document to PDF to scan folder
  2. Run OCR through Adobe Acrobat
  3. Hazel rule to rename file to NLJ No. 1111 (the number being obtained from the contents of the document)
  4. Using Keyboard Maestro macro to further rename file via a prompt to allow me to enter the date of the report)
  5. Embed the Keyboard Maestro macro within the Hazel rule via AppleScript.
  6. Hazel then to move the file to another folder.

Hey Victor,

So you're using the date of the report as part of the file name rather than the current date. Okay, that makes sense.

set fileName to "theFileName"
set dateStr to text returned of (display dialog "Please Enter a Date String:" default answer "false" giving up after 30)

if dateStr ≠ false then
	set fileName to fileName & " " & dateStr
else
	error "The dateStr dialog timed-out!"
end if

I don't have Hazel, so I'm not sure what's possible. I'd hope the rule could pass the file name to the AppleScript.

Run the script in the Applescript Editor to see how it works.

Using this you may be able to bypass KM for this particular task. KM has prettier dialogs, but this works.

Best Regards,
Chris