Apple Intelligence is now available on iPhone, iPad, and Mac!

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

Shortcuts App for Mac

I'm trying to create a Shortcut that will check the contents of one folder to another and create a list of files that do not exist in the second folder.

I have been assigning variables to the CONTENTS OF FOLDER of the "source folder" and "the Folder to check", then checking created a repeat loop on the source folder that GETS THE NAME of the repeat item then filters the "Folder to check" folder with name is "Name". Then an If statement that writes appends a note if the there is no value.


I get a list of items in the source folder as my result. I've tried variations but nothing is working. I'm stumped at this point.


MacBook Pro 16″, macOS 12.4

Posted on Apr 8, 2023 1:46 PM

Reply
Question marked as Top-ranking reply

Posted on Apr 11, 2023 10:43 AM

Apple's Script Editor is for AppleScript and JavaScript, but not other scripting languages. If you want to develop shell scripts, then the free BBEdit would be a good start. After its initial trial is over, it defaults to a free text editor.


Replace the entire code in the Run Shell Script with the following:


#!/bin/zsh

: <<"COMMENT"
Script that takes two folders from the command line and compares the files in the first
folder to those in the second folder. It performs an array difference to isolate
files unique to the first folder.

Usage: ${ZSH_ARG0} folder1 folder2

COMMENT

typeset -a firstdir=() otherdir=() diffdir=()

SRCDIR="${1:a:s/\~\//}"
NXTDIR="${2:a:s/\~\//}"

# just put the filename without path or extension (:r:t) in the array
# sort the case-insensitive filenames alphabetically (on)
setopt nocaseglob
firstdir=( ${SRCDIR}/*.psd(.Non:r:t) )
otherdir=( ${NXTDIR}/*.(jpg|jpeg|jp2)(.Non:r:t) )

# array difference comparison
diffdir=( ${firstdir:|otherdir} )
# print out the tilde path to each file in diffdir
for f in ${diffdir};
do
	print -Dl "${SRCDIR}/${f:t}.psd"
done
unset firstdir otherdir diffdir
exit 0


I have stripped the path and file extension of the sorted elements placed in the respective arrays. Additionally, I have allowed for different extensions for jpeg categories. Assumptions here are the first passed folder is .psd files and the second folder is jpegs. Similarly, I have added the .psd extension to the differences files for the text report.


You could wipe the contents of that Run Shell Script and then copy/paste the script code above and paste right back into the Run Shell Script.

Similar questions

9 replies
Question marked as Top-ranking reply

Apr 11, 2023 10:43 AM in response to Tommy-Tunes

Apple's Script Editor is for AppleScript and JavaScript, but not other scripting languages. If you want to develop shell scripts, then the free BBEdit would be a good start. After its initial trial is over, it defaults to a free text editor.


Replace the entire code in the Run Shell Script with the following:


#!/bin/zsh

: <<"COMMENT"
Script that takes two folders from the command line and compares the files in the first
folder to those in the second folder. It performs an array difference to isolate
files unique to the first folder.

Usage: ${ZSH_ARG0} folder1 folder2

COMMENT

typeset -a firstdir=() otherdir=() diffdir=()

SRCDIR="${1:a:s/\~\//}"
NXTDIR="${2:a:s/\~\//}"

# just put the filename without path or extension (:r:t) in the array
# sort the case-insensitive filenames alphabetically (on)
setopt nocaseglob
firstdir=( ${SRCDIR}/*.psd(.Non:r:t) )
otherdir=( ${NXTDIR}/*.(jpg|jpeg|jp2)(.Non:r:t) )

# array difference comparison
diffdir=( ${firstdir:|otherdir} )
# print out the tilde path to each file in diffdir
for f in ${diffdir};
do
	print -Dl "${SRCDIR}/${f:t}.psd"
done
unset firstdir otherdir diffdir
exit 0


I have stripped the path and file extension of the sorted elements placed in the respective arrays. Additionally, I have allowed for different extensions for jpeg categories. Assumptions here are the first passed folder is .psd files and the second folder is jpegs. Similarly, I have added the .psd extension to the differences files for the text report.


You could wipe the contents of that Run Shell Script and then copy/paste the script code above and paste right back into the Run Shell Script.

Apr 8, 2023 4:30 PM in response to Tommy-Tunes

The following code in a Zsh script, when passed the first folder and second folder (in that order) will list the files in the first folder that are not found in the second folder. Tested on macOS 13.3.1 with Zsh 5.9.


#!/bin/zsh

# arrays
typeset -a firstdir=() otherdir=() diffdir=()

# in case tilde paths passed in the Terminal need smacked into absolute (:a) paths
SRCDIR="${1:a:s/\~\//}"
NXTDIR="${2:a:s/\~\//}"

# just put the sorted (on) filename.ext in the arrays using :t
firstdir=( ${SRCDIR}/*(.Non:t) )
otherdir=( ${NXTDIR}/*(.Non:t) )
# array difference comparison, use the * operator for intersections of common elements
diffdir=( ${firstdir:|otherdir} )
# print out the tilde path to each file in diffdir
print -Dl ${${diffdir}:a}
unset firstdir otherdir diffdir
exit 0


If have two folders on my Desktop and the first selected folder has two files not present in the second folder. This script lists those two files from the first folder — when run as a Zsh script, or interactively input in the Terminal.



The same code doesn't display an error but is ignored in a Shortcuts Run Shell Script even when one can confirm that the selected folders are passed in by selection order. This wouldn't happen in an Automator Run Shell Script.


Shortcuts is Apple's strategic direction away from Automator, but for some solutions, it costs considerable time to get a working shortcut that should take five minutes to implement in Automator.

Apr 9, 2023 5:53 AM in response to VikingOSX

A slight correction. To output the correct tilde path from the first folder, one needs this code that incorporates the path to the first folder:


#!/bin/zsh

: <<"COMMENT"
Script that takes two folders from the command line and compares the files in the first
folder to those in the second folder. It performs an array difference to isolate
files unique to the first folder.

Usage: ${ZSH_ARG0} folder1 folder2

COMMENT

typeset -a firstdir=() otherdir=() diffdir=()

SRCDIR="${1:a:s/\~\//}"
NXTDIR="${2:a:s/\~\//}"

# just put the sorted (on) filename.ext in the arrays using :t
firstdir=( ${SRCDIR}/*(.Non:t) )
otherdir=( ${NXTDIR}/*(.Non:t) )
# array difference comparison
diffdir=( ${firstdir:|otherdir} )
# print out the tilde path to each file in diffdir
for f in ${diffdir};
do
	print -Dl "${SRCDIR}/${f:t}"
done
unset firstdir otherdir diffdir
exit 0


which would output:



and without a script, but in the Terminal, one can use the UNIX diff utility to show a difference:


Apr 9, 2023 8:37 AM in response to Tommy-Tunes

The following Automator application will write a text file on your Desktop containing the file paths that are only in your first directory selection. I used two Ask for Finder Items in case the directories that you select are not located in the same location. If they are, you can remove one of the Ask Finder Items, select Allow Multiple Section, and use the ⌘-key to select multiple folders in the one action. I am not processing sub-folders in these selected folders for simplicity and not introducing potential duplicates, although that can be managed if you need it.


Here is the workflow:



and the contents of the text file written to the Desktop where the tilde character replaces /Users/yourname in the filepath.


~/Desktop/A/text0017.txt

~/Desktop/A/text0018.txt

Apr 11, 2023 9:48 AM in response to VikingOSX

I had some time to work with what you have shown me. I tried putting the corrected script in Scrip editor and got all sorts of errors. When you showed me the Shell script being applied within an Automator work flow, I initially thought I had to write the script outside automator, which I again had trouble with. Writing within the "Run Shell Script" box within Automator worked, that is to say it ran with out errors. However it yielded no results (it said 1 item, but none were visible) after the Script box, and the new Text file had no text, though the second folder had 1 file removed.


Apr 11, 2023 11:33 AM in response to Tommy-Tunes

I went back to Shortcuts. I found my problem. When I did my "filter" before my "If" statement, I was checking the name to the file. I first had to get the name of the file and compare file name to file name in my "If" statement.

Because the "name" does not include the extension (by default), the shortcut does what I want it to do, which is output update a Note file with the "missing" names. To bad there wasn't an error message to alert me that I was comparing a file to a name.

Apr 9, 2023 6:42 AM in response to VikingOSX

Thanks for this. Going to take me a while to digest this, since I have not really made or even used scripts much.

I did try Automator first but was getting permission issues, though I can't remember if it was the record function that was a problem. Permission issues are big since my usage for these automations is at my place of work. My thinking on trying shortcuts was that it would be something that would have more support within mac, but also with 3rd party applications. I guess it is too new.

Apr 11, 2023 10:00 AM in response to Tommy-Tunes

But....

The Corrected script that you initially gave me works when put inside Automator!


So all is good - I think - except there is one thing I did not mention. I work with images, and the purpose of the script I am trying to use, is checking that there is a PSD file for every JPG file. The sets of files are kept in separate folders (and as you anticipated, in different locations on a server (thanks for accommodating for that). So though the script runs correctly, but I need it to ignore the file extension when making the comparison.


I tried tweaking what you wrote, but I am not successful at this point. Is there a simple fix to the expression, or does this require a rewrite? I tried putting the closed bracket after the asterisk *} (after doing a search for file name without extension) but to no avail.

Shortcuts App for Mac

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