Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

patjbrown

macrumors newbie
Original poster
Oct 12, 2017
2
0
Hi - Im very new to scripting and im trying to build a Hazel - Automator - Script workflow that achieves the following (hazel being an If this, then do that software that monitors watch folders)


If watch folder contains a Folder, then run automator workflow

Automator action is

Get Folder Contents (repeat fo each subfolder)

Create new text file from result

New mail containing text file

Email text file

Screen Cap of Workflow

This works great to give me a list of contents, The part I need help with is how to provide a Count of the files. I can produce a list of files in the directory by using the following automator action but im really looking for a count of files inside the folder and its subdirectories.

I had an alternate workflow that id found, which calls an apple script but the script cant find the folder im specifying, so im not sure how to direct it the input folder being fed into the automator action



Heres that workflow

If watch folder contains a Folder, then run automator workflow

Automator action is

Get Value of Variable (Path)

Run Apple Script

tell application "Finder"
set theText to name of theFolder & ":" & return
set k to 0
repeat with thisSubfolder in (get folders of theFolder)
set k to k + 1
set theText to theText & (k as text) & ". " & name of thisSubfolder & ¬
" / " & (count items in thisSubfolder) & return
end repeat
end tell

Create new text file from result

New mail containing text file

Email text file

Screencap of workflow

This workflow gives the error "The variable theFolder is not defined" - So im guessing it cant find the folder I want it to count.


Any ideas on how to feed the script the correct input location?
 
Answer here worked https://discussions.apple.com/message/18812642#18812642

"
Your script isn't picking up input (which is simply a variable name) - instead you've got an undefined variable: thefolder.



And rather than trying to pass the result of the AppleScript to another action, remove the New Text File action from the workflow and try this, which is a variation on the original AppleScript:"

on run {thefolder} --input to the script is an alias, not a Finder item



tell application "Finder"

set thefolder to folder thefolder --alias becomes a Finder item

set theText to name of thefolder & ":" & return



set k to 0



set the_subfolders to folders of thefolder

repeat with each_subfolder in the_subfolders

set k to k + 1

set theText to theText & (k as text) & ". " & name of each_subfolder & ¬

" / " & (count items in each_subfolder) & return

end repeat





end tell

tell application "TextEdit"

activate

make new document with properties {text:theText}

end tell



end run
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.