Scanning a list of files and adding part of a name to an Excel file or Google sheet

Hey Panos,

Okay. I've done as you asked.

(I'm assuming you meant the macro and not the AppleScript.)

I've also changed the path and file window actions to automatically get the current year and month.

~/Downloads/%ICUDateTime%Y-MM% - Meals.xlsx

You need to change the ~/Downloads/ portion of that to be correct on your system.

And of course you need to change to root meals folder path to be correct as well.

~/test_directory/INSYS/Meals

-Chris

Extract Meal Data for Excel Spreadsheet v1.02 @working.kmmacros (12 KB)

Hey Panos,

That's not a bad idea.

I've written this task in AppleScript again, but I've used only vanilla AppleScript and the Keyboard Maestro Engine's AppleScript functions.

You need to adjust these two lines:

set mealsRootPath to "~/test_directory/INSYS/Meals/"
set excelFile to "~/Downloads/yyyy-mm - Meals.xlsx"

Do not touch this part of the Excel file path:

/yyyy-mm - Meals.xlsx

Run the AppleScript from the Script Editor.app to see how it works.

Run it from an Execute an AppleScript action in Keyboard Maestro.

The script gets the same data the others do, but it omits the week. Use Excel's =WEEKNUM(cellRef) function to populate your WEEK column.

The script finds the correct insertion point in Excel and pastes automatically, and that should function more organically than brute-forcing the UI as we did with Keyboard Maestro. This should also prevent the timing issues you were having with the Keyboard Maestro macro.

When you run the script the currently existing files of the current month will be processed and marked with a green label. Subsequent runs of the script will ignore already labeled files.

I'm using Excel from Office 2011, so I can't guarantee it'll work with Office 2016 – but chances are that it will.

-Chris

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/04/04 05:19
# dMod: 2018/04/04 05:46
# Appl: Finder, Keyboard Maestro Engine, Microsoft Excel, System Events
# Task: Extract Meal Data from file names in Finder and paste them into an Excel spreadsheet.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Keyboard_Maestro_Engine, @Microsoft_Excel, @System_Events, @Extract, @Meal, @Data, @File, @Names, @Paste, @Excel, @Spreadsheet
# Vers: 1.00
----------------------------------------------------------------

set mealsRootPath to "~/test_directory/INSYS/Meals/"
set excelFile to "~/Downloads/yyyy-mm - Meals.xlsx"

set yearMonth to text 1 thru 7 of ((current date) as «class isot» as string)
set excelFile to kmReplace("yyyy-mm", yearMonth, excelFile, false, false, false)
set yearMonth to kmReplace("-", "/", yearMonth, false, false, false)
set mealsMonthPath to mealsRootPath & yearMonth

tell application "System Events"
   set excelFile to (path of file excelFile) as alias
   set excelFileName to name of excelFile
   set fileList to path of files of folder mealsMonthPath whose visible is true
end tell

tell application "Finder"
   repeat with theFile in fileList
      set fileRef to theFile as alias
      if fileRef's label index = 6 then
         set contents of theFile to 0
      else
         set fileRef's label index to 6
         set contents of theFile to name of fileRef
      end if
   end repeat
   
end tell

set fileList to text of fileList
set AppleScript's text item delimiters to linefeed
set fileList to fileList as text
set fileList to kmReplace(" - | (?=\\d)", tab, fileList, true, false, false)
set fileList to kmReplace("(?m)\\.pdf$", "", fileList, true, false, false)

set the clipboard to fileList

if fileList ≠ "" then
   tell application "Microsoft Excel"
      if not running then
         run
         delay 5
      end if
      activate
      if document excelFileName exists then
         activate object window excelFileName
      else
         open excelFile
      end if
      
      tell active sheet of document excelFileName
         set lastfilledcell to get end (range "B65536") direction toward the top
         set firstBlankCell to get offset lastfilledcell row offset 1
         select firstBlankCell
         paste
      end tell
      
   end tell
end if

----------------------------------------------------------------
--» HANDLERS
----------------------------------------------------------------
on kmReplace(findPattern, replacePattern, dataStr, regExBool, caseBool, tokensBool)
   tell application "Keyboard Maestro Engine"
      set foundDataList to search dataStr for findPattern replace replacePattern ¬
         regex regExBool case sensitive caseBool process tokens tokensBool
   end tell
end kmReplace
----------------------------------------------------------------

:astonished: consider my mind blown…

I already implemented your first change and it works great!
I’ll try the Apple Script solution and let you know how it works.

Thanks once again!

Hey Chris,

I tried it and at one point i got an error which I think is related to the fact that the name of the excel file as it appears in Excel 2017 is without the extension.

For some reason I don't get the error any more but nothing is pasted in Excel.
Everything is there on the clipboard, but if I try to type "CMD + V" everything is pasted only in the first column.

Thanks,

Panos

Somehow now I don't get the error any more and since I removed the green tag from the finder and tested it again, everything is pasted in the correct excel file but only in one column.

Hey Panos,

That may be a difference in Office 2016 OR in our settings...

On my system Excel knows the data is tab-delimited and acts accordingly.

I'll see about fixing it later – right now I'm seriously knackered and need a nap.

-Chris

No problem.

I investigated a little more, and discovered that if I paste special, the below three options appear.

37

Only the first (default) option pastes everything in one column.
The other two options have the desired result.

Unfortunately I cannot find an option in preferences to change the default.

Hey Panos,

Try this script. I’ve used the paste special command instead of paste, and I hope that will change the behavior on your system.

-Chris

----------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2018/04/04 05:19
# dMod: 2018/04/06 03:38
# Appl: Finder, Keyboard Maestro Engine, Microsoft Excel, System Events
# Task: Extract Meal Data from file names in Finder and paste them into an Excel spreadsheet.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Keyboard_Maestro_Engine, @Microsoft_Excel, @System_Events, @Extract, @Meal, @Data, @File, @Names, @Paste, @Excel, @Spreadsheet
# Vers: 1.01
----------------------------------------------------------------

set mealsRootPath to "~/test_directory/INSYS/Meals/"
set excelFile to "~/Documents/Clients/Panos Matiakis ⇢ KM Forum/2018-04 - Meals.xlsx"

set yearMonth to text 1 thru 7 of ((current date) as «class isot» as string)
set excelFile to kmReplace("yyyy-mm", yearMonth, excelFile, false, false, false)
set yearMonth to kmReplace("-", "/", yearMonth, false, false, false)
set mealsMonthPath to mealsRootPath & yearMonth

tell application "System Events"
   set excelFile to (path of file excelFile) as alias
   set excelFileName to name of excelFile
   set fileList to path of files of folder mealsMonthPath whose visible is true
end tell

tell application "Finder"
   repeat with theFile in fileList
      set fileRef to theFile as alias
      if fileRef's label index = 6 then
         set contents of theFile to 0
      else
         set fileRef's label index to 6
         set contents of theFile to name of fileRef
      end if
   end repeat
   
end tell

set fileList to text of fileList
set AppleScript's text item delimiters to linefeed
set fileList to fileList as text
set fileList to kmReplace(" - | (?=\\d)", tab, fileList, true, false, false)
set fileList to kmReplace("(?m)\\.pdf$", "", fileList, true, false, false)

set the clipboard to fileList

if fileList ≠ "" then
   tell application "Microsoft Excel"
      if not running then
         run
         delay 5
      end if
      activate
      if document excelFileName exists then
         activate object window excelFileName
      else
         open excelFile
      end if
      
      tell active sheet of document excelFileName
         set lastfilledcell to get end (range "B65536") direction toward the top
         set firstBlankCell to get offset lastfilledcell row offset 1
         select firstBlankCell
      end tell
      
      tell active cell
         set theCell to it
         set cellRange to item 1 of (get areas)
      end tell
      
      paste special (theCell) what paste values
      
   end tell
end if

----------------------------------------------------------------
--» HANDLERS
----------------------------------------------------------------
on kmReplace(findPattern, replacePattern, dataStr, regExBool, caseBool, tokensBool)
   tell application "Keyboard Maestro Engine"
      set foundDataList to search dataStr for findPattern replace replacePattern ¬
         regex regExBool case sensitive caseBool process tokens tokensBool
   end tell
end kmReplace
----------------------------------------------------------------