Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator - dynamic folder creation and traverse

Hello all,

I am trying to automate screenshot creation.

Workflow I need:

Screenshot all the monitors

Set the proper name to include time

and save it to a folder structure dynamically created by current date.


So basically the idea of the workflow from the technical perspective is:

1) Check, if year folder exists, if not, create it

2) Traverse inside, check if month folder exists, if not, create it.

3) Traverse inside, check if day folder exists, if not, create it.

4) Traverse inside, save the picture there.

5) Delete everything past XXX days (so far, optional)


But I have hit some limitations that I need helping with:


I have figured out the variables. Nice, I can have e.g. current day and create a folder with that name. However, the folder does not exist, so the next step, where I need to create another folder seems to be impossible, because I cannot select the folder that does not exist. I tried to even create a new variariable, but to that popup, you cannot drag/drop the existing variables to the chain.


Same for the picture, where I need to select where to store it, but as the folders does not exist yet, I cannot select the destination.


Other thing is, I don`t see any option for checking, if the folder already exists, so the system won`t try to create a duplicate folder each time the workflow runs. Even in the Automator app, there seems to be no condition, only action after action approach.


Any ideas here, please?


Thank you!

MacBook Pro

Posted on Jun 21, 2022 8:50 AM

Reply
Question marked as Best reply

Posted on Jun 22, 2022 2:49 AM

Don't overlook the macOS screencapture utility that you can apply in your Run Shell Script action, instead of the Automator Take Screenshot action.


man screencapture


The screencapture utility cannot take three concurrent screen snaps, but it can simultaneously capture each screen in a for-loop with the screencapture -D switch:


SCREENS=3
for s in {1..$SCREENS};
do
   /usr/sbin/screencapture -D $s -x -r /path/to/folder
done



Similar questions

5 replies
Question marked as Best reply

Jun 22, 2022 2:49 AM in response to Sergth

Don't overlook the macOS screencapture utility that you can apply in your Run Shell Script action, instead of the Automator Take Screenshot action.


man screencapture


The screencapture utility cannot take three concurrent screen snaps, but it can simultaneously capture each screen in a for-loop with the screencapture -D switch:


SCREENS=3
for s in {1..$SCREENS};
do
   /usr/sbin/screencapture -D $s -x -r /path/to/folder
done



Jun 21, 2022 11:35 AM in response to Sergth

Automator is great at linear tasks. If fails miserably when there is any kind of logic (e.g. "does the folder already exist?"), error handling, and looping.


Since your workflow has multiple variables and checks, I'd suggest moving up the stack and using something more flexible such as AppleScript (which can be invoked via a Run AppleScript action) or a shell script.


Another twist is that some of the built-in shell commands can already take care of some of the idiosyncrasies for you - for example, this shell command will create a directory tree on your desktop, creating directory levels as needed, and not erroring if the directory already exists:


mkdir -p ~/Desktop/2022/06/21


This one line takes care of your steps 1, 2, and 3



This AppleScript (which you can incorporate into your Workflow) should meet your basic req - creating the directories and saving the screenshot


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- get the current date
set cur_date to (get current date)
-- extract the date components
tell cur_date
	set y to year as text
	set m to my trim(its month as number)
	set d to day as text
	-- initially, the time is represented as a count of seconds since midnight
	-- this calls a handler to format it in a human-readable manner
	set t to my formatTime(time)
end tell

set the_path to "~/Desktop/" & y & "/" & m & "/" & d
do shell script "mkdir -p " & the_path

do shell script "cd " & the_path & "; screencapture " & t & ".png"

on formatTime(t)
	-- take the number of seconds and break out hours, mins secs
	set the_hours to my trim(t div 60 div 60)
	set the_mins to my trim(t div 60 mod 60)
	set the_secs to my trim(t mod 60)
	-- format it into a human readable form
	return the_hours & "_" & the_mins & "_" & the_secs as text
end formatTime

on trim(num)
	-- quick method to prepend 0 to single-digit numbers
	-- this turns "6" into "06"
	return characters -2 through -1 of ("0" & num) as text
end trim


There's a little more work to do - specifically how to handle multiple screens (appending arguments to the screencapture command), and the cleanup (which is pretty easy, just have to work out the scheduling)

Jun 21, 2022 11:38 AM in response to Sergth

Since Automator is water over a waterfall of data between subsequent actions, it has no logic or control blocks unless you create them in a Run AppleScript or Run Shell Script action. For instance, let's say you want to create a year, month and day folder hierarchy based on the current date and you are using the Zsh shell:


mkdir -p "${(j:/:)${(s:-:)$(date +%F)}}"


would create the following folder hierarchy in the current folder:



The (s:-:) splits the 2022-06-21 date/time format (%F) into an array of three elements, and then (j:/:) joins those array elements into a new string that is interpreted by mkdir as:


mkdir -p 2022/06/21


which then creates the outer 2022 directory, a 06 month sub-folder, and then the 21 sub-folder within the month. If these already exists, then mkdir -p quietly does nothing.

Jun 22, 2022 2:20 AM in response to VikingOSX

Hi, thank you for the reply. In the meantime, I played with the shell as well, and althoug I was in the end able to achieve the correct behavior through many checks, e.g.



if ! [[ -d ~/Pictures/Screenshot_flow/timeline/$(date +%Y) ]]
then
mkdir ~/Pictures/Screenshot_flow/timeline/$(date +%Y)
fi

if ! [[ -d ~/Pictures/Screenshot_flow/timeline/$(date +%Y)/$(date +%m) ]]
then
mkdir ~/Pictures/Screenshot_flow/timeline/$(date +%Y)/$(date +%m)
fi

...etc..


your solution is much more efficient.


Unfortunately, the whole concept turned very wrong, as it seems even the Automator app wont be the solution.


What I have discovered is, that wen I call "MyCompiledWorkflow.app", macOS is ALWAYS ask for permission (popup) to allow screen recording, even when the app is already enabled in the security settings.


The second issue, that Take screenshot action, even if you UNCHECK the "Main monitor only", it never screenshots all 3 connected monitors at once, so anyway, this automator app in the end won`t work for me as well and I need to look elsewhere.


Anyway, thank you !

Jun 22, 2022 2:21 AM in response to Camelot

Hi, thank you for the reply. I played with shell, where it seems to be more easier to write, but I suppose both shell script and your Apple Script will in the end do the same result.


Unfortunately, the whole concept turned very wrong, as it seems even the Automator app won`t be the solution.


What I have discovered is, that wen I call "MyCompiledWorkflow.app", macOS is ALWAYS ask for permission (popup) to allow screen recording, even when the app is already enabled in the security settings.


The second issue, that Take screenshot action, even if you UNCHECK the "Main monitor only", it never screenshots all 3 connected monitors at once, so anyway, this automator app in the end won`t work for me as well and I need to look elsewhere.


Anyway, thank you!

Automator - dynamic folder creation and traverse

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.