I know fairly little about scripting, especially when it comes to Mac. On my work I have a script (Windows .BAT file) which copy every jpg image filename in the folder, randomizing the order and saves it to a .txt where the names are delimited by line break.
I have tried, tried and tried to accomplish this on a Mac which my colleague is using. I have been googling and testing for hours - but no success.
I want a file equivalent to a BAT file, to just run directly from the folder.
I have tried both with a .sh file and an AppleScript saved as app, no real use to show the code since they do not work and have been altered and destroyed by me. image of folder with the files
Is there someone who can help med accomplish this? :)
Thanks!
CodePudding user response:
I think I just solved it myself! I placed the navigation command in the script as well, and it worked like a charm! Thank you for putting me in the right direction.
#!/usr/bin/env bash
cd Desktop/NYHETER
ls *.jpg | sort -R > list.txt
CodePudding user response:
You should be able to get a list of JPEGs like this:
ls *.jpg
You should be able to get a randomised list like this:
ls *.jpg | sort -R
And you should be able to get a list, randomise it and save to a file like this:
ls *.jpg | sort -R > list.txt
Note that the "normal" way would be to use shuf
but Apple doesn't supply that with macOS, so I suggested sort -R
.
Another option is to install core-utils
with homebrew and use gshuf
.
CodePudding user response:
I'm not sure why but over the years, several people have suggested to me not to use ls
for retrieving file names for this kind of purpose... That I should use find
instead.
That said, instead of hard-coding the folder path cd Desktop/NYHETER
directly in your code, if you plan on running random.sh directly from within Finder by double clicking it, this following shell script allows the random.sh file to be placed in any folder and will randomize the .jpg file names from any folder containing the script and writes the random names to to file in that same folder.
#! /bin/bash
currentDirectory="$(osascript -e 'tell application "Finder" to set shellScript to container¬
of (get selection as alias) as alias' -e 'set shellScript to POSIX path of shellScript')"
cd "$currentDirectory" || return
find . -name "*.jpg" -mindepth 1 -maxdepth 1 |sort -R |cut -c 3- > ./list.txt